-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.preprocessor.js
More file actions
42 lines (41 loc) · 1.34 KB
/
Copy pathjest.preprocessor.js
File metadata and controls
42 lines (41 loc) · 1.34 KB
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
/*
* CSS|SASS preprocessor
* Converts the CSS to a hashmap containing the selectors
* Used to simulate css-loader in Webpack
*/
const cssjson = require('cssjson');
const sass = require('node-sass');
module.exports = {
getSelectors(rules) {
let selectors = {};
Object.keys(rules).forEach(rule => {
if(rule.charAt(0) !== '@') {
// Remove the first . or # (if there is one)
let selectorName = rule.replace(/^[.#]/, '');
// Remove any :pseudo
selectorName = selectorName.replace(/:[A-Za-z0-9()-]+/g, '');
// No spaces for you
selectorName = selectorName.replace(/\s+/g, '');
// Remove any [ ] attrs
selectorName = selectorName.replace(/\[[^\]]+\]/, '');
// Split any selectors up
let selectorTree = selectorName.split(/[^A-Za-z0-9_-]/);
// Add them all into the selector list
selectorTree.forEach(sT => selectors[sT] = sT);
}
if(rules[rule].children) {
Object.assign(selectors, this.getSelectors(rules[rule].children));
}
});
return selectors;
},
process(src, path) {
let stylesObject = {};
if(/\.s?css$/i.test(path)) {
let result = sass.renderSync({file: path});
let parsed = cssjson.toJSON(result.css.toString('utf8'));
stylesObject = this.getSelectors(parsed.children);
}
return 'module.exports = ' + JSON.stringify(stylesObject);
}
};