Skip to content

Commit 51f0c90

Browse files
authored
fix: allow importing relative paths in global resources (#373)
* fix: allow importing relative paths in global resources * fix: allow importing relative paths in style blocks
1 parent efe0488 commit 51f0c90

File tree

5 files changed

+22
-7
lines changed

5 files changed

+22
-7
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;

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 cssExtract = require('extract-from-css')
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 cssNames = cssExtract.extractClasses(cssCode)
2533
const cssMap = {}
@@ -32,6 +40,7 @@ function extractClassMap(cssCode) {
3240
function getPreprocessOptions(lang, filePath, jestConfig) {
3341
if (lang === 'scss' || lang === 'sass') {
3442
return {
43+
filename: filePath,
3544
importer: (url, prev, done) => ({
3645
file: applyModuleNameMapper(
3746
url,

0 commit comments

Comments
 (0)