-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
46 lines (40 loc) · 1.31 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
const HtmlWebpackPlugin = require("html-webpack-plugin");
class NetlifyPushWebpackPlugin {
constructor(options) {
this.options = options;
}
buildHeaders({ js, css }) {
const headers = this.options.headers || [];
const include = this.options.include || "all";
const scripts = js.map((f) => ` Link: <${f}>; rel=preload; as=script`);
const styles = css.map((f) => ` Link: <${f}>; rel=preload; as=style`);
if (include === "all")
return ["/*", ...scripts, ...styles, ...headers].join("\n");
if (include === "js")
return ["/*", ...scripts, ...headers].join("\n");
if (include === "css")
return ["/*", ...styles, ...headers].join("\n");
return "";
}
apply(compiler) {
compiler.hooks.compilation.tap(
"NetlifyPushWebpackPlugin",
(compilation) => {
HtmlWebpackPlugin.getHooks(
compilation
).beforeAssetTagGeneration.tapAsync(
"NetlifyPushWebpackPlugin",
(data, cb) => {
const filedata = this.buildHeaders(data.assets, this.options);
compilation.assets[`${this.options.filename}`] = {
source: () => filedata,
size: () => filedata.length,
};
cb(null, data);
}
);
}
);
}
}
module.exports = NetlifyPushWebpackPlugin;