-
Notifications
You must be signed in to change notification settings - Fork 1
/
inline-plugin.mjs
52 lines (50 loc) · 1.59 KB
/
inline-plugin.mjs
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
import HtmlWebpackPlugin from "html-webpack-plugin";
export default class InlinePlugin {
apply(compiler) {
compiler.hooks.compilation.tap("inline-plugin", (compilation) => {
const hooks = HtmlWebpackPlugin.getHooks(compilation);
hooks.alterAssetTagGroups.tap("inline-plugin", (assets) => {
const inline = (tag) => {
if (
tag.tagName === "script" &&
tag.attributes &&
tag.attributes.src
) {
const key = tag.attributes.src.replace(/^\/+/g, "");
const asset = compilation.assets[key];
if (asset) {
delete compilation.assets[key];
return {
tagName: "script",
innerHTML: asset.source(),
voidTag: false,
};
}
} else if (
tag.tagName === "link" &&
tag.attributes &&
tag.attributes.rel === "stylesheet" &&
tag.attributes.href
) {
const key = tag.attributes.href.replace(/^\/+/g, "");
const asset = compilation.assets[key];
if (asset) {
delete compilation.assets[key];
return {
tagName: "style",
attributes: {
type: "text/css",
},
innerHTML: asset.source(),
voidTag: false,
};
}
}
return tag;
};
assets.headTags = assets.headTags.map(inline);
assets.bodyTags = assets.bodyTags.map(inline);
});
});
}
}