1
1
const { compileStyle } = require ( '@vue/compiler-sfc' )
2
2
const path = require ( 'path' )
3
- const fs = require ( 'fs' )
4
3
const cssTree = require ( 'css-tree' )
5
4
const getVueJestConfig = require ( './utils' ) . getVueJestConfig
6
5
const applyModuleNameMapper = require ( './module-name-mapper-helper' )
@@ -12,14 +11,23 @@ function getGlobalResources(resources, lang) {
12
11
let globalResources = ''
13
12
if ( resources && resources [ lang ] ) {
14
13
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 ( '' )
19
19
}
20
20
return globalResources
21
21
}
22
22
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
+
23
31
function extractClassMap ( cssCode ) {
24
32
const ast = cssTree . parse ( cssCode )
25
33
@@ -35,6 +43,7 @@ function extractClassMap(cssCode) {
35
43
function getPreprocessOptions ( lang , filePath , jestConfig ) {
36
44
if ( lang === 'scss' || lang === 'sass' ) {
37
45
return {
46
+ filename : filePath ,
38
47
importer : ( url , prev ) => ( {
39
48
file : applyModuleNameMapper (
40
49
url ,
0 commit comments