Skip to content

Commit 411d1e8

Browse files
committed
fix: allow importing relative paths in global resources
1 parent e632171 commit 411d1e8

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

e2e/__projects__/style/colors.less

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

e2e/__projects__/style/colors.scss

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

e2e/__projects__/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/__projects__/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;

lib/process-style.js

+19-7
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
@@ -8,18 +7,31 @@ const getCustomTransformer = require('./utils').getCustomTransformer
87
const logResultErrors = require('./utils').logResultErrors
98
const loadSrc = require('./utils').loadSrc
109

11-
function getGlobalResources(resources, lang) {
10+
function getGlobalResources(resources, lang, filePath) {
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 relativePath = path.relative(
16+
path.dirname(filePath),
17+
path.resolve(process.cwd(), resource)
18+
)
19+
return `${getImportLine(lang, relativePath)}\n`
20+
})
21+
.join('')
1922
}
2023
return globalResources
2124
}
2225

26+
function getImportLine(lang, filePath) {
27+
const placeholder = '__PATH__'
28+
const formats = {
29+
default: `@import "${placeholder}";`,
30+
sass: `@import "${placeholder}"`
31+
}
32+
return (formats[lang] || formats.default).replace(placeholder, filePath)
33+
}
34+
2335
function extractClassMap(cssCode) {
2436
const cssNames = cssExtract.extractClasses(cssCode)
2537
const cssMap = {}
@@ -68,7 +80,7 @@ module.exports = function processStyle(stylePart, filePath, config = {}) {
6880
}
6981

7082
let content =
71-
getGlobalResources(vueJestConfig.resources, stylePart.lang) +
83+
getGlobalResources(vueJestConfig.resources, stylePart.lang, filePath) +
7284
stylePart.content
7385

7486
const transformer =

0 commit comments

Comments
 (0)