@serwist/webpack-plugin 8.4.4 → 9.0.0-preview.1
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 +10 -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 +3 -7
- package/dist/index.js +29 -317
- 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
package/dist/index.cjs
DELETED
|
@@ -1,579 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var build = require('@serwist/build');
|
|
4
|
-
var stringify = require('fast-json-stable-stringify');
|
|
5
|
-
var upath = require('upath');
|
|
6
|
-
var webpack = require('webpack');
|
|
7
|
-
var crypto = require('crypto');
|
|
8
|
-
var relativeToOutputPath = require('./relative-to-output-path.cjs');
|
|
9
|
-
|
|
10
|
-
const BYTE_UNITS = [
|
|
11
|
-
'B',
|
|
12
|
-
'kB',
|
|
13
|
-
'MB',
|
|
14
|
-
'GB',
|
|
15
|
-
'TB',
|
|
16
|
-
'PB',
|
|
17
|
-
'EB',
|
|
18
|
-
'ZB',
|
|
19
|
-
'YB'
|
|
20
|
-
];
|
|
21
|
-
const BIBYTE_UNITS = [
|
|
22
|
-
'B',
|
|
23
|
-
'KiB',
|
|
24
|
-
'MiB',
|
|
25
|
-
'GiB',
|
|
26
|
-
'TiB',
|
|
27
|
-
'PiB',
|
|
28
|
-
'EiB',
|
|
29
|
-
'ZiB',
|
|
30
|
-
'YiB'
|
|
31
|
-
];
|
|
32
|
-
const BIT_UNITS = [
|
|
33
|
-
'b',
|
|
34
|
-
'kbit',
|
|
35
|
-
'Mbit',
|
|
36
|
-
'Gbit',
|
|
37
|
-
'Tbit',
|
|
38
|
-
'Pbit',
|
|
39
|
-
'Ebit',
|
|
40
|
-
'Zbit',
|
|
41
|
-
'Ybit'
|
|
42
|
-
];
|
|
43
|
-
const BIBIT_UNITS = [
|
|
44
|
-
'b',
|
|
45
|
-
'kibit',
|
|
46
|
-
'Mibit',
|
|
47
|
-
'Gibit',
|
|
48
|
-
'Tibit',
|
|
49
|
-
'Pibit',
|
|
50
|
-
'Eibit',
|
|
51
|
-
'Zibit',
|
|
52
|
-
'Yibit'
|
|
53
|
-
];
|
|
54
|
-
/*
|
|
55
|
-
Formats the given number using `Number#toLocaleString`.
|
|
56
|
-
- If locale is a string, the value is expected to be a locale-key (for example: `de`).
|
|
57
|
-
- If locale is true, the system default locale is used for translation.
|
|
58
|
-
- If no value for locale is specified, the number is returned unmodified.
|
|
59
|
-
*/ const toLocaleString = (number, locale, options)=>{
|
|
60
|
-
let result = number;
|
|
61
|
-
if (typeof locale === 'string' || Array.isArray(locale)) {
|
|
62
|
-
result = number.toLocaleString(locale, options);
|
|
63
|
-
} else if (locale === true || options !== undefined) {
|
|
64
|
-
result = number.toLocaleString(undefined, options);
|
|
65
|
-
}
|
|
66
|
-
return result;
|
|
67
|
-
};
|
|
68
|
-
function prettyBytes(number, options) {
|
|
69
|
-
if (!Number.isFinite(number)) {
|
|
70
|
-
throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`);
|
|
71
|
-
}
|
|
72
|
-
options = {
|
|
73
|
-
bits: false,
|
|
74
|
-
binary: false,
|
|
75
|
-
space: true,
|
|
76
|
-
...options
|
|
77
|
-
};
|
|
78
|
-
const UNITS = options.bits ? options.binary ? BIBIT_UNITS : BIT_UNITS : options.binary ? BIBYTE_UNITS : BYTE_UNITS;
|
|
79
|
-
const separator = options.space ? ' ' : '';
|
|
80
|
-
if (options.signed && number === 0) {
|
|
81
|
-
return ` 0${separator}${UNITS[0]}`;
|
|
82
|
-
}
|
|
83
|
-
const isNegative = number < 0;
|
|
84
|
-
const prefix = isNegative ? '-' : options.signed ? '+' : '';
|
|
85
|
-
if (isNegative) {
|
|
86
|
-
number = -number;
|
|
87
|
-
}
|
|
88
|
-
let localeOptions;
|
|
89
|
-
if (options.minimumFractionDigits !== undefined) {
|
|
90
|
-
localeOptions = {
|
|
91
|
-
minimumFractionDigits: options.minimumFractionDigits
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
if (options.maximumFractionDigits !== undefined) {
|
|
95
|
-
localeOptions = {
|
|
96
|
-
maximumFractionDigits: options.maximumFractionDigits,
|
|
97
|
-
...localeOptions
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
if (number < 1) {
|
|
101
|
-
const numberString = toLocaleString(number, options.locale, localeOptions);
|
|
102
|
-
return prefix + numberString + separator + UNITS[0];
|
|
103
|
-
}
|
|
104
|
-
const exponent = Math.min(Math.floor(options.binary ? Math.log(number) / Math.log(1024) : Math.log10(number) / 3), UNITS.length - 1);
|
|
105
|
-
number /= (options.binary ? 1024 : 1000) ** exponent;
|
|
106
|
-
if (!localeOptions) {
|
|
107
|
-
number = number.toPrecision(3);
|
|
108
|
-
}
|
|
109
|
-
const numberString = toLocaleString(Number(number), options.locale, localeOptions);
|
|
110
|
-
const unit = UNITS[exponent];
|
|
111
|
-
return prefix + numberString + separator + unit;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* @param asset
|
|
116
|
-
* @returns The MD5 hash of the asset's source.
|
|
117
|
-
*
|
|
118
|
-
* @private
|
|
119
|
-
*/ const getAssetHash = (asset)=>{
|
|
120
|
-
// If webpack has the asset marked as immutable, then we don't need to
|
|
121
|
-
// use an out-of-band revision for it.
|
|
122
|
-
// See https://github.com/webpack/webpack/issues/9038
|
|
123
|
-
if (asset.info?.immutable) {
|
|
124
|
-
return null;
|
|
125
|
-
}
|
|
126
|
-
return crypto.createHash("md5").update(Buffer.from(asset.source.source())).digest("hex");
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
/*
|
|
130
|
-
Copyright 2018 Google LLC
|
|
131
|
-
|
|
132
|
-
Use of this source code is governed by an MIT-style
|
|
133
|
-
license that can be found in the LICENSE file or at
|
|
134
|
-
https://opensource.org/licenses/MIT.
|
|
135
|
-
*/ /**
|
|
136
|
-
* Resolves a url in the way that webpack would (with string concatenation)
|
|
137
|
-
*
|
|
138
|
-
* Use publicPath + filePath instead of url.resolve(publicPath, filePath) see:
|
|
139
|
-
* https://webpack.js.org/configuration/output/#output-publicpath
|
|
140
|
-
*
|
|
141
|
-
* @param publicPath The publicPath value from webpack's compilation.
|
|
142
|
-
* @param paths File paths to join
|
|
143
|
-
* @returns Joined file path
|
|
144
|
-
* @private
|
|
145
|
-
*/ function resolveWebpackURL(publicPath, ...paths) {
|
|
146
|
-
// This is a change in webpack v5.
|
|
147
|
-
// See https://github.com/jantimon/html-webpack-plugin/pull/1516
|
|
148
|
-
if (publicPath === "auto") {
|
|
149
|
-
return paths.join("");
|
|
150
|
-
}
|
|
151
|
-
return [
|
|
152
|
-
publicPath,
|
|
153
|
-
...paths
|
|
154
|
-
].join("");
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* For a given asset, checks whether at least one of the conditions matches.
|
|
159
|
-
*
|
|
160
|
-
* @param asset The webpack asset in question. This will be passed
|
|
161
|
-
* to any functions that are listed as conditions.
|
|
162
|
-
* @param compilation The webpack compilation. This will be passed
|
|
163
|
-
* to any functions that are listed as conditions.
|
|
164
|
-
* @param conditions
|
|
165
|
-
* @returns Whether or not at least one condition matches.
|
|
166
|
-
* @private
|
|
167
|
-
*/ function checkConditions(asset, compilation, conditions = []) {
|
|
168
|
-
for (const condition of conditions){
|
|
169
|
-
if (typeof condition === "function") {
|
|
170
|
-
return condition({
|
|
171
|
-
asset,
|
|
172
|
-
compilation
|
|
173
|
-
});
|
|
174
|
-
//return compilation !== null;
|
|
175
|
-
}
|
|
176
|
-
if (webpack.ModuleFilenameHelpers.matchPart(asset.name, condition)) {
|
|
177
|
-
return true;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
// We'll only get here if none of the conditions applied.
|
|
181
|
-
return false;
|
|
182
|
-
}
|
|
183
|
-
/**
|
|
184
|
-
* Returns the names of all the assets in all the chunks in a chunk group,
|
|
185
|
-
* if provided a chunk group name.
|
|
186
|
-
* Otherwise, if provided a chunk name, return all the assets in that chunk.
|
|
187
|
-
* Otherwise, if there isn't a chunk group or chunk with that name, return null.
|
|
188
|
-
*
|
|
189
|
-
* @param compilation
|
|
190
|
-
* @param chunkOrGroup
|
|
191
|
-
* @returns
|
|
192
|
-
* @private
|
|
193
|
-
*/ function getNamesOfAssetsInChunkOrGroup(compilation, chunkOrGroup) {
|
|
194
|
-
const chunkGroup = compilation.namedChunkGroups?.get(chunkOrGroup);
|
|
195
|
-
if (chunkGroup) {
|
|
196
|
-
const assetNames = [];
|
|
197
|
-
for (const chunk of chunkGroup.chunks){
|
|
198
|
-
assetNames.push(...getNamesOfAssetsInChunk(chunk));
|
|
199
|
-
}
|
|
200
|
-
return assetNames;
|
|
201
|
-
}
|
|
202
|
-
const chunk = compilation.namedChunks?.get(chunkOrGroup);
|
|
203
|
-
if (chunk) {
|
|
204
|
-
return getNamesOfAssetsInChunk(chunk);
|
|
205
|
-
}
|
|
206
|
-
// If we get here, there's no chunkGroup or chunk with that name.
|
|
207
|
-
return null;
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* Returns the names of all the assets in a chunk.
|
|
211
|
-
*
|
|
212
|
-
* @param chunk
|
|
213
|
-
* @returns
|
|
214
|
-
* @private
|
|
215
|
-
*/ function getNamesOfAssetsInChunk(chunk) {
|
|
216
|
-
const assetNames = [];
|
|
217
|
-
assetNames.push(...chunk.files);
|
|
218
|
-
// This only appears to be set in webpack v5.
|
|
219
|
-
if (chunk.auxiliaryFiles) {
|
|
220
|
-
assetNames.push(...chunk.auxiliaryFiles);
|
|
221
|
-
}
|
|
222
|
-
return assetNames;
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* Filters the set of assets out, based on the configuration options provided:
|
|
226
|
-
* - chunks and excludeChunks, for chunkName-based criteria.
|
|
227
|
-
* - include and exclude, for more general criteria.
|
|
228
|
-
*
|
|
229
|
-
* @param compilation The webpack compilation.
|
|
230
|
-
* @param config The validated configuration, obtained from the plugin.
|
|
231
|
-
* @returns The assets that should be included in the manifest,
|
|
232
|
-
* based on the criteria provided.
|
|
233
|
-
* @private
|
|
234
|
-
*/ function filterAssets(compilation, config) {
|
|
235
|
-
const filteredAssets = new Set();
|
|
236
|
-
const assets = compilation.getAssets();
|
|
237
|
-
const allowedAssetNames = new Set();
|
|
238
|
-
// See https://github.com/GoogleChrome/workbox/issues/1287
|
|
239
|
-
if (Array.isArray(config.chunks)) {
|
|
240
|
-
for (const name of config.chunks){
|
|
241
|
-
// See https://github.com/GoogleChrome/workbox/issues/2717
|
|
242
|
-
const assetsInChunkOrGroup = getNamesOfAssetsInChunkOrGroup(compilation, name);
|
|
243
|
-
if (assetsInChunkOrGroup) {
|
|
244
|
-
for (const assetName of assetsInChunkOrGroup){
|
|
245
|
-
allowedAssetNames.add(assetName);
|
|
246
|
-
}
|
|
247
|
-
} else {
|
|
248
|
-
compilation.warnings.push(new Error(`The chunk '${name}' was provided in your Serwist chunks config, but was not found in the compilation.`));
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
const deniedAssetNames = new Set();
|
|
253
|
-
if (Array.isArray(config.excludeChunks)) {
|
|
254
|
-
for (const name of config.excludeChunks){
|
|
255
|
-
// See https://github.com/GoogleChrome/workbox/issues/2717
|
|
256
|
-
const assetsInChunkOrGroup = getNamesOfAssetsInChunkOrGroup(compilation, name);
|
|
257
|
-
if (assetsInChunkOrGroup) {
|
|
258
|
-
for (const assetName of assetsInChunkOrGroup){
|
|
259
|
-
deniedAssetNames.add(assetName);
|
|
260
|
-
}
|
|
261
|
-
} // Don't warn if the chunk group isn't found.
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
for (const asset of assets){
|
|
265
|
-
// chunk based filtering is funky because:
|
|
266
|
-
// - Each asset might belong to one or more chunks.
|
|
267
|
-
// - If *any* of those chunk names match our config.excludeChunks,
|
|
268
|
-
// then we skip that asset.
|
|
269
|
-
// - If the config.chunks is defined *and* there's no match
|
|
270
|
-
// between at least one of the chunkNames and one entry, then
|
|
271
|
-
// we skip that assets as well.
|
|
272
|
-
if (deniedAssetNames.has(asset.name)) {
|
|
273
|
-
continue;
|
|
274
|
-
}
|
|
275
|
-
if (Array.isArray(config.chunks) && !allowedAssetNames.has(asset.name)) {
|
|
276
|
-
continue;
|
|
277
|
-
}
|
|
278
|
-
// Next, check asset-level checks via includes/excludes:
|
|
279
|
-
const isExcluded = checkConditions(asset, compilation, config.exclude);
|
|
280
|
-
if (isExcluded) {
|
|
281
|
-
continue;
|
|
282
|
-
}
|
|
283
|
-
// Treat an empty config.includes as an implicit inclusion.
|
|
284
|
-
const isIncluded = !Array.isArray(config.include) || checkConditions(asset, compilation, config.include);
|
|
285
|
-
if (!isIncluded) {
|
|
286
|
-
continue;
|
|
287
|
-
}
|
|
288
|
-
// If we've gotten this far, then add the asset.
|
|
289
|
-
filteredAssets.add(asset);
|
|
290
|
-
}
|
|
291
|
-
return filteredAssets;
|
|
292
|
-
}
|
|
293
|
-
async function getManifestEntriesFromCompilation(compilation, config) {
|
|
294
|
-
const filteredAssets = filterAssets(compilation, config);
|
|
295
|
-
const { publicPath } = compilation.options.output;
|
|
296
|
-
const fileDetails = Array.from(filteredAssets).map((asset)=>{
|
|
297
|
-
return {
|
|
298
|
-
file: resolveWebpackURL(publicPath, asset.name),
|
|
299
|
-
hash: getAssetHash(asset),
|
|
300
|
-
size: asset.source.size() || 0
|
|
301
|
-
};
|
|
302
|
-
});
|
|
303
|
-
const { manifestEntries, size, warnings } = await build.transformManifest({
|
|
304
|
-
fileDetails,
|
|
305
|
-
additionalPrecacheEntries: config.additionalPrecacheEntries,
|
|
306
|
-
dontCacheBustURLsMatching: config.dontCacheBustURLsMatching,
|
|
307
|
-
manifestTransforms: config.manifestTransforms,
|
|
308
|
-
maximumFileSizeToCacheInBytes: config.maximumFileSizeToCacheInBytes,
|
|
309
|
-
modifyURLPrefix: config.modifyURLPrefix,
|
|
310
|
-
transformParam: compilation
|
|
311
|
-
});
|
|
312
|
-
// See https://github.com/GoogleChrome/workbox/issues/2790
|
|
313
|
-
for (const warning of warnings){
|
|
314
|
-
compilation.warnings.push(new Error(warning));
|
|
315
|
-
}
|
|
316
|
-
// Ensure that the entries are properly sorted by URL.
|
|
317
|
-
const sortedEntries = manifestEntries?.sort((a, b)=>a.url === b.url ? 0 : a.url > b.url ? 1 : -1);
|
|
318
|
-
return {
|
|
319
|
-
size,
|
|
320
|
-
sortedEntries
|
|
321
|
-
};
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
/**
|
|
325
|
-
* If our bundled swDest file contains a sourcemap, we would invalidate that
|
|
326
|
-
* mapping if we just replaced injectionPoint with the stringified manifest.
|
|
327
|
-
* Instead, we need to update the swDest contents as well as the sourcemap
|
|
328
|
-
* at the same time.
|
|
329
|
-
*
|
|
330
|
-
* See https://github.com/GoogleChrome/workbox/issues/2235
|
|
331
|
-
*
|
|
332
|
-
* @param compilation The current webpack compilation.
|
|
333
|
-
* @param swContents The contents of the swSrc file, which may or
|
|
334
|
-
* may not include a valid sourcemap comment.
|
|
335
|
-
* @param swDest The configured swDest value.
|
|
336
|
-
* @returns If the swContents contains a valid sourcemap
|
|
337
|
-
* comment pointing to an asset present in the compilation, this will return the
|
|
338
|
-
* name of that asset. Otherwise, it will return undefined.
|
|
339
|
-
* @private
|
|
340
|
-
*/ function getSourcemapAssetName(compilation, swContents, swDest) {
|
|
341
|
-
const url = build.getSourceMapURL(swContents);
|
|
342
|
-
if (url) {
|
|
343
|
-
// Translate the relative URL to what the presumed name for the webpack
|
|
344
|
-
// asset should be.
|
|
345
|
-
// This *might* not be a valid asset if the sourcemap URL that was found
|
|
346
|
-
// was added by another module incidentally.
|
|
347
|
-
// See https://github.com/GoogleChrome/workbox/issues/2250
|
|
348
|
-
const swAssetDirname = upath.dirname(swDest);
|
|
349
|
-
const sourcemapURLAssetName = upath.normalize(upath.join(swAssetDirname, url));
|
|
350
|
-
// Not sure if there's a better way to check for asset existence?
|
|
351
|
-
if (compilation.getAsset(sourcemapURLAssetName)) {
|
|
352
|
-
return sourcemapURLAssetName;
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
return undefined;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
// Used to keep track of swDest files written by *any* instance of this plugin.
|
|
359
|
-
// See https://github.com/GoogleChrome/workbox/issues/2181
|
|
360
|
-
const _generatedAssetNames = new Set();
|
|
361
|
-
/**
|
|
362
|
-
* This class supports compiling a service worker file provided via `swSrc`,
|
|
363
|
-
* and injecting into that service worker a list of URLs and revision
|
|
364
|
-
* information for precaching based on the webpack asset pipeline.
|
|
365
|
-
*
|
|
366
|
-
* Use an instance of `InjectManifest` in the
|
|
367
|
-
* [`plugins` array](https://webpack.js.org/concepts/plugins/#usage) of a
|
|
368
|
-
* webpack config.
|
|
369
|
-
*
|
|
370
|
-
* In addition to injecting the manifest, this plugin will perform a compilation
|
|
371
|
-
* of the `swSrc` file, using the options from the main webpack configuration.
|
|
372
|
-
*
|
|
373
|
-
* ```
|
|
374
|
-
* // The following lists some common options; see the rest of the documentation
|
|
375
|
-
* // for the full set of options and defaults.
|
|
376
|
-
* new InjectManifest({
|
|
377
|
-
* exclude: [/.../, '...'],
|
|
378
|
-
* maximumFileSizeToCacheInBytes: ...,
|
|
379
|
-
* swSrc: '...',
|
|
380
|
-
* });
|
|
381
|
-
* ```
|
|
382
|
-
*/ class InjectManifest {
|
|
383
|
-
config;
|
|
384
|
-
alreadyCalled;
|
|
385
|
-
/**
|
|
386
|
-
* Creates an instance of InjectManifest.
|
|
387
|
-
*/ constructor(config){
|
|
388
|
-
this.config = config;
|
|
389
|
-
this.alreadyCalled = false;
|
|
390
|
-
}
|
|
391
|
-
/**
|
|
392
|
-
* @param compiler default compiler object passed from webpack
|
|
393
|
-
*
|
|
394
|
-
* @private
|
|
395
|
-
*/ propagateWebpackConfig(compiler) {
|
|
396
|
-
const parsedSwSrc = upath.parse(this.config.swSrc);
|
|
397
|
-
// Because this.config is listed last, properties that are already set
|
|
398
|
-
// there take precedence over derived properties from the compiler.
|
|
399
|
-
this.config = Object.assign({
|
|
400
|
-
mode: compiler.options.mode,
|
|
401
|
-
// Use swSrc with a hardcoded .js extension, in case swSrc is a .ts file.
|
|
402
|
-
swDest: `${parsedSwSrc.name}.js`
|
|
403
|
-
}, this.config);
|
|
404
|
-
}
|
|
405
|
-
/**
|
|
406
|
-
* `getManifestEntriesFromCompilation` with a few additional checks.
|
|
407
|
-
*
|
|
408
|
-
* @private
|
|
409
|
-
*/ async getManifestEntries(compilation, config) {
|
|
410
|
-
if (config.disablePrecacheManifest) {
|
|
411
|
-
return {
|
|
412
|
-
size: 0,
|
|
413
|
-
sortedEntries: undefined,
|
|
414
|
-
manifestString: "undefined"
|
|
415
|
-
};
|
|
416
|
-
}
|
|
417
|
-
// See https://github.com/GoogleChrome/workbox/issues/1790
|
|
418
|
-
if (this.alreadyCalled) {
|
|
419
|
-
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.`;
|
|
420
|
-
if (!compilation.warnings.some((warning)=>warning instanceof Error && warning.message === warningMessage)) {
|
|
421
|
-
compilation.warnings.push(new Error(warningMessage));
|
|
422
|
-
}
|
|
423
|
-
} else {
|
|
424
|
-
this.alreadyCalled = true;
|
|
425
|
-
}
|
|
426
|
-
// Ensure that we don't precache any of the assets generated by *any*
|
|
427
|
-
// instance of this plugin.
|
|
428
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
429
|
-
config.exclude.push(({ asset })=>_generatedAssetNames.has(asset.name));
|
|
430
|
-
const { size, sortedEntries } = await getManifestEntriesFromCompilation(compilation, config);
|
|
431
|
-
let manifestString = stringify(sortedEntries);
|
|
432
|
-
if (this.config.compileSrc && // See https://github.com/GoogleChrome/workbox/issues/2729
|
|
433
|
-
!(compilation.options?.devtool === "eval-cheap-source-map" && compilation.options.optimization?.minimize)) {
|
|
434
|
-
// See https://github.com/GoogleChrome/workbox/issues/2263
|
|
435
|
-
manifestString = manifestString.replace(/"/g, `'`);
|
|
436
|
-
}
|
|
437
|
-
return {
|
|
438
|
-
size,
|
|
439
|
-
sortedEntries,
|
|
440
|
-
manifestString
|
|
441
|
-
};
|
|
442
|
-
}
|
|
443
|
-
/**
|
|
444
|
-
* @param compiler default compiler object passed from webpack
|
|
445
|
-
*
|
|
446
|
-
* @private
|
|
447
|
-
*/ apply(compiler) {
|
|
448
|
-
this.propagateWebpackConfig(compiler);
|
|
449
|
-
compiler.hooks.make.tapPromise(this.constructor.name, (compilation)=>this.handleMake(compilation, compiler).catch((error)=>{
|
|
450
|
-
compilation.errors.push(error);
|
|
451
|
-
}));
|
|
452
|
-
const { PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER } = webpack.Compilation;
|
|
453
|
-
// Specifically hook into thisCompilation, as per
|
|
454
|
-
// https://github.com/webpack/webpack/issues/11425#issuecomment-690547848
|
|
455
|
-
compiler.hooks.thisCompilation.tap(this.constructor.name, (compilation)=>{
|
|
456
|
-
compilation.hooks.processAssets.tapPromise({
|
|
457
|
-
name: this.constructor.name,
|
|
458
|
-
// TODO(jeffposnick): This may need to change eventually.
|
|
459
|
-
// See https://github.com/webpack/webpack/issues/11822#issuecomment-726184972
|
|
460
|
-
stage: PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER - 10
|
|
461
|
-
}, ()=>this.addAssets(compilation).catch((error)=>{
|
|
462
|
-
compilation.errors.push(error);
|
|
463
|
-
}));
|
|
464
|
-
});
|
|
465
|
-
}
|
|
466
|
-
/**
|
|
467
|
-
* @param compilation The webpack compilation.
|
|
468
|
-
* @param parentCompiler The webpack parent compiler.
|
|
469
|
-
*
|
|
470
|
-
* @private
|
|
471
|
-
*/ async performChildCompilation(compilation, parentCompiler) {
|
|
472
|
-
const outputOptions = {
|
|
473
|
-
filename: this.config.swDest
|
|
474
|
-
};
|
|
475
|
-
const childCompiler = compilation.createChildCompiler(this.constructor.name, outputOptions, []);
|
|
476
|
-
childCompiler.context = parentCompiler.context;
|
|
477
|
-
childCompiler.inputFileSystem = parentCompiler.inputFileSystem;
|
|
478
|
-
childCompiler.outputFileSystem = parentCompiler.outputFileSystem;
|
|
479
|
-
if (Array.isArray(this.config.webpackCompilationPlugins)) {
|
|
480
|
-
for (const plugin of this.config.webpackCompilationPlugins){
|
|
481
|
-
plugin.apply(childCompiler);
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
new webpack.EntryPlugin(parentCompiler.context, this.config.swSrc, this.constructor.name).apply(childCompiler);
|
|
485
|
-
await new Promise((resolve, reject)=>{
|
|
486
|
-
childCompiler.runAsChild((error, _entries, childCompilation)=>{
|
|
487
|
-
if (error) {
|
|
488
|
-
reject(error);
|
|
489
|
-
} else {
|
|
490
|
-
compilation.warnings = compilation.warnings.concat(childCompilation?.warnings ?? []);
|
|
491
|
-
compilation.errors = compilation.errors.concat(childCompilation?.errors ?? []);
|
|
492
|
-
resolve();
|
|
493
|
-
}
|
|
494
|
-
});
|
|
495
|
-
});
|
|
496
|
-
}
|
|
497
|
-
/**
|
|
498
|
-
* @param compilation The webpack compilation.
|
|
499
|
-
* @param parentCompiler The webpack parent compiler.
|
|
500
|
-
*
|
|
501
|
-
* @private
|
|
502
|
-
*/ addSrcToAssets(compilation, parentCompiler) {
|
|
503
|
-
// eslint-disable-next-line
|
|
504
|
-
const source = parentCompiler.inputFileSystem.readFileSync(this.config.swSrc);
|
|
505
|
-
compilation.emitAsset(this.config.swDest, new webpack.sources.RawSource(source));
|
|
506
|
-
}
|
|
507
|
-
/**
|
|
508
|
-
* @param compilation The webpack compilation.
|
|
509
|
-
* @param parentCompiler The webpack parent compiler.
|
|
510
|
-
*
|
|
511
|
-
* @private
|
|
512
|
-
*/ async handleMake(compilation, parentCompiler) {
|
|
513
|
-
try {
|
|
514
|
-
this.config = build.validateWebpackInjectManifestOptions(this.config);
|
|
515
|
-
} catch (error) {
|
|
516
|
-
if (error instanceof Error) {
|
|
517
|
-
throw new Error(`Please check your ${this.constructor.name} plugin ` + `configuration:\n${error.message}`);
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
this.config.swDest = relativeToOutputPath.relativeToOutputPath(compilation, this.config.swDest);
|
|
521
|
-
_generatedAssetNames.add(this.config.swDest);
|
|
522
|
-
if (this.config.compileSrc) {
|
|
523
|
-
await this.performChildCompilation(compilation, parentCompiler);
|
|
524
|
-
} else {
|
|
525
|
-
this.addSrcToAssets(compilation, parentCompiler);
|
|
526
|
-
// This used to be a fatal error, but just warn at runtime because we
|
|
527
|
-
// can't validate it easily.
|
|
528
|
-
if (Array.isArray(this.config.webpackCompilationPlugins) && this.config.webpackCompilationPlugins.length > 0) {
|
|
529
|
-
compilation.warnings.push(new Error("compileSrc is false, so the " + "webpackCompilationPlugins option will be ignored."));
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
/**
|
|
534
|
-
* @param compilation The webpack compilation.
|
|
535
|
-
*
|
|
536
|
-
* @private
|
|
537
|
-
*/ async addAssets(compilation) {
|
|
538
|
-
const config = Object.assign({}, this.config);
|
|
539
|
-
const { size, sortedEntries, manifestString } = await this.getManifestEntries(compilation, config);
|
|
540
|
-
// See https://webpack.js.org/contribute/plugin-patterns/#monitoring-the-watch-graph
|
|
541
|
-
const absoluteSwSrc = upath.resolve(config.swSrc);
|
|
542
|
-
compilation.fileDependencies.add(absoluteSwSrc);
|
|
543
|
-
const swAsset = compilation.getAsset(config.swDest);
|
|
544
|
-
const swAssetString = swAsset.source.source().toString();
|
|
545
|
-
const globalRegexp = new RegExp(build.escapeRegExp(config.injectionPoint), "g");
|
|
546
|
-
const injectionResults = swAssetString.match(globalRegexp);
|
|
547
|
-
if (!injectionResults) {
|
|
548
|
-
throw new Error(`Can't find ${config.injectionPoint} in your SW source.`);
|
|
549
|
-
}
|
|
550
|
-
if (injectionResults.length !== 1) {
|
|
551
|
-
throw new Error(`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`);
|
|
552
|
-
}
|
|
553
|
-
const sourcemapAssetName = getSourcemapAssetName(compilation, swAssetString, config.swDest);
|
|
554
|
-
if (sourcemapAssetName) {
|
|
555
|
-
_generatedAssetNames.add(sourcemapAssetName);
|
|
556
|
-
const sourcemapAsset = compilation.getAsset(sourcemapAssetName);
|
|
557
|
-
const { source, map } = await build.replaceAndUpdateSourceMap({
|
|
558
|
-
jsFilename: config.swDest,
|
|
559
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
560
|
-
originalMap: JSON.parse(sourcemapAsset.source.source().toString()),
|
|
561
|
-
originalSource: swAssetString,
|
|
562
|
-
replaceString: manifestString,
|
|
563
|
-
searchString: config.injectionPoint
|
|
564
|
-
});
|
|
565
|
-
compilation.updateAsset(sourcemapAssetName, new webpack.sources.RawSource(map));
|
|
566
|
-
compilation.updateAsset(config.swDest, new webpack.sources.RawSource(source));
|
|
567
|
-
} else {
|
|
568
|
-
// If there's no sourcemap associated with swDest, a simple string
|
|
569
|
-
// replacement will suffice.
|
|
570
|
-
compilation.updateAsset(config.swDest, new webpack.sources.RawSource(swAssetString.replace(config.injectionPoint, manifestString)));
|
|
571
|
-
}
|
|
572
|
-
if (compilation.getLogger) {
|
|
573
|
-
const logger = compilation.getLogger(this.constructor.name);
|
|
574
|
-
logger.info(`The service worker at ${config.swDest ?? ""} will precache ${sortedEntries?.length ?? 0} URLs, totaling ${prettyBytes(size)}.`);
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
exports.InjectManifest = InjectManifest;
|
package/dist/index.d.cts
DELETED
|
File without changes
|
package/dist/index.internal.cjs
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var webpack = require('webpack');
|
|
4
|
-
var relativeToOutputPath = require('./relative-to-output-path.cjs');
|
|
5
|
-
require('upath');
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Compile a file by creating a child of the hooked compiler.
|
|
9
|
-
*
|
|
10
|
-
* @private
|
|
11
|
-
*/ class ChildCompilationPlugin {
|
|
12
|
-
src;
|
|
13
|
-
dest;
|
|
14
|
-
plugins;
|
|
15
|
-
constructor({ src, dest, plugins }){
|
|
16
|
-
this.src = src;
|
|
17
|
-
this.dest = dest;
|
|
18
|
-
this.plugins = plugins;
|
|
19
|
-
}
|
|
20
|
-
apply(compiler) {
|
|
21
|
-
compiler.hooks.make.tapPromise(this.constructor.name, (compilation)=>this.performChildCompilation(compilation, compiler).catch((error)=>{
|
|
22
|
-
compilation.errors.push(error);
|
|
23
|
-
}));
|
|
24
|
-
}
|
|
25
|
-
async performChildCompilation(compilation, parentCompiler) {
|
|
26
|
-
const resolvedDest = relativeToOutputPath.relativeToOutputPath(compilation, this.dest);
|
|
27
|
-
const outputOptions = {
|
|
28
|
-
filename: resolvedDest
|
|
29
|
-
};
|
|
30
|
-
const childCompiler = compilation.createChildCompiler(this.constructor.name, outputOptions, []);
|
|
31
|
-
childCompiler.context = parentCompiler.context;
|
|
32
|
-
childCompiler.inputFileSystem = parentCompiler.inputFileSystem;
|
|
33
|
-
childCompiler.outputFileSystem = parentCompiler.outputFileSystem;
|
|
34
|
-
if (this.plugins !== undefined) {
|
|
35
|
-
for (const plugin of this.plugins){
|
|
36
|
-
plugin?.apply(childCompiler);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
new webpack.EntryPlugin(parentCompiler.context, this.src, this.constructor.name).apply(childCompiler);
|
|
40
|
-
await new Promise((resolve, reject)=>{
|
|
41
|
-
childCompiler.runAsChild((error, _entries, childCompilation)=>{
|
|
42
|
-
if (error) {
|
|
43
|
-
reject(error);
|
|
44
|
-
} else {
|
|
45
|
-
compilation.warnings = compilation.warnings.concat(childCompilation?.warnings ?? []);
|
|
46
|
-
compilation.errors = compilation.errors.concat(childCompilation?.errors ?? []);
|
|
47
|
-
resolve();
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
exports.ChildCompilationPlugin = ChildCompilationPlugin;
|
|
File without changes
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { WebpackPluginInstance } from "webpack";
|
|
2
|
-
import webpack from "webpack";
|
|
3
|
-
export interface ChildCompilationPluginOptions {
|
|
4
|
-
src: string;
|
|
5
|
-
dest: string;
|
|
6
|
-
plugins?: WebpackPluginInstance[];
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Compile a file by creating a child of the hooked compiler.
|
|
10
|
-
*
|
|
11
|
-
* @private
|
|
12
|
-
*/
|
|
13
|
-
export declare class ChildCompilationPlugin implements WebpackPluginInstance {
|
|
14
|
-
src: string;
|
|
15
|
-
dest: string;
|
|
16
|
-
plugins: WebpackPluginInstance[] | undefined;
|
|
17
|
-
constructor({ src, dest, plugins }: ChildCompilationPluginOptions);
|
|
18
|
-
apply(compiler: webpack.Compiler): void;
|
|
19
|
-
performChildCompilation(compilation: webpack.Compilation, parentCompiler: webpack.Compiler): Promise<void>;
|
|
20
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { ManifestEntry, WebpackInjectManifestOptions } from "@serwist/build";
|
|
2
|
-
import type { Compilation } from "webpack";
|
|
3
|
-
export declare function getManifestEntriesFromCompilation(compilation: Compilation, config: WebpackInjectManifestOptions): Promise<{
|
|
4
|
-
size: number;
|
|
5
|
-
sortedEntries: ManifestEntry[] | undefined;
|
|
6
|
-
}>;
|
|
File without changes
|
|
File without changes
|