1
1
const path = require ( 'path' )
2
- const fs = require ( 'fs' )
3
2
const cssExtract = require ( 'extract-from-css' )
4
3
const getVueJestConfig = require ( './utils' ) . getVueJestConfig
5
4
const compileStyle = require ( '@vue/component-compiler-utils' ) . compileStyle
@@ -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 cssNames = cssExtract . extractClasses ( cssCode )
25
33
const cssMap = { }
@@ -32,6 +40,7 @@ function extractClassMap(cssCode) {
32
40
function getPreprocessOptions ( lang , filePath , jestConfig ) {
33
41
if ( lang === 'scss' || lang === 'sass' ) {
34
42
return {
43
+ filename : filePath ,
35
44
importer : ( url , prev , done ) => ( {
36
45
file : applyModuleNameMapper (
37
46
url ,
0 commit comments