-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstyleguide-webpack-plugin.js
67 lines (57 loc) · 1.76 KB
/
styleguide-webpack-plugin.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
let kss
try {
kss = require('kss');
} catch (err) {
kss = require(__dirname + '/../front-end-starter-kit/node_modules/kss');
}
function StyleguideWebpackPlugin (options) {
if (!options) {
throw 'kss webpack plugin: options is not defined. should be an object with at least "source"';
}
if (!options.source) {
throw 'kss webpack plugin: source is not defined';
}
this.options = options;
this.webpackCss = [];
this.webpackJs = [];
}
StyleguideWebpackPlugin.prototype.apply = function (compiler) {
const self = this;
compiler.plugin('after-emit', function (compilation, callback) {
// Reset the asset arrays.
self.webpackCss = [];
self.webpackJs = [];
for (const filename in compilation.assets) {
const assetPath = '../' + filename;
// TODO find best way to determine order of assets from webpack.
if (/\.js$/.test(filename)) {
// JS file.
if (!self.webpackJs.includes(assetPath)) {
if (/hot-update\.js/.test(filename)) {
// Place hot-update last.
self.webpackJs.push(assetPath);
} else {
self.webpackJs.unshift(assetPath);
}
}
} else if (/\.css$/.test(filename)) {
// CSS file.
if (!self.webpackCss.includes(assetPath)) {
self.webpackCss.unshift(assetPath);
}
}
}
callback();
});
compiler.plugin('done', function () {
self.options.css = self.options.css || [];
self.options.js = self.options.js || [];
// Add the webpack assets.
const kssOptions = Object.assign({}, self.options, {
css: self.options.css.concat(self.webpackCss),
js: self.options.js.concat(self.webpackJs)
});
kss(kssOptions);
});
};
module.exports = StyleguideWebpackPlugin;