forked from tompascall/js-to-styles-var-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
136 lines (117 loc) · 4.25 KB
/
index.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const path = require('path');
const decache = require('decache');
const squba = require('squba');
const requireReg = /\@import-js\s*(["'])([\w.\/]+)(?:\1)((?:\.[\w_-]+)*);?/igm;
const operator = {
validateExportType (data, relativePath) {
if (Object.prototype.toString.call(data) !== '[object Object]') {
throw new Error(`Export must be an object '${relativePath}'`);
}
},
// Ensure it is a flat object with finite number/string values.
validateVariablesValue(value, property, relativePath) {
if (Object.prototype.toString.call(value) !== '[object Object]') {
throw new Error(`Only an object can be converted to style vars (${relativePath}${property})`);
}
const keys = Object.keys(value);
for (const k of keys) {
if (!(
// Define ok types of value (can be output as a style var)
typeof value[k] === "string"
|| (typeof value[k] === "number" && Number.isFinite(value[k]))
)) {
throw new Error(
`Style vars must have a value of type "string" or "number". Only flat objects are supported. ` +
`In: ${relativePath}${property ? ":" : ""}${property}`);
}
}
return true;
},
getVarData (relativePath, property) {
decache(relativePath);
const data = require(relativePath);
if (!data) {
throw new Error(`No data in '${relativePath}'`);
}
this.validateExportType(data, relativePath);
if (property) {
const propVal = squba(data, property);
this.validateExportType(propVal, relativePath);
this.validateVariablesValue(propVal, property, relativePath)
return propVal;
}
return data;
},
transformToSassVars (varData) {
const keys = Object.keys(varData);
return keys.reduce( (result, key) => {
result += `$${key}: ${varData[key]};\n`;
return result;
}, '');
},
transformToLessVars (varData) {
const keys = Object.keys(varData);
return keys.reduce( (result, key) => {
result += `@${key}: ${varData[key]};\n`;
return result;
}, '');
},
transformToStyleVars ({ type, varData } = {}) {
switch (type) {
case 'sass':
return this.transformToSassVars(varData);
case 'less':
return this.transformToLessVars(varData);
default:
throw Error(`Unknown preprocessor type: ${type}`);
}
},
propDeDot (strPropMatch) {
if (!strPropMatch || strPropMatch[0] !== ".")
return strPropMatch;
else
return strPropMatch.substr(1);
},
mergeVarsToContent (content, webpackContext, preprocessorType) {
const replacer = function (m,q, relativePath, property) {
const modulePath = path.join(webpackContext.context, relativePath)
const varData = this.getVarData(modulePath, this.propDeDot(property));
webpackContext.addDependency(modulePath);
return this.transformToStyleVars({
type: preprocessorType,
varData
});
}
return content.replace(
requireReg,
replacer.bind(this)
);
},
getResource (context) {
return context._module.resource;
},
getPreprocessorType ( { resource } ={}) {
const preProcs = [
{
type: 'sass',
reg: /\.scss$|\.sass$/
},
{
type: 'less',
reg: /\.less$/
}
];
const result = preProcs.find( item => item.reg.test(resource));
if (result) return result.type;
throw Error(`Unknown preprocesor type for ${resource}`);
}
};
exports.operator = operator;
const loader = function (content) {
const webpackContext = this;
const resource = operator.getResource(webpackContext);
const preprocessorType = operator.getPreprocessorType({ resource });
const merged = operator.mergeVarsToContent(content, webpackContext, preprocessorType);
return merged;
};
exports.default = loader;