-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
54 lines (45 loc) · 1.51 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
const _ = require('lodash/fp');
const postCss = require('postcss');
const keyFramesParent = ['keyframes', '-webkit-keyframes', '-moz-keyframes'];
const postCssWrapperPlugin= postCss.plugin('postcss-wrapper-plugin', function(prefix) {
return function(css) {
css.walkRules(function(rule) {
let parentName = _.get('parent.name', rule);
if (_.includes(parentName, keyFramesParent))
return;
const selector = rule.selector;
rule.selector = _.pipe(_.split(','), _.map(_.pipe(_.trim, joinPrefix(prefix))),
_.join(', '))(selector);
});
};
});
const joinPrefix = function(prefix) {
return function(selector) {
if(selector === 'html') {
return selector+prefix;
}
return _.join(' ', [prefix, selector]);
};
};
function PostCssWrapper(file, container) {
this.file = file;
this.container = container;
}
PostCssWrapper.prototype.apply = function(compiler) {
const file = this.file;
const container = this.container;
compiler.plugin('emit', function(compilation, callback) {
const assets = compilation.assets;
if (!_.has(file, assets)) return callback();
const source = assets[file].source();
const processor = postCss([postCssWrapperPlugin(container)]);
processor.process(source).then(function(result) {
compilation.assets[file] = {
source: function() { return result.css; },
size: function() { return result.css.length; }
};
callback();
}, callback);
});
};
module.exports = PostCssWrapper;