-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjsvars-loader.js
More file actions
30 lines (27 loc) · 881 Bytes
/
jsvars-loader.js
File metadata and controls
30 lines (27 loc) · 881 Bytes
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
/**
* webpack loader that converts a JS file import into variables that postcss-simple-vars can understand.
*/
var path = require('path');
var fs = require('fs');
var flatten = require('flat');
module.exports = function(source) {
var regexp = new RegExp('(jsvars\\(([^)]*)\\))');
var match = source.match(regexp);
if (match && match[2]){
// has to mark as uncacheable if using jsvars so that the new file is fetched
this.cacheable(false);
var file = path.resolve(match[2]);
this.addDependency(file);
var contents = this.exec(fs.readFileSync(file).toString(), file);
var res = flatten(contents, {delimiter: '-'});
var final = Object.keys(res).map(function(key){
return '$' + key + ': ' + res[key] + ';';
})
.join('\n');
source = source.replace(match[1], final);
}
else{
this.cacheable();
}
return source;
};