-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess-style.js
108 lines (96 loc) · 3.3 KB
/
process-style.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const path = require('path');
const fs = require('fs');
const cssExtract = require('extract-from-css');
const getVueJestConfig = require('./utils').getVueJestConfig;
const compileStyle = require('@vue/component-compiler-utils').compileStyle;
const applyModuleNameMapper = require('./module-name-mapper-helper');
const getCustomTransformer = require('./utils').getCustomTransformer;
const logResultErrors = require('./utils').logResultErrors;
const loadSrc = require('./utils').loadSrc;
function getGlobalResources(resources, lang) {
let globalResources = '';
if (resources && resources[lang]) {
globalResources = resources[lang]
.map((resource) => path.resolve(process.cwd(), resource))
.filter((resourcePath) => fs.existsSync(resourcePath))
.map((resourcePath) => fs.readFileSync(resourcePath).toString())
.join('\n');
}
return globalResources;
}
function extractClassMap(cssCode) {
const cssNames = cssExtract.extractClasses(cssCode);
const cssMap = {};
for (let i = 0, l = cssNames.length; i < l; i++) {
cssMap[cssNames[i]] = cssNames[i];
}
return cssMap;
}
function getPreprocessOptions(lang, filePath, jestConfig) {
if (lang === 'scss' || lang === 'sass') {
return {
importer: (url, prev, done) => ({
file: applyModuleNameMapper(
url,
prev === 'stdin' ? filePath : prev,
jestConfig,
lang,
),
}),
};
}
if (lang === 'styl' || lang === 'stylus') {
return {
paths: [path.dirname(filePath), process.cwd()],
};
}
}
module.exports = function processStyle(stylePart, filePath, config = {}) {
const vueJestConfig = getVueJestConfig(config);
if (stylePart.src && !stylePart.content.trim()) {
const cssFilePath = applyModuleNameMapper(
stylePart.src,
filePath,
config,
stylePart.lang,
);
stylePart.content = loadSrc(cssFilePath, filePath);
filePath = cssFilePath;
}
if (vueJestConfig.experimentalCSSCompile === false || !stylePart.content) {
return '{}';
}
let content =
getGlobalResources(vueJestConfig.resources, stylePart.lang) +
stylePart.content;
const transformer =
getCustomTransformer(vueJestConfig.transform, stylePart.lang) || {};
// pre process
if (transformer.preprocess) {
content = transformer.preprocess(content, filePath, config, stylePart.attrs);
}
// transform
if (transformer.process) {
content = transformer.process(content, filePath, config, stylePart.attrs);
} else {
const preprocessOptions = getPreprocessOptions(
stylePart.lang,
filePath,
config,
);
const result = compileStyle({
source: content,
filePath,
preprocessLang: stylePart.lang,
preprocessOptions,
scoped: false,
});
logResultErrors(result);
content = result.code;
}
// post process
if (transformer.postprocess) {
return transformer.postprocess(content, filePath, config, stylePart.attrs);
}
return JSON.stringify(extractClassMap(content));
};