@serwist/webpack-plugin 8.3.0 → 8.4.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/index.cjs CHANGED
@@ -120,7 +120,7 @@ function prettyBytes(number, options) {
120
120
  // If webpack has the asset marked as immutable, then we don't need to
121
121
  // use an out-of-band revision for it.
122
122
  // See https://github.com/webpack/webpack/issues/9038
123
- if (asset.info && asset.info.immutable) {
123
+ if (asset.info?.immutable) {
124
124
  return null;
125
125
  }
126
126
  return crypto.createHash("md5").update(Buffer.from(asset.source.source())).digest("hex");
@@ -147,12 +147,11 @@ function prettyBytes(number, options) {
147
147
  // See https://github.com/jantimon/html-webpack-plugin/pull/1516
148
148
  if (publicPath === "auto") {
149
149
  return paths.join("");
150
- } else {
151
- return [
152
- publicPath,
153
- ...paths
154
- ].join("");
155
150
  }
151
+ return [
152
+ publicPath,
153
+ ...paths
154
+ ].join("");
156
155
  }
157
156
 
158
157
  /**
@@ -173,10 +172,9 @@ function prettyBytes(number, options) {
173
172
  compilation
174
173
  });
175
174
  //return compilation !== null;
176
- } else {
177
- if (webpack.ModuleFilenameHelpers.matchPart(asset.name, condition)) {
178
- return true;
179
- }
175
+ }
176
+ if (webpack.ModuleFilenameHelpers.matchPart(asset.name, condition)) {
177
+ return true;
180
178
  }
181
179
  }
182
180
  // We'll only get here if none of the conditions applied.
@@ -193,18 +191,17 @@ function prettyBytes(number, options) {
193
191
  * @returns
194
192
  * @private
195
193
  */ function getNamesOfAssetsInChunkOrGroup(compilation, chunkOrGroup) {
196
- const chunkGroup = compilation.namedChunkGroups && compilation.namedChunkGroups.get(chunkOrGroup);
194
+ const chunkGroup = compilation.namedChunkGroups?.get(chunkOrGroup);
197
195
  if (chunkGroup) {
198
196
  const assetNames = [];
199
197
  for (const chunk of chunkGroup.chunks){
200
198
  assetNames.push(...getNamesOfAssetsInChunk(chunk));
201
199
  }
202
200
  return assetNames;
203
- } else {
204
- const chunk = compilation.namedChunks && compilation.namedChunks.get(chunkOrGroup);
205
- if (chunk) {
206
- return getNamesOfAssetsInChunk(chunk);
207
- }
201
+ }
202
+ const chunk = compilation.namedChunks?.get(chunkOrGroup);
203
+ if (chunk) {
204
+ return getNamesOfAssetsInChunk(chunk);
208
205
  }
209
206
  // If we get here, there's no chunkGroup or chunk with that name.
210
207
  return null;
@@ -248,7 +245,7 @@ function prettyBytes(number, options) {
248
245
  allowedAssetNames.add(assetName);
249
246
  }
250
247
  } else {
251
- compilation.warnings.push(new Error(`The chunk '${name}' was ` + `provided in your Serwist chunks config, but was not found in the ` + `compilation.`));
248
+ compilation.warnings.push(new Error(`The chunk '${name}' was provided in your Serwist chunks config, but was not found in the compilation.`));
252
249
  }
253
250
  }
254
251
  }
@@ -402,7 +399,7 @@ const _generatedAssetNames = new Set();
402
399
  this.config = Object.assign({
403
400
  mode: compiler.options.mode,
404
401
  // Use swSrc with a hardcoded .js extension, in case swSrc is a .ts file.
405
- swDest: parsedSwSrc.name + ".js"
402
+ swDest: `${parsedSwSrc.name}.js`
406
403
  }, this.config);
407
404
  }
408
405
  /**
@@ -419,7 +416,7 @@ const _generatedAssetNames = new Set();
419
416
  }
420
417
  // See https://github.com/GoogleChrome/workbox/issues/1790
421
418
  if (this.alreadyCalled) {
422
- 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.`;
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.`;
423
420
  if (!compilation.warnings.some((warning)=>warning instanceof Error && warning.message === warningMessage)) {
424
421
  compilation.warnings.push(new Error(warningMessage));
425
422
  }
@@ -551,7 +548,7 @@ const _generatedAssetNames = new Set();
551
548
  throw new Error(`Can't find ${config.injectionPoint} in your SW source.`);
552
549
  }
553
550
  if (injectionResults.length !== 1) {
554
- 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");
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`);
555
552
  }
556
553
  const sourcemapAssetName = getSourcemapAssetName(compilation, swAssetString, config.swDest);
557
554
  if (sourcemapAssetName) {
package/dist/index.d.cts CHANGED
@@ -0,0 +1,2 @@
1
+ import { InjectManifest } from "./inject-manifest.js";
2
+ export { InjectManifest };
package/dist/index.js CHANGED
@@ -118,7 +118,7 @@ function prettyBytes(number, options) {
118
118
  // If webpack has the asset marked as immutable, then we don't need to
119
119
  // use an out-of-band revision for it.
120
120
  // See https://github.com/webpack/webpack/issues/9038
121
- if (asset.info && asset.info.immutable) {
121
+ if (asset.info?.immutable) {
122
122
  return null;
123
123
  }
124
124
  return crypto.createHash("md5").update(Buffer.from(asset.source.source())).digest("hex");
@@ -145,12 +145,11 @@ function prettyBytes(number, options) {
145
145
  // See https://github.com/jantimon/html-webpack-plugin/pull/1516
146
146
  if (publicPath === "auto") {
147
147
  return paths.join("");
148
- } else {
149
- return [
150
- publicPath,
151
- ...paths
152
- ].join("");
153
148
  }
149
+ return [
150
+ publicPath,
151
+ ...paths
152
+ ].join("");
154
153
  }
155
154
 
156
155
  /**
@@ -171,10 +170,9 @@ function prettyBytes(number, options) {
171
170
  compilation
172
171
  });
173
172
  //return compilation !== null;
174
- } else {
175
- if (webpack.ModuleFilenameHelpers.matchPart(asset.name, condition)) {
176
- return true;
177
- }
173
+ }
174
+ if (webpack.ModuleFilenameHelpers.matchPart(asset.name, condition)) {
175
+ return true;
178
176
  }
179
177
  }
180
178
  // We'll only get here if none of the conditions applied.
@@ -191,18 +189,17 @@ function prettyBytes(number, options) {
191
189
  * @returns
192
190
  * @private
193
191
  */ function getNamesOfAssetsInChunkOrGroup(compilation, chunkOrGroup) {
194
- const chunkGroup = compilation.namedChunkGroups && compilation.namedChunkGroups.get(chunkOrGroup);
192
+ const chunkGroup = compilation.namedChunkGroups?.get(chunkOrGroup);
195
193
  if (chunkGroup) {
196
194
  const assetNames = [];
197
195
  for (const chunk of chunkGroup.chunks){
198
196
  assetNames.push(...getNamesOfAssetsInChunk(chunk));
199
197
  }
200
198
  return assetNames;
201
- } else {
202
- const chunk = compilation.namedChunks && compilation.namedChunks.get(chunkOrGroup);
203
- if (chunk) {
204
- return getNamesOfAssetsInChunk(chunk);
205
- }
199
+ }
200
+ const chunk = compilation.namedChunks?.get(chunkOrGroup);
201
+ if (chunk) {
202
+ return getNamesOfAssetsInChunk(chunk);
206
203
  }
207
204
  // If we get here, there's no chunkGroup or chunk with that name.
208
205
  return null;
@@ -246,7 +243,7 @@ function prettyBytes(number, options) {
246
243
  allowedAssetNames.add(assetName);
247
244
  }
248
245
  } else {
249
- compilation.warnings.push(new Error(`The chunk '${name}' was ` + `provided in your Serwist chunks config, but was not found in the ` + `compilation.`));
246
+ compilation.warnings.push(new Error(`The chunk '${name}' was provided in your Serwist chunks config, but was not found in the compilation.`));
250
247
  }
251
248
  }
252
249
  }
@@ -400,7 +397,7 @@ const _generatedAssetNames = new Set();
400
397
  this.config = Object.assign({
401
398
  mode: compiler.options.mode,
402
399
  // Use swSrc with a hardcoded .js extension, in case swSrc is a .ts file.
403
- swDest: parsedSwSrc.name + ".js"
400
+ swDest: `${parsedSwSrc.name}.js`
404
401
  }, this.config);
405
402
  }
406
403
  /**
@@ -417,7 +414,7 @@ const _generatedAssetNames = new Set();
417
414
  }
418
415
  // See https://github.com/GoogleChrome/workbox/issues/1790
419
416
  if (this.alreadyCalled) {
420
- 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.`;
417
+ 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.`;
421
418
  if (!compilation.warnings.some((warning)=>warning instanceof Error && warning.message === warningMessage)) {
422
419
  compilation.warnings.push(new Error(warningMessage));
423
420
  }
@@ -549,7 +546,7 @@ const _generatedAssetNames = new Set();
549
546
  throw new Error(`Can't find ${config.injectionPoint} in your SW source.`);
550
547
  }
551
548
  if (injectionResults.length !== 1) {
552
- 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");
549
+ 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`);
553
550
  }
554
551
  const sourcemapAssetName = getSourcemapAssetName(compilation, swAssetString, config.swDest);
555
552
  if (sourcemapAssetName) {
@@ -0,0 +1,82 @@
1
+ import type { WebpackInjectManifestOptions } from "@serwist/build";
2
+ import webpack from "webpack";
3
+ /**
4
+ * This class supports compiling a service worker file provided via `swSrc`,
5
+ * and injecting into that service worker a list of URLs and revision
6
+ * information for precaching based on the webpack asset pipeline.
7
+ *
8
+ * Use an instance of `InjectManifest` in the
9
+ * [`plugins` array](https://webpack.js.org/concepts/plugins/#usage) of a
10
+ * webpack config.
11
+ *
12
+ * In addition to injecting the manifest, this plugin will perform a compilation
13
+ * of the `swSrc` file, using the options from the main webpack configuration.
14
+ *
15
+ * ```
16
+ * // The following lists some common options; see the rest of the documentation
17
+ * // for the full set of options and defaults.
18
+ * new InjectManifest({
19
+ * exclude: [/.../, '...'],
20
+ * maximumFileSizeToCacheInBytes: ...,
21
+ * swSrc: '...',
22
+ * });
23
+ * ```
24
+ */
25
+ declare class InjectManifest {
26
+ protected config: WebpackInjectManifestOptions;
27
+ private alreadyCalled;
28
+ /**
29
+ * Creates an instance of InjectManifest.
30
+ */
31
+ constructor(config: WebpackInjectManifestOptions);
32
+ /**
33
+ * @param compiler default compiler object passed from webpack
34
+ *
35
+ * @private
36
+ */
37
+ propagateWebpackConfig(compiler: webpack.Compiler): void;
38
+ /**
39
+ * `getManifestEntriesFromCompilation` with a few additional checks.
40
+ *
41
+ * @private
42
+ */
43
+ getManifestEntries(compilation: webpack.Compilation, config: WebpackInjectManifestOptions): Promise<{
44
+ size: number;
45
+ sortedEntries: import("@serwist/build").ManifestEntry[] | undefined;
46
+ manifestString: string;
47
+ }>;
48
+ /**
49
+ * @param compiler default compiler object passed from webpack
50
+ *
51
+ * @private
52
+ */
53
+ apply(compiler: webpack.Compiler): void;
54
+ /**
55
+ * @param compilation The webpack compilation.
56
+ * @param parentCompiler The webpack parent compiler.
57
+ *
58
+ * @private
59
+ */
60
+ performChildCompilation(compilation: webpack.Compilation, parentCompiler: webpack.Compiler): Promise<void>;
61
+ /**
62
+ * @param compilation The webpack compilation.
63
+ * @param parentCompiler The webpack parent compiler.
64
+ *
65
+ * @private
66
+ */
67
+ addSrcToAssets(compilation: webpack.Compilation, parentCompiler: webpack.Compiler): void;
68
+ /**
69
+ * @param compilation The webpack compilation.
70
+ * @param parentCompiler The webpack parent compiler.
71
+ *
72
+ * @private
73
+ */
74
+ handleMake(compilation: webpack.Compilation, parentCompiler: webpack.Compiler): Promise<void>;
75
+ /**
76
+ * @param compilation The webpack compilation.
77
+ *
78
+ * @private
79
+ */
80
+ addAssets(compilation: webpack.Compilation): Promise<void>;
81
+ }
82
+ export { InjectManifest };
@@ -0,0 +1,19 @@
1
+ import type { Compilation } from "webpack";
2
+ /**
3
+ * If our bundled swDest file contains a sourcemap, we would invalidate that
4
+ * mapping if we just replaced injectionPoint with the stringified manifest.
5
+ * Instead, we need to update the swDest contents as well as the sourcemap
6
+ * at the same time.
7
+ *
8
+ * See https://github.com/GoogleChrome/workbox/issues/2235
9
+ *
10
+ * @param compilation The current webpack compilation.
11
+ * @param swContents The contents of the swSrc file, which may or
12
+ * may not include a valid sourcemap comment.
13
+ * @param swDest The configured swDest value.
14
+ * @returns If the swContents contains a valid sourcemap
15
+ * comment pointing to an asset present in the compilation, this will return the
16
+ * name of that asset. Otherwise, it will return undefined.
17
+ * @private
18
+ */
19
+ export declare function getSourcemapAssetName(compilation: Compilation, swContents: string, swDest: string): string | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serwist/webpack-plugin",
3
- "version": "8.3.0",
3
+ "version": "8.4.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": [
@@ -24,7 +24,7 @@
24
24
  "license": "MIT",
25
25
  "repository": "serwist/serwist",
26
26
  "bugs": "https://github.com/serwist/serwist/issues",
27
- "homepage": "https://serwist.vercel.app",
27
+ "homepage": "https://serwist.pages.dev",
28
28
  "module": "./dist/index.js",
29
29
  "main": "./dist/index.cjs",
30
30
  "types": "./dist/index.d.ts",
@@ -61,7 +61,7 @@
61
61
  "dependencies": {
62
62
  "fast-json-stable-stringify": "2.1.0",
63
63
  "upath": "2.0.1",
64
- "@serwist/build": "8.3.0"
64
+ "@serwist/build": "8.4.1"
65
65
  },
66
66
  "peerDependencies": {
67
67
  "webpack": "4.4.0 || ^5.9.0"
@@ -71,11 +71,12 @@
71
71
  "@types/webpack": "5.28.5",
72
72
  "pretty-bytes": "6.1.1",
73
73
  "rollup": "4.9.1",
74
- "@serwist/constants": "8.3.0"
74
+ "@serwist/constants": "8.4.1"
75
75
  },
76
76
  "scripts": {
77
77
  "build": "rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js",
78
- "lint": "eslint src --ext ts,tsx,js,jsx,cjs,mjs",
78
+ "dev": "rollup --config rollup.config.js --watch",
79
+ "lint": "biome lint ./src",
79
80
  "typecheck": "tsc"
80
81
  }
81
82
  }