Skip to content

Commit b0c843a

Browse files
authored
fix: allow importing relative paths in global resources (#548)
1 parent ec74482 commit b0c843a

File tree

10 files changed

+44
-14
lines changed

10 files changed

+44
-14
lines changed

e2e/2.x/style/colors.less

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@primary-color: "red";

e2e/2.x/style/colors.scss

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$primary-color: #333;

e2e/2.x/style/variables.less

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
@primary-color: "red";
1+
@import "./colors.less";
2+
3+
@font-size: 16px;

e2e/2.x/style/variables.scss

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
$primary-color: #333;
1+
@import './colors.scss';
2+
3+
$font-size: 16px;

e2e/3.x/style/colors.less

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@primary-color: "red";

e2e/3.x/style/colors.scss

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$primary-color: #333;

e2e/3.x/style/variables.less

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
@primary-color: "red";
1+
@import "./colors.less";
2+
3+
@font-size: 16px;

e2e/3.x/style/variables.scss

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
$primary-color: #333;
1+
@import './colors.scss';
2+
3+
$font-size: 16px;

packages/vue2-jest/lib/process-style.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const path = require('path')
2-
const fs = require('fs')
32
const cssTree = require('css-tree')
43
const getVueJestConfig = require('./utils').getVueJestConfig
54
const compileStyle = require('@vue/component-compiler-utils').compileStyle
@@ -12,14 +11,23 @@ function getGlobalResources(resources, lang) {
1211
let globalResources = ''
1312
if (resources && resources[lang]) {
1413
globalResources = resources[lang]
15-
.map(resource => path.resolve(process.cwd(), resource))
16-
.filter(resourcePath => fs.existsSync(resourcePath))
17-
.map(resourcePath => fs.readFileSync(resourcePath).toString())
18-
.join('\n')
14+
.map(resource => {
15+
const absolutePath = path.resolve(process.cwd(), resource)
16+
return `${getImportLine(lang, absolutePath)}\n`
17+
})
18+
.join('')
1919
}
2020
return globalResources
2121
}
2222

23+
function getImportLine(lang, filePath) {
24+
const importLines = {
25+
default: `@import "${filePath}";`,
26+
sass: `@import "${filePath}"`
27+
}
28+
return importLines[lang] || importLines.default
29+
}
30+
2331
function extractClassMap(cssCode) {
2432
const ast = cssTree.parse(cssCode)
2533

@@ -35,6 +43,7 @@ function extractClassMap(cssCode) {
3543
function getPreprocessOptions(lang, filePath, jestConfig) {
3644
if (lang === 'scss' || lang === 'sass') {
3745
return {
46+
filename: filePath,
3847
importer: (url, prev, done) => ({
3948
file: applyModuleNameMapper(
4049
url,

packages/vue3-jest/lib/process-style.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const { compileStyle } = require('@vue/compiler-sfc')
22
const path = require('path')
3-
const fs = require('fs')
43
const cssTree = require('css-tree')
54
const getVueJestConfig = require('./utils').getVueJestConfig
65
const applyModuleNameMapper = require('./module-name-mapper-helper')
@@ -12,14 +11,23 @@ function getGlobalResources(resources, lang) {
1211
let globalResources = ''
1312
if (resources && resources[lang]) {
1413
globalResources = resources[lang]
15-
.map(resource => path.resolve(process.cwd(), resource))
16-
.filter(resourcePath => fs.existsSync(resourcePath))
17-
.map(resourcePath => fs.readFileSync(resourcePath).toString())
18-
.join('\n')
14+
.map(resource => {
15+
const absolutePath = path.resolve(process.cwd(), resource)
16+
return `${getImportLine(lang, absolutePath)}\n`
17+
})
18+
.join('')
1919
}
2020
return globalResources
2121
}
2222

23+
function getImportLine(lang, filePath) {
24+
const importLines = {
25+
default: `@import "${filePath}";`,
26+
sass: `@import "${filePath}"`
27+
}
28+
return importLines[lang] || importLines.default
29+
}
30+
2331
function extractClassMap(cssCode) {
2432
const ast = cssTree.parse(cssCode)
2533

@@ -35,6 +43,7 @@ function extractClassMap(cssCode) {
3543
function getPreprocessOptions(lang, filePath, jestConfig) {
3644
if (lang === 'scss' || lang === 'sass') {
3745
return {
46+
filename: filePath,
3847
importer: (url, prev) => ({
3948
file: applyModuleNameMapper(
4049
url,

0 commit comments

Comments
 (0)