@serwist/webpack-plugin 8.4.3 → 9.0.0-preview.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunks/relative-to-output-path.js +20 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.internal.d.ts +3 -1
- package/dist/index.internal.d.ts.map +1 -0
- package/dist/index.internal.js +2 -2
- package/dist/index.js +16 -22
- package/dist/inject-manifest.d.ts +7 -3
- package/dist/inject-manifest.d.ts.map +1 -0
- package/dist/lib/child-compilation-plugin.d.ts +1 -0
- package/dist/lib/child-compilation-plugin.d.ts.map +1 -0
- package/dist/lib/get-asset-hash.d.ts +1 -0
- package/dist/lib/get-asset-hash.d.ts.map +1 -0
- package/dist/lib/get-manifest-entries-from-compilation.d.ts +2 -1
- package/dist/lib/get-manifest-entries-from-compilation.d.ts.map +1 -0
- package/dist/lib/get-script-files-for-chunks.d.ts +2 -1
- package/dist/lib/get-script-files-for-chunks.d.ts.map +1 -0
- package/dist/lib/get-sourcemap-asset-name.d.ts +2 -1
- package/dist/lib/get-sourcemap-asset-name.d.ts.map +1 -0
- package/dist/lib/relative-to-output-path.d.ts +5 -4
- package/dist/lib/relative-to-output-path.d.ts.map +1 -0
- package/dist/lib/resolve-webpack-url.d.ts +2 -1
- package/dist/lib/resolve-webpack-url.d.ts.map +1 -0
- package/package.json +24 -29
- package/src/index.internal.ts +4 -0
- package/src/index.ts +11 -0
- package/src/inject-manifest.ts +287 -0
- package/src/lib/child-compilation-plugin.ts +66 -0
- package/src/lib/get-asset-hash.ts +27 -0
- package/src/lib/get-manifest-entries-from-compilation.ts +214 -0
- package/src/lib/get-script-files-for-chunks.ts +38 -0
- package/src/lib/get-sourcemap-asset-name.ts +47 -0
- package/src/lib/relative-to-output-path.ts +29 -0
- package/src/lib/resolve-webpack-url.ts +27 -0
- package/dist/index.cjs +0 -579
- package/dist/index.d.cts +0 -0
- package/dist/index.internal.cjs +0 -54
- package/dist/index.internal.d.cts +0 -2
- package/dist/inject-manifest.d.cts +0 -0
- package/dist/lib/child-compilation-plugin.d.cts +0 -20
- package/dist/lib/get-asset-hash.d.cts +0 -8
- package/dist/lib/get-manifest-entries-from-compilation.d.cts +0 -6
- package/dist/lib/get-script-files-for-chunks.d.cts +0 -0
- package/dist/lib/get-sourcemap-asset-name.d.cts +0 -0
- package/dist/lib/relative-to-output-path.d.cts +0 -11
- package/dist/lib/resolve-webpack-url.d.cts +0 -12
- package/dist/relative-to-output-path.cjs +0 -22
- package/dist/relative-to-output-path.js +0 -20
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
import type { WebpackInjectManifestOptions } from "@serwist/build";
|
|
9
|
+
import { escapeRegExp, replaceAndUpdateSourceMap, validateWebpackInjectManifestOptions } from "@serwist/build";
|
|
10
|
+
import stringify from "fast-json-stable-stringify";
|
|
11
|
+
import prettyBytes from "pretty-bytes";
|
|
12
|
+
import upath from "upath";
|
|
13
|
+
import type { Compilation } from "webpack";
|
|
14
|
+
import webpack from "webpack";
|
|
15
|
+
|
|
16
|
+
import { getManifestEntriesFromCompilation } from "./lib/get-manifest-entries-from-compilation.js";
|
|
17
|
+
import { getSourcemapAssetName } from "./lib/get-sourcemap-asset-name.js";
|
|
18
|
+
import { relativeToOutputPath } from "./lib/relative-to-output-path.js";
|
|
19
|
+
|
|
20
|
+
// Used to keep track of swDest files written by *any* instance of this plugin.
|
|
21
|
+
// See https://github.com/GoogleChrome/workbox/issues/2181
|
|
22
|
+
const _generatedAssetNames = new Set<string>();
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* This class supports compiling a service worker file provided via `swSrc`,
|
|
26
|
+
* and injecting into that service worker a list of URLs and revision
|
|
27
|
+
* information for precaching based on the webpack asset pipeline.
|
|
28
|
+
*
|
|
29
|
+
* Use an instance of `InjectManifest` in the
|
|
30
|
+
* [`plugins` array](https://webpack.js.org/concepts/plugins/#usage) of a
|
|
31
|
+
* webpack config.
|
|
32
|
+
*
|
|
33
|
+
* In addition to injecting the manifest, this plugin will perform a compilation
|
|
34
|
+
* of the `swSrc` file, using the options from the main webpack configuration.
|
|
35
|
+
*
|
|
36
|
+
* ```
|
|
37
|
+
* // The following lists some common options; see the rest of the documentation
|
|
38
|
+
* // for the full set of options and defaults.
|
|
39
|
+
* new InjectManifest({
|
|
40
|
+
* exclude: [/.../, '...'],
|
|
41
|
+
* maximumFileSizeToCacheInBytes: ...,
|
|
42
|
+
* swSrc: '...',
|
|
43
|
+
* });
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export class InjectManifest {
|
|
47
|
+
protected config: WebpackInjectManifestOptions;
|
|
48
|
+
private alreadyCalled: boolean;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Creates an instance of InjectManifest.
|
|
52
|
+
*/
|
|
53
|
+
constructor(config: WebpackInjectManifestOptions) {
|
|
54
|
+
this.config = config;
|
|
55
|
+
this.alreadyCalled = false;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @param compiler default compiler object passed from webpack
|
|
60
|
+
*
|
|
61
|
+
* @private
|
|
62
|
+
*/
|
|
63
|
+
propagateWebpackConfig(compiler: webpack.Compiler): void {
|
|
64
|
+
const parsedSwSrc = upath.parse(this.config.swSrc);
|
|
65
|
+
// Because this.config is listed last, properties that are already set
|
|
66
|
+
// there take precedence over derived properties from the compiler.
|
|
67
|
+
this.config = Object.assign(
|
|
68
|
+
{
|
|
69
|
+
mode: compiler.options.mode,
|
|
70
|
+
// Use swSrc with a hardcoded .js extension, in case swSrc is a .ts file.
|
|
71
|
+
swDest: `${parsedSwSrc.name}.js`,
|
|
72
|
+
},
|
|
73
|
+
this.config,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* `getManifestEntriesFromCompilation` with a few additional checks.
|
|
79
|
+
*
|
|
80
|
+
* @private
|
|
81
|
+
*/
|
|
82
|
+
async getManifestEntries(compilation: webpack.Compilation, config: WebpackInjectManifestOptions) {
|
|
83
|
+
if (config.disablePrecacheManifest) {
|
|
84
|
+
return {
|
|
85
|
+
size: 0,
|
|
86
|
+
sortedEntries: undefined,
|
|
87
|
+
manifestString: "undefined",
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// See https://github.com/GoogleChrome/workbox/issues/1790
|
|
92
|
+
if (this.alreadyCalled) {
|
|
93
|
+
const warningMessage = `${this.constructor.name} has been called multiple times, perhaps due to running webpack in --watch mode. The precache manifest generated after the first call may be inaccurate! Please see https://github.com/GoogleChrome/workbox/issues/1790 for more information.`;
|
|
94
|
+
|
|
95
|
+
if (!compilation.warnings.some((warning) => warning instanceof Error && warning.message === warningMessage)) {
|
|
96
|
+
compilation.warnings.push(new Error(warningMessage) as webpack.WebpackError);
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
this.alreadyCalled = true;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Ensure that we don't precache any of the assets generated by *any*
|
|
103
|
+
// instance of this plugin.
|
|
104
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
105
|
+
config.exclude!.push(({ asset }) => _generatedAssetNames.has(asset.name));
|
|
106
|
+
|
|
107
|
+
const { size, sortedEntries } = await getManifestEntriesFromCompilation(compilation, config);
|
|
108
|
+
|
|
109
|
+
let manifestString = stringify(sortedEntries);
|
|
110
|
+
if (
|
|
111
|
+
this.config.compileSrc &&
|
|
112
|
+
// See https://github.com/GoogleChrome/workbox/issues/2729
|
|
113
|
+
!(compilation.options?.devtool === "eval-cheap-source-map" && compilation.options.optimization?.minimize)
|
|
114
|
+
) {
|
|
115
|
+
// See https://github.com/GoogleChrome/workbox/issues/2263
|
|
116
|
+
manifestString = manifestString.replace(/"/g, `'`);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return { size, sortedEntries, manifestString };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* @param compiler default compiler object passed from webpack
|
|
124
|
+
*
|
|
125
|
+
* @private
|
|
126
|
+
*/
|
|
127
|
+
apply(compiler: webpack.Compiler): void {
|
|
128
|
+
this.propagateWebpackConfig(compiler);
|
|
129
|
+
|
|
130
|
+
compiler.hooks.make.tapPromise(this.constructor.name, (compilation) =>
|
|
131
|
+
this.handleMake(compilation, compiler).catch((error: webpack.WebpackError) => {
|
|
132
|
+
compilation.errors.push(error);
|
|
133
|
+
}),
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
const { PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER } = webpack.Compilation;
|
|
137
|
+
// Specifically hook into thisCompilation, as per
|
|
138
|
+
// https://github.com/webpack/webpack/issues/11425#issuecomment-690547848
|
|
139
|
+
compiler.hooks.thisCompilation.tap(this.constructor.name, (compilation) => {
|
|
140
|
+
compilation.hooks.processAssets.tapPromise(
|
|
141
|
+
{
|
|
142
|
+
name: this.constructor.name,
|
|
143
|
+
// TODO(jeffposnick): This may need to change eventually.
|
|
144
|
+
// See https://github.com/webpack/webpack/issues/11822#issuecomment-726184972
|
|
145
|
+
stage: PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER - 10,
|
|
146
|
+
},
|
|
147
|
+
() =>
|
|
148
|
+
this.addAssets(compilation).catch((error: webpack.WebpackError) => {
|
|
149
|
+
compilation.errors.push(error);
|
|
150
|
+
}),
|
|
151
|
+
);
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* @param compilation The webpack compilation.
|
|
157
|
+
* @param parentCompiler The webpack parent compiler.
|
|
158
|
+
*
|
|
159
|
+
* @private
|
|
160
|
+
*/
|
|
161
|
+
async performChildCompilation(compilation: webpack.Compilation, parentCompiler: webpack.Compiler): Promise<void> {
|
|
162
|
+
const outputOptions: Parameters<Compilation["createChildCompiler"]>["1"] = {
|
|
163
|
+
filename: this.config.swDest,
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const childCompiler = compilation.createChildCompiler(this.constructor.name, outputOptions, []);
|
|
167
|
+
|
|
168
|
+
childCompiler.context = parentCompiler.context;
|
|
169
|
+
childCompiler.inputFileSystem = parentCompiler.inputFileSystem;
|
|
170
|
+
childCompiler.outputFileSystem = parentCompiler.outputFileSystem;
|
|
171
|
+
|
|
172
|
+
if (Array.isArray(this.config.webpackCompilationPlugins)) {
|
|
173
|
+
for (const plugin of this.config.webpackCompilationPlugins) {
|
|
174
|
+
plugin.apply(childCompiler);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
new webpack.EntryPlugin(parentCompiler.context, this.config.swSrc, this.constructor.name).apply(childCompiler);
|
|
179
|
+
|
|
180
|
+
await new Promise<void>((resolve, reject) => {
|
|
181
|
+
childCompiler.runAsChild((error, _entries, childCompilation) => {
|
|
182
|
+
if (error) {
|
|
183
|
+
reject(error);
|
|
184
|
+
} else {
|
|
185
|
+
compilation.warnings = compilation.warnings.concat(childCompilation?.warnings ?? []);
|
|
186
|
+
compilation.errors = compilation.errors.concat(childCompilation?.errors ?? []);
|
|
187
|
+
|
|
188
|
+
resolve();
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* @param compilation The webpack compilation.
|
|
196
|
+
* @param parentCompiler The webpack parent compiler.
|
|
197
|
+
*
|
|
198
|
+
* @private
|
|
199
|
+
*/
|
|
200
|
+
addSrcToAssets(compilation: webpack.Compilation, parentCompiler: webpack.Compiler): void {
|
|
201
|
+
// eslint-disable-next-line
|
|
202
|
+
const source = (parentCompiler.inputFileSystem as any).readFileSync(this.config.swSrc);
|
|
203
|
+
compilation.emitAsset(this.config.swDest!, new webpack.sources.RawSource(source));
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* @param compilation The webpack compilation.
|
|
208
|
+
* @param parentCompiler The webpack parent compiler.
|
|
209
|
+
*
|
|
210
|
+
* @private
|
|
211
|
+
*/
|
|
212
|
+
async handleMake(compilation: webpack.Compilation, parentCompiler: webpack.Compiler): Promise<void> {
|
|
213
|
+
this.config = await validateWebpackInjectManifestOptions(this.config);
|
|
214
|
+
this.config.swDest = relativeToOutputPath(compilation, this.config.swDest!);
|
|
215
|
+
_generatedAssetNames.add(this.config.swDest);
|
|
216
|
+
|
|
217
|
+
if (this.config.compileSrc) {
|
|
218
|
+
await this.performChildCompilation(compilation, parentCompiler);
|
|
219
|
+
} else {
|
|
220
|
+
this.addSrcToAssets(compilation, parentCompiler);
|
|
221
|
+
// This used to be a fatal error, but just warn at runtime because we
|
|
222
|
+
// can't validate it easily.
|
|
223
|
+
if (Array.isArray(this.config.webpackCompilationPlugins) && this.config.webpackCompilationPlugins.length > 0) {
|
|
224
|
+
compilation.warnings.push(
|
|
225
|
+
new Error("compileSrc is false, so the " + "webpackCompilationPlugins option will be ignored.") as webpack.WebpackError,
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* @param compilation The webpack compilation.
|
|
233
|
+
*
|
|
234
|
+
* @private
|
|
235
|
+
*/
|
|
236
|
+
async addAssets(compilation: webpack.Compilation): Promise<void> {
|
|
237
|
+
const config = Object.assign({}, this.config);
|
|
238
|
+
|
|
239
|
+
const { size, sortedEntries, manifestString } = await this.getManifestEntries(compilation, config);
|
|
240
|
+
|
|
241
|
+
// See https://webpack.js.org/contribute/plugin-patterns/#monitoring-the-watch-graph
|
|
242
|
+
const absoluteSwSrc = upath.resolve(config.swSrc);
|
|
243
|
+
compilation.fileDependencies.add(absoluteSwSrc);
|
|
244
|
+
|
|
245
|
+
const swAsset = compilation.getAsset(config.swDest!);
|
|
246
|
+
const swAssetString = swAsset!.source.source().toString();
|
|
247
|
+
|
|
248
|
+
const globalRegexp = new RegExp(escapeRegExp(config.injectionPoint!), "g");
|
|
249
|
+
const injectionResults = swAssetString.match(globalRegexp);
|
|
250
|
+
|
|
251
|
+
if (!injectionResults) {
|
|
252
|
+
throw new Error(`Can't find ${config.injectionPoint} in your SW source.`);
|
|
253
|
+
}
|
|
254
|
+
if (injectionResults.length !== 1) {
|
|
255
|
+
throw new Error(
|
|
256
|
+
`Multiple instances of ${config.injectionPoint} were found in your SW source. Include it only once. For more info, see https://github.com/GoogleChrome/workbox/issues/2681`,
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const sourcemapAssetName = getSourcemapAssetName(compilation, swAssetString, config.swDest!);
|
|
261
|
+
|
|
262
|
+
if (sourcemapAssetName) {
|
|
263
|
+
_generatedAssetNames.add(sourcemapAssetName);
|
|
264
|
+
const sourcemapAsset = compilation.getAsset(sourcemapAssetName);
|
|
265
|
+
const { source, map } = await replaceAndUpdateSourceMap({
|
|
266
|
+
jsFilename: config.swDest!,
|
|
267
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
268
|
+
originalMap: JSON.parse(sourcemapAsset!.source.source().toString()),
|
|
269
|
+
originalSource: swAssetString,
|
|
270
|
+
replaceString: manifestString,
|
|
271
|
+
searchString: config.injectionPoint!,
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
compilation.updateAsset(sourcemapAssetName, new webpack.sources.RawSource(map));
|
|
275
|
+
compilation.updateAsset(config.swDest!, new webpack.sources.RawSource(source));
|
|
276
|
+
} else {
|
|
277
|
+
// If there's no sourcemap associated with swDest, a simple string
|
|
278
|
+
// replacement will suffice.
|
|
279
|
+
compilation.updateAsset(config.swDest!, new webpack.sources.RawSource(swAssetString.replace(config.injectionPoint!, manifestString)));
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (compilation.getLogger) {
|
|
283
|
+
const logger = compilation.getLogger(this.constructor.name);
|
|
284
|
+
logger.info(`The service worker at ${config.swDest ?? ""} will precache ${sortedEntries?.length ?? 0} URLs, totaling ${prettyBytes(size)}.`);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { Compilation, WebpackPluginInstance } from "webpack";
|
|
2
|
+
import webpack from "webpack";
|
|
3
|
+
|
|
4
|
+
import { relativeToOutputPath } from "./relative-to-output-path.js";
|
|
5
|
+
|
|
6
|
+
export interface ChildCompilationPluginOptions {
|
|
7
|
+
src: string;
|
|
8
|
+
dest: string;
|
|
9
|
+
plugins?: WebpackPluginInstance[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Compile a file by creating a child of the hooked compiler.
|
|
14
|
+
*
|
|
15
|
+
* @private
|
|
16
|
+
*/
|
|
17
|
+
export class ChildCompilationPlugin implements WebpackPluginInstance {
|
|
18
|
+
src: string;
|
|
19
|
+
dest: string;
|
|
20
|
+
plugins: WebpackPluginInstance[] | undefined;
|
|
21
|
+
constructor({ src, dest, plugins }: ChildCompilationPluginOptions) {
|
|
22
|
+
this.src = src;
|
|
23
|
+
this.dest = dest;
|
|
24
|
+
this.plugins = plugins;
|
|
25
|
+
}
|
|
26
|
+
apply(compiler: webpack.Compiler) {
|
|
27
|
+
compiler.hooks.make.tapPromise(this.constructor.name, (compilation) =>
|
|
28
|
+
this.performChildCompilation(compilation, compiler).catch((error: webpack.WebpackError) => {
|
|
29
|
+
compilation.errors.push(error);
|
|
30
|
+
}),
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
async performChildCompilation(compilation: webpack.Compilation, parentCompiler: webpack.Compiler): Promise<void> {
|
|
34
|
+
const resolvedDest = relativeToOutputPath(compilation, this.dest);
|
|
35
|
+
const outputOptions: Parameters<Compilation["createChildCompiler"]>["1"] = {
|
|
36
|
+
filename: resolvedDest,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const childCompiler = compilation.createChildCompiler(this.constructor.name, outputOptions, []);
|
|
40
|
+
|
|
41
|
+
childCompiler.context = parentCompiler.context;
|
|
42
|
+
childCompiler.inputFileSystem = parentCompiler.inputFileSystem;
|
|
43
|
+
childCompiler.outputFileSystem = parentCompiler.outputFileSystem;
|
|
44
|
+
|
|
45
|
+
if (this.plugins !== undefined) {
|
|
46
|
+
for (const plugin of this.plugins) {
|
|
47
|
+
plugin?.apply(childCompiler);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
new webpack.EntryPlugin(parentCompiler.context, this.src, this.constructor.name).apply(childCompiler);
|
|
52
|
+
|
|
53
|
+
await new Promise<void>((resolve, reject) => {
|
|
54
|
+
childCompiler.runAsChild((error, _entries, childCompilation) => {
|
|
55
|
+
if (error) {
|
|
56
|
+
reject(error);
|
|
57
|
+
} else {
|
|
58
|
+
compilation.warnings = compilation.warnings.concat(childCompilation?.warnings ?? []);
|
|
59
|
+
compilation.errors = compilation.errors.concat(childCompilation?.errors ?? []);
|
|
60
|
+
|
|
61
|
+
resolve();
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import crypto from "crypto";
|
|
10
|
+
import type { Asset } from "webpack";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param asset
|
|
14
|
+
* @returns The MD5 hash of the asset's source.
|
|
15
|
+
*
|
|
16
|
+
* @private
|
|
17
|
+
*/
|
|
18
|
+
export const getAssetHash = (asset: Asset): string | null => {
|
|
19
|
+
// If webpack has the asset marked as immutable, then we don't need to
|
|
20
|
+
// use an out-of-band revision for it.
|
|
21
|
+
// See https://github.com/webpack/webpack/issues/9038
|
|
22
|
+
if (asset.info?.immutable) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return crypto.createHash("md5").update(Buffer.from(asset.source.source())).digest("hex");
|
|
27
|
+
};
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { FileDetails, ManifestEntry, WebpackInjectManifestOptions } from "@serwist/build";
|
|
10
|
+
import { transformManifest } from "@serwist/build";
|
|
11
|
+
import type { Asset, Chunk, Compilation, WebpackError } from "webpack";
|
|
12
|
+
import webpack from "webpack";
|
|
13
|
+
|
|
14
|
+
import { getAssetHash } from "./get-asset-hash.js";
|
|
15
|
+
import { resolveWebpackURL } from "./resolve-webpack-url.js";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* For a given asset, checks whether at least one of the conditions matches.
|
|
19
|
+
*
|
|
20
|
+
* @param asset The webpack asset in question. This will be passed
|
|
21
|
+
* to any functions that are listed as conditions.
|
|
22
|
+
* @param compilation The webpack compilation. This will be passed
|
|
23
|
+
* to any functions that are listed as conditions.
|
|
24
|
+
* @param conditions
|
|
25
|
+
* @returns Whether or not at least one condition matches.
|
|
26
|
+
* @private
|
|
27
|
+
*/
|
|
28
|
+
const checkConditions = (
|
|
29
|
+
asset: Asset,
|
|
30
|
+
compilation: Compilation,
|
|
31
|
+
|
|
32
|
+
conditions: Array<string | RegExp | ((arg0: any) => boolean)> = [],
|
|
33
|
+
): boolean => {
|
|
34
|
+
for (const condition of conditions) {
|
|
35
|
+
if (typeof condition === "function") {
|
|
36
|
+
return condition({ asset, compilation });
|
|
37
|
+
//return compilation !== null;
|
|
38
|
+
}
|
|
39
|
+
if (webpack.ModuleFilenameHelpers.matchPart(asset.name, condition)) {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// We'll only get here if none of the conditions applied.
|
|
45
|
+
return false;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Returns the names of all the assets in all the chunks in a chunk group,
|
|
50
|
+
* if provided a chunk group name.
|
|
51
|
+
* Otherwise, if provided a chunk name, return all the assets in that chunk.
|
|
52
|
+
* Otherwise, if there isn't a chunk group or chunk with that name, return null.
|
|
53
|
+
*
|
|
54
|
+
* @param compilation
|
|
55
|
+
* @param chunkOrGroup
|
|
56
|
+
* @returns
|
|
57
|
+
* @private
|
|
58
|
+
*/
|
|
59
|
+
const getNamesOfAssetsInChunkOrGroup = (compilation: Compilation, chunkOrGroup: string): string[] | null => {
|
|
60
|
+
const chunkGroup = compilation.namedChunkGroups?.get(chunkOrGroup);
|
|
61
|
+
if (chunkGroup) {
|
|
62
|
+
const assetNames = [];
|
|
63
|
+
for (const chunk of chunkGroup.chunks) {
|
|
64
|
+
assetNames.push(...getNamesOfAssetsInChunk(chunk));
|
|
65
|
+
}
|
|
66
|
+
return assetNames;
|
|
67
|
+
}
|
|
68
|
+
const chunk = compilation.namedChunks?.get(chunkOrGroup);
|
|
69
|
+
if (chunk) {
|
|
70
|
+
return getNamesOfAssetsInChunk(chunk);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// If we get here, there's no chunkGroup or chunk with that name.
|
|
74
|
+
return null;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Returns the names of all the assets in a chunk.
|
|
79
|
+
*
|
|
80
|
+
* @param chunk
|
|
81
|
+
* @returns
|
|
82
|
+
* @private
|
|
83
|
+
*/
|
|
84
|
+
const getNamesOfAssetsInChunk = (chunk: Chunk): string[] => {
|
|
85
|
+
const assetNames: string[] = [];
|
|
86
|
+
|
|
87
|
+
assetNames.push(...chunk.files);
|
|
88
|
+
|
|
89
|
+
// This only appears to be set in webpack v5.
|
|
90
|
+
if (chunk.auxiliaryFiles) {
|
|
91
|
+
assetNames.push(...chunk.auxiliaryFiles);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return assetNames;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Filters the set of assets out, based on the configuration options provided:
|
|
99
|
+
* - chunks and excludeChunks, for chunkName-based criteria.
|
|
100
|
+
* - include and exclude, for more general criteria.
|
|
101
|
+
*
|
|
102
|
+
* @param compilation The webpack compilation.
|
|
103
|
+
* @param config The validated configuration, obtained from the plugin.
|
|
104
|
+
* @returns The assets that should be included in the manifest,
|
|
105
|
+
* based on the criteria provided.
|
|
106
|
+
* @private
|
|
107
|
+
*/
|
|
108
|
+
const filterAssets = (compilation: Compilation, config: WebpackInjectManifestOptions): Set<Asset> => {
|
|
109
|
+
const filteredAssets = new Set<Asset>();
|
|
110
|
+
const assets = compilation.getAssets();
|
|
111
|
+
|
|
112
|
+
const allowedAssetNames = new Set<string>();
|
|
113
|
+
// See https://github.com/GoogleChrome/workbox/issues/1287
|
|
114
|
+
if (Array.isArray(config.chunks)) {
|
|
115
|
+
for (const name of config.chunks) {
|
|
116
|
+
// See https://github.com/GoogleChrome/workbox/issues/2717
|
|
117
|
+
const assetsInChunkOrGroup = getNamesOfAssetsInChunkOrGroup(compilation, name);
|
|
118
|
+
if (assetsInChunkOrGroup) {
|
|
119
|
+
for (const assetName of assetsInChunkOrGroup) {
|
|
120
|
+
allowedAssetNames.add(assetName);
|
|
121
|
+
}
|
|
122
|
+
} else {
|
|
123
|
+
compilation.warnings.push(
|
|
124
|
+
new Error(`The chunk '${name}' was provided in your Serwist chunks config, but was not found in the compilation.`) as WebpackError,
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const deniedAssetNames = new Set();
|
|
131
|
+
if (Array.isArray(config.excludeChunks)) {
|
|
132
|
+
for (const name of config.excludeChunks) {
|
|
133
|
+
// See https://github.com/GoogleChrome/workbox/issues/2717
|
|
134
|
+
const assetsInChunkOrGroup = getNamesOfAssetsInChunkOrGroup(compilation, name);
|
|
135
|
+
if (assetsInChunkOrGroup) {
|
|
136
|
+
for (const assetName of assetsInChunkOrGroup) {
|
|
137
|
+
deniedAssetNames.add(assetName);
|
|
138
|
+
}
|
|
139
|
+
} // Don't warn if the chunk group isn't found.
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
for (const asset of assets) {
|
|
144
|
+
// chunk based filtering is funky because:
|
|
145
|
+
// - Each asset might belong to one or more chunks.
|
|
146
|
+
// - If *any* of those chunk names match our config.excludeChunks,
|
|
147
|
+
// then we skip that asset.
|
|
148
|
+
// - If the config.chunks is defined *and* there's no match
|
|
149
|
+
// between at least one of the chunkNames and one entry, then
|
|
150
|
+
// we skip that assets as well.
|
|
151
|
+
|
|
152
|
+
if (deniedAssetNames.has(asset.name)) {
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (Array.isArray(config.chunks) && !allowedAssetNames.has(asset.name)) {
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Next, check asset-level checks via includes/excludes:
|
|
161
|
+
const isExcluded = checkConditions(asset, compilation, config.exclude);
|
|
162
|
+
if (isExcluded) {
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Treat an empty config.includes as an implicit inclusion.
|
|
167
|
+
const isIncluded = !Array.isArray(config.include) || checkConditions(asset, compilation, config.include);
|
|
168
|
+
if (!isIncluded) {
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// If we've gotten this far, then add the asset.
|
|
173
|
+
filteredAssets.add(asset);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return filteredAssets;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
export const getManifestEntriesFromCompilation = async (
|
|
180
|
+
compilation: Compilation,
|
|
181
|
+
config: WebpackInjectManifestOptions,
|
|
182
|
+
): Promise<{ size: number; sortedEntries: ManifestEntry[] | undefined }> => {
|
|
183
|
+
const filteredAssets = filterAssets(compilation, config);
|
|
184
|
+
|
|
185
|
+
const { publicPath } = compilation.options.output;
|
|
186
|
+
|
|
187
|
+
const fileDetails = Array.from(filteredAssets).map((asset) => {
|
|
188
|
+
return {
|
|
189
|
+
file: resolveWebpackURL(publicPath as string, asset.name),
|
|
190
|
+
hash: getAssetHash(asset),
|
|
191
|
+
size: asset.source.size() || 0,
|
|
192
|
+
} as FileDetails;
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
const { manifestEntries, size, warnings } = await transformManifest({
|
|
196
|
+
fileDetails,
|
|
197
|
+
additionalPrecacheEntries: config.additionalPrecacheEntries,
|
|
198
|
+
dontCacheBustURLsMatching: config.dontCacheBustURLsMatching,
|
|
199
|
+
manifestTransforms: config.manifestTransforms,
|
|
200
|
+
maximumFileSizeToCacheInBytes: config.maximumFileSizeToCacheInBytes,
|
|
201
|
+
modifyURLPrefix: config.modifyURLPrefix,
|
|
202
|
+
transformParam: compilation,
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
// See https://github.com/GoogleChrome/workbox/issues/2790
|
|
206
|
+
for (const warning of warnings) {
|
|
207
|
+
compilation.warnings.push(new Error(warning) as WebpackError);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Ensure that the entries are properly sorted by URL.
|
|
211
|
+
const sortedEntries = manifestEntries?.sort((a, b) => (a.url === b.url ? 0 : a.url > b.url ? 1 : -1));
|
|
212
|
+
|
|
213
|
+
return { size, sortedEntries };
|
|
214
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import upath from "upath";
|
|
10
|
+
import type { Compilation, WebpackError } from "webpack";
|
|
11
|
+
|
|
12
|
+
import { resolveWebpackURL } from "./resolve-webpack-url.js";
|
|
13
|
+
|
|
14
|
+
export const getScriptFilesForChunks = (compilation: Compilation, chunkNames: string[]): string[] => {
|
|
15
|
+
const { chunks } = compilation.getStats().toJson({ chunks: true });
|
|
16
|
+
const { publicPath } = compilation.options.output;
|
|
17
|
+
const scriptFiles = new Set<string>();
|
|
18
|
+
|
|
19
|
+
for (const chunkName of chunkNames) {
|
|
20
|
+
const chunk = chunks!.find((chunk) => chunk.names?.includes(chunkName));
|
|
21
|
+
if (chunk) {
|
|
22
|
+
for (const file of chunk?.files ?? []) {
|
|
23
|
+
// See https://github.com/GoogleChrome/workbox/issues/2161
|
|
24
|
+
if (upath.extname(file) === ".js") {
|
|
25
|
+
scriptFiles.add(resolveWebpackURL(publicPath as string, file));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
} else {
|
|
29
|
+
compilation.warnings.push(new Error(`${chunkName} was provided to importScriptsViaChunks, but didn't match any named chunks.`) as WebpackError);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (scriptFiles.size === 0) {
|
|
34
|
+
compilation.warnings.push(new Error(`There were no assets matching importScriptsViaChunks: [${chunkNames.join(" ")}].`) as WebpackError);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return Array.from(scriptFiles);
|
|
38
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { getSourceMapURL } from "@serwist/build";
|
|
10
|
+
import upath from "upath";
|
|
11
|
+
import type { Compilation } from "webpack";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* If our bundled swDest file contains a sourcemap, we would invalidate that
|
|
15
|
+
* mapping if we just replaced injectionPoint with the stringified manifest.
|
|
16
|
+
* Instead, we need to update the swDest contents as well as the sourcemap
|
|
17
|
+
* at the same time.
|
|
18
|
+
*
|
|
19
|
+
* See https://github.com/GoogleChrome/workbox/issues/2235
|
|
20
|
+
*
|
|
21
|
+
* @param compilation The current webpack compilation.
|
|
22
|
+
* @param swContents The contents of the swSrc file, which may or
|
|
23
|
+
* may not include a valid sourcemap comment.
|
|
24
|
+
* @param swDest The configured swDest value.
|
|
25
|
+
* @returns If the swContents contains a valid sourcemap
|
|
26
|
+
* comment pointing to an asset present in the compilation, this will return the
|
|
27
|
+
* name of that asset. Otherwise, it will return undefined.
|
|
28
|
+
* @private
|
|
29
|
+
*/
|
|
30
|
+
export const getSourcemapAssetName = (compilation: Compilation, swContents: string, swDest: string): string | undefined => {
|
|
31
|
+
const url = getSourceMapURL(swContents);
|
|
32
|
+
if (url) {
|
|
33
|
+
// Translate the relative URL to what the presumed name for the webpack
|
|
34
|
+
// asset should be.
|
|
35
|
+
// This *might* not be a valid asset if the sourcemap URL that was found
|
|
36
|
+
// was added by another module incidentally.
|
|
37
|
+
// See https://github.com/GoogleChrome/workbox/issues/2250
|
|
38
|
+
const swAssetDirname = upath.dirname(swDest);
|
|
39
|
+
const sourcemapURLAssetName = upath.normalize(upath.join(swAssetDirname, url));
|
|
40
|
+
|
|
41
|
+
// Not sure if there's a better way to check for asset existence?
|
|
42
|
+
if (compilation.getAsset(sourcemapURLAssetName)) {
|
|
43
|
+
return sourcemapURLAssetName;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return undefined;
|
|
47
|
+
};
|