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