-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
/
webpack.config.js
222 lines (213 loc) · 5.59 KB
/
webpack.config.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const {
sources: { RawSource, OriginalSource, ReplaceSource },
Compilation,
util: { createHash },
optimize: { RealContentHashPlugin }
} = require("../../../../");
class VerifyAdditionalAssetsPlugin {
constructor(stage) {
this.stage = stage;
}
apply(compiler) {
compiler.hooks.compilation.tap(
"VerifyAdditionalAssetsPlugin",
compilation => {
const alreadySeenAssets = new Set();
compilation.hooks.processAssets.tap(
{
name: "VerifyAdditionalAssetsPlugin",
stage: this.stage,
additionalAssets: true
},
assets => {
for (const asset of Object.keys(assets)) {
expect(alreadySeenAssets).not.toContain(asset);
alreadySeenAssets.add(asset);
}
}
);
}
);
}
}
class HtmlPlugin {
constructor(entrypoints) {
this.entrypoints = entrypoints;
}
apply(compiler) {
compiler.hooks.compilation.tap("html-plugin", compilation => {
compilation.hooks.processAssets.tap(
{
name: "html-plugin",
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
},
() => {
const publicPath = compilation.outputOptions.publicPath;
const files = [];
for (const name of this.entrypoints) {
for (const file of compilation.entrypoints.get(name).getFiles())
files.push(file);
}
const toScriptTag = (file, extra) => {
const asset = compilation.getAsset(file);
const hash = createHash("sha512");
hash.update(asset.source.source());
const integrity = `sha512-${hash.digest("base64")}`;
compilation.updateAsset(
file,
x => x,
assetInfo => ({
...assetInfo,
contenthash: Array.isArray(assetInfo.contenthash)
? [...new Set([...assetInfo.contenthash, integrity])]
: assetInfo.contenthash
? [assetInfo.contenthash, integrity]
: integrity
})
);
return `<script src="${
publicPath === "auto" ? "" : publicPath
}${file}" integrity="${integrity}"></script>`;
};
compilation.emitAsset(
"index.html",
new OriginalSource(
`<html>
<body>
${files.map(file => ` ${toScriptTag(file)}`).join("\n")}
</body>
</html>`,
"index.html"
)
);
}
);
});
}
}
class HtmlInlinePlugin {
constructor(inline) {
this.inline = inline;
}
apply(compiler) {
compiler.hooks.compilation.tap("html-inline-plugin", compilation => {
compilation.hooks.processAssets.tap(
{
name: "html-inline-plugin",
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE,
additionalAssets: true
},
assets => {
const publicPath = compilation.outputOptions.publicPath;
for (const name of Object.keys(assets)) {
if (/\.html$/.test(name)) {
const asset = compilation.getAsset(name);
const content = asset.source.source();
const matches = [];
const regExp = /<script\s+src\s*=\s*"([^"]+)"(?:\s+[^"=\s]+(?:\s*=\s*(?:"[^"]*"|[^\s]+))?)*\s*><\/script>/g;
let match = regExp.exec(content);
while (match) {
let url = match[1];
if (url.startsWith(publicPath))
url = url.slice(publicPath.length);
if (this.inline.test(url)) {
const asset = compilation.getAsset(url);
matches.push({
start: match.index,
length: match[0].length,
asset
});
}
match = regExp.exec(content);
}
if (matches.length > 0) {
const newSource = new ReplaceSource(asset.source, name);
for (const { start, length, asset } of matches) {
newSource.replace(
start,
start + length - 1,
`<script>${asset.source.source()}</script>`
);
}
compilation.updateAsset(name, newSource);
}
}
}
}
);
});
}
}
class SriHashSupportPlugin {
apply(compiler) {
compiler.hooks.compilation.tap("sri-hash-support-plugin", compilation => {
RealContentHashPlugin.getCompilationHooks(compilation).updateHash.tap(
"sri-hash-support-plugin",
(input, oldHash) => {
if (/^sha512-.{88}$/.test(oldHash) && input.length === 1) {
const hash = createHash("sha512");
hash.update(input[0]);
return `sha512-${hash.digest("base64")}`;
}
}
);
});
}
}
class HtmlMinimizePlugin {
apply(compiler) {
compiler.hooks.compilation.tap("html-minimize-plugin", compilation => {
compilation.hooks.processAssets.tap(
{
name: "html-minimize-plugin",
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
additionalAssets: true
},
assets => {
for (const name of Object.keys(assets)) {
if (/\.html$/.test(name)) {
compilation.updateAsset(
name,
source => new RawSource(source.source().replace(/\s+/g, " ")),
assetInfo => ({
...assetInfo,
minimized: true
})
);
}
}
}
);
});
}
}
/** @type {import("../../../../").Configuration} */
module.exports = {
mode: "production",
entry: {
test: { import: "./index.js", filename: "test.js" },
inline: "./inline.js",
normal: "./normal.js"
},
output: {
filename: "[name]-[contenthash].js"
},
optimization: {
minimize: true,
minimizer: ["...", new HtmlMinimizePlugin()]
},
node: {
__dirname: false,
__filename: false
},
plugins: [
new VerifyAdditionalAssetsPlugin(
Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL - 1
),
// new VerifyAdditionalAssetsPlugin(Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE),
// new VerifyAdditionalAssetsPlugin(Compilation.PROCESS_ASSETS_STAGE_REPORT),
new HtmlPlugin(["inline", "normal"]),
new HtmlInlinePlugin(/inline/),
new SriHashSupportPlugin()
]
};