-
Notifications
You must be signed in to change notification settings - Fork 1
/
vue-css-modules.js
57 lines (53 loc) · 1.9 KB
/
vue-css-modules.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
global.vue = global.vue || {}
// global.vue.lang = global.vue.lang || {}
global.vue.cssModules = compileStyles;
function compileStyles({
source,
inputFile,
dependencyManager,
tag,
cssModules,
}) {
try {
if (!global.cssModules) {
console.error('nathantreid:vue-css-modules sometimes requires the nathantreid:css-modules package to be installed. To add the package, run the following command:');
console.error('\nmeteor add nathantreid:css-modules\n');
console.error('By default, nathantreid:css-modules will process your .css files as well. To avoid this, add the following to your package.json:');
console.error(`
{
"cssModules": {
"extensions": ["dont-process-anything"],
}
}
`);
throw new Error("See above error message");
}
const result = global.cssModules.compiler.compileFromSource(source, inputFile);
const compileResult = result.compileResult;
let importsCode;
if (compileResult.imports) {
importsCode = compileResult.imports.map(importPath => `require ('${importPath}');`).join('\n');
}
if (compileResult.importTree) {
compileResult.importTree.forEach(importPath => dependencyManager.addDependency(importPath));
}
if (compileResult.stylesCode) {
const moduleName = typeof tag.attrs.module === 'string' ? tag.attrs.module : '$style';
if (cssModules === undefined) {
cssModules = {};
}
cssModules[moduleName] = compileResult.stylesCode;
}
const vueResult = {
css: result.compileResult.stylesheet,
map: JSON.stringify(result.sourceMap),
cssModules,
js: importsCode,
};
return vueResult;
} catch (err) {
console.error(err);
// console.error(err.stack);
throw err;
}
}