forked from lukeed/webpack-critical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
30 lines (25 loc) · 901 Bytes
/
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
const basename = require('path').basename;
const minifier = require('html-minifier').minify;
const critical = require('inline-critical');
const filterCSS = require('filter-css');
const context = process.cwd();
const ignore = ['@font-face', /import/, /url\(/];
class WebpackCritical {
constructor(opts) {
this.opts = Object.assign({ ignore, context }, opts);
}
apply(compiler) {
const opts = this.opts;
compiler.plugin('compilation', bundle => {
bundle.plugin('html-webpack-plugin-after-html-processing', (data, cb) => {
const file = opts.stylesheet || basename(data.assets.css[0]);
const source = bundle.assets[file].source();
const css = filterCSS(source, opts.ignore);
const result = critical(data.html, css, opts);
const html = minifier(result.toString(), data.plugin.options.minify);
cb(null, { html });
});
});
}
}
module.exports = WebpackCritical;