@serwist/webpack-plugin 9.0.5 → 9.0.7

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2018 Google LLC, 2019 ShadowWalker w@weiw.io https://weiw.io, 2023 Serwist
3
+ Copyright (c) 2018-2023 Google LLC, 2019-2023 ShadowWalker w@weiw.io https://weiw.io, 2020-2023 Anthony Fu <https://github.com/antfu>, 2023-PRESENT Serwist
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -0,0 +1,33 @@
1
+ import path from 'node:path';
2
+
3
+ const relativeToOutputPath = (compilation, originalPath)=>{
4
+ if (path.resolve(originalPath) === path.normalize(originalPath)) {
5
+ return path.relative(compilation.options.output.path, originalPath);
6
+ }
7
+ return originalPath;
8
+ };
9
+
10
+ const performChildCompilation = async (compiler, compilation, name, src, dest, plugins)=>{
11
+ const childCompiler = compilation.createChildCompiler(name, {
12
+ filename: dest
13
+ }, plugins);
14
+ new compiler.webpack.webworker.WebWorkerTemplatePlugin().apply(childCompiler);
15
+ new compiler.webpack.EntryPlugin(compiler.context, src, name).apply(childCompiler);
16
+ await new Promise((resolve, reject)=>{
17
+ childCompiler.runAsChild((error, _entries, childCompilation)=>{
18
+ if (error) {
19
+ reject(error);
20
+ } else {
21
+ if (childCompilation?.warnings) {
22
+ compilation.warnings.push(...childCompilation.warnings);
23
+ }
24
+ if (childCompilation?.errors) {
25
+ compilation.errors.push(...childCompilation.errors);
26
+ }
27
+ resolve();
28
+ }
29
+ });
30
+ });
31
+ };
32
+
33
+ export { performChildCompilation as p, relativeToOutputPath as r };
@@ -1,54 +1,20 @@
1
- import { r as relativeToOutputPath } from './chunks/relative-to-output-path.js';
2
- import 'upath';
1
+ import { p as performChildCompilation, r as relativeToOutputPath } from './chunks/perform-child-compilation.js';
2
+ import 'node:path';
3
3
 
4
4
  class ChildCompilationPlugin {
5
5
  src;
6
6
  dest;
7
7
  plugins;
8
- webpack;
9
8
  constructor({ src, dest, plugins }){
10
9
  this.src = src;
11
10
  this.dest = dest;
12
11
  this.plugins = plugins;
13
- this.webpack = null;
14
- }
15
- propagateWebpackConfig(compiler) {
16
- this.webpack = compiler.webpack;
17
12
  }
18
13
  apply(compiler) {
19
- this.propagateWebpackConfig(compiler);
20
- compiler.hooks.make.tapPromise(this.constructor.name, (compilation)=>this.performChildCompilation(compilation, compiler).catch((error)=>{
14
+ compiler.hooks.make.tapPromise(this.constructor.name, (compilation)=>performChildCompilation(compiler, compilation, this.constructor.name, this.src, relativeToOutputPath(compilation, this.dest), this.plugins).catch((error)=>{
21
15
  compilation.errors.push(error);
22
16
  }));
23
17
  }
24
- async performChildCompilation(compilation, parentCompiler) {
25
- const resolvedDest = relativeToOutputPath(compilation, this.dest);
26
- const outputOptions = {
27
- filename: resolvedDest
28
- };
29
- const childCompiler = compilation.createChildCompiler(this.constructor.name, outputOptions, []);
30
- childCompiler.options.target = "webworker";
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 this.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
18
  }
53
19
 
54
20
  export { ChildCompilationPlugin, relativeToOutputPath };
package/dist/index.js CHANGED
@@ -1,9 +1,13 @@
1
+ import path from 'node:path';
1
2
  import { transformManifest, getSourceMapURL, stringify, escapeRegExp, replaceAndUpdateSourceMap } from '@serwist/build';
2
3
  import prettyBytes from 'pretty-bytes';
3
- import upath from 'upath';
4
4
  import { validationErrorMap, SerwistConfigError } from '@serwist/build/schema';
5
5
  import crypto from 'node:crypto';
6
- import { r as relativeToOutputPath } from './chunks/relative-to-output-path.js';
6
+ import { r as relativeToOutputPath, p as performChildCompilation } from './chunks/perform-child-compilation.js';
7
+
8
+ const toUnix = (p)=>{
9
+ return p.replace(/\\/g, "/").replace(/(?<!^)\/+/g, "/");
10
+ };
7
11
 
8
12
  const validateInjectManifestOptions = async (input)=>{
9
13
  const result = await (await import('./chunks/schema.js')).injectManifestOptions.spa(input, {
@@ -151,8 +155,8 @@ const getManifestEntriesFromCompilation = async (compilation, config)=>{
151
155
  const getSourcemapAssetName = (compilation, swContents, swDest)=>{
152
156
  const url = getSourceMapURL(swContents);
153
157
  if (url) {
154
- const swAssetDirname = upath.dirname(swDest);
155
- const sourcemapURLAssetName = upath.normalize(upath.join(swAssetDirname, url));
158
+ const swAssetDirname = path.dirname(swDest);
159
+ const sourcemapURLAssetName = path.normalize(path.join(swAssetDirname, url));
156
160
  if (compilation.getAsset(sourcemapURLAssetName)) {
157
161
  return sourcemapURLAssetName;
158
162
  }
@@ -172,7 +176,7 @@ class InjectManifest {
172
176
  }
173
177
  propagateWebpackConfig(compiler) {
174
178
  this.webpack = compiler.webpack;
175
- const parsedSwSrc = upath.parse(this.config.swSrc);
179
+ const parsedSwSrc = path.parse(this.config.swSrc);
176
180
  this.config = {
177
181
  swDest: `${parsedSwSrc.name}.js`,
178
182
  ...this.config
@@ -208,7 +212,7 @@ class InjectManifest {
208
212
  }
209
213
  apply(compiler) {
210
214
  this.propagateWebpackConfig(compiler);
211
- compiler.hooks.make.tapPromise(this.constructor.name, (compilation)=>this.handleMake(compilation, compiler).catch((error)=>{
215
+ compiler.hooks.make.tapPromise(this.constructor.name, (compilation)=>this.handleMake(compiler, compilation).catch((error)=>{
212
216
  compilation.errors.push(error);
213
217
  }));
214
218
  const { PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER } = this.webpack.Compilation;
@@ -221,48 +225,18 @@ class InjectManifest {
221
225
  }));
222
226
  });
223
227
  }
224
- async performChildCompilation(compilation, parentCompiler) {
225
- const outputOptions = {
226
- filename: this.config.swDest
227
- };
228
- const childCompiler = compilation.createChildCompiler(this.constructor.name, outputOptions, []);
229
- childCompiler.context = parentCompiler.context;
230
- childCompiler.inputFileSystem = parentCompiler.inputFileSystem;
231
- childCompiler.outputFileSystem = parentCompiler.outputFileSystem;
232
- if (Array.isArray(this.config.webpackCompilationPlugins)) {
233
- for (const plugin of this.config.webpackCompilationPlugins){
234
- plugin.apply(childCompiler);
235
- }
236
- }
237
- new this.webpack.EntryPlugin(parentCompiler.context, this.config.swSrc, this.constructor.name).apply(childCompiler);
238
- await new Promise((resolve, reject)=>{
239
- childCompiler.runAsChild((error, _entries, childCompilation)=>{
240
- if (error) {
241
- reject(error);
242
- } else {
243
- if (childCompilation?.warnings) {
244
- compilation.warnings.push(...childCompilation.warnings);
245
- }
246
- if (childCompilation?.errors) {
247
- compilation.errors.push(...childCompilation.errors);
248
- }
249
- resolve();
250
- }
251
- });
252
- });
253
- }
254
- addSrcToAssets(compilation, parentCompiler) {
255
- const source = parentCompiler.inputFileSystem.readFileSync(this.config.swSrc);
228
+ addSrcToAssets(compiler, compilation) {
229
+ const source = compiler.inputFileSystem.readFileSync(this.config.swSrc);
256
230
  compilation.emitAsset(this.config.swDest, new this.webpack.sources.RawSource(source));
257
231
  }
258
- async handleMake(compilation, parentCompiler) {
232
+ async handleMake(compiler, compilation) {
259
233
  this.config = await validateInjectManifestOptions(this.config);
260
234
  this.config.swDest = relativeToOutputPath(compilation, this.config.swDest);
261
235
  _generatedAssetNames.add(this.config.swDest);
262
236
  if (this.config.compileSrc) {
263
- await this.performChildCompilation(compilation, parentCompiler);
237
+ await performChildCompilation(compiler, compilation, this.constructor.name, this.config.swSrc, this.config.swDest, this.config.webpackCompilationPlugins);
264
238
  } else {
265
- this.addSrcToAssets(compilation, parentCompiler);
239
+ this.addSrcToAssets(compiler, compilation);
266
240
  if (Array.isArray(this.config.webpackCompilationPlugins) && this.config.webpackCompilationPlugins.length > 0) {
267
241
  compilation.warnings.push(new Error("'compileSrc' is 'false', so the 'webpackCompilationPlugins' option will be ignored."));
268
242
  }
@@ -271,8 +245,7 @@ class InjectManifest {
271
245
  async addAssets(compilation) {
272
246
  const config = Object.assign({}, this.config);
273
247
  const { size, sortedEntries, manifestString } = await this.getManifestEntries(compilation, config);
274
- const absoluteSwSrc = upath.resolve(config.swSrc);
275
- compilation.fileDependencies.add(absoluteSwSrc);
248
+ compilation.fileDependencies.add(path.resolve(config.swSrc));
276
249
  const swAsset = compilation.getAsset(config.swDest);
277
250
  const swAssetString = swAsset.source.source().toString();
278
251
  const globalRegexp = new RegExp(escapeRegExp(config.injectionPoint), "g");
@@ -288,7 +261,7 @@ class InjectManifest {
288
261
  _generatedAssetNames.add(sourcemapAssetName);
289
262
  const sourcemapAsset = compilation.getAsset(sourcemapAssetName);
290
263
  const { source, map } = await replaceAndUpdateSourceMap({
291
- jsFilename: config.swDest,
264
+ jsFilename: toUnix(config.swDest),
292
265
  originalMap: JSON.parse(sourcemapAsset.source.source().toString()),
293
266
  originalSource: swAssetString,
294
267
  replaceString: manifestString,
@@ -49,22 +49,15 @@ export declare class InjectManifest {
49
49
  */
50
50
  apply(compiler: Compiler): void;
51
51
  /**
52
+ * @param compiler The webpack parent compiler.
52
53
  * @param compilation The webpack compilation.
53
- * @param parentCompiler The webpack parent compiler.
54
- *
55
- * @private
56
- */
57
- private performChildCompilation;
58
- /**
59
- * @param compilation The webpack compilation.
60
- * @param parentCompiler The webpack parent compiler.
61
54
  *
62
55
  * @private
63
56
  */
64
57
  private addSrcToAssets;
65
58
  /**
59
+ * @param compiler The webpack parent compiler.
66
60
  * @param compilation The webpack compilation.
67
- * @param parentCompiler The webpack parent compiler.
68
61
  *
69
62
  * @private
70
63
  */
@@ -1 +1 @@
1
- {"version":3,"file":"inject-manifest.d.ts","sourceRoot":"","sources":["../src/inject-manifest.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAe,QAAQ,EAAoC,MAAM,SAAS,CAAC;AACvF,OAAO,KAAK,EAAE,qBAAqB,EAAE,6BAA6B,EAAE,MAAM,gBAAgB,CAAC;AAW3F;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,cAAc;IACzB,SAAS,CAAC,MAAM,EAAE,6BAA6B,CAAC;IAChD,OAAO,CAAC,aAAa,CAAU;IAC/B,OAAO,CAAC,OAAO,CAAiB;IAEhC;;OAEG;gBACS,MAAM,EAAE,qBAAqB;IAOzC;;;;OAIG;IACH,OAAO,CAAC,sBAAsB;IAa9B;;;;OAIG;YACW,kBAAkB;IAwChC;;;;OAIG;IACH,KAAK,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI;IA6B/B;;;;;OAKG;YACW,uBAAuB;IAoCrC;;;;;OAKG;IACH,OAAO,CAAC,cAAc;IAKtB;;;;;OAKG;YACW,UAAU;IAiBxB;;;;OAIG;YACW,SAAS;CAmDxB"}
1
+ {"version":3,"file":"inject-manifest.d.ts","sourceRoot":"","sources":["../src/inject-manifest.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAe,QAAQ,EAAoC,MAAM,SAAS,CAAC;AACvF,OAAO,KAAK,EAAE,qBAAqB,EAAE,6BAA6B,EAAE,MAAM,gBAAgB,CAAC;AAW3F;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,cAAc;IACzB,SAAS,CAAC,MAAM,EAAE,6BAA6B,CAAC;IAChD,OAAO,CAAC,aAAa,CAAU;IAC/B,OAAO,CAAC,OAAO,CAAiB;IAEhC;;OAEG;gBACS,MAAM,EAAE,qBAAqB;IAQzC;;;;OAIG;IACH,OAAO,CAAC,sBAAsB;IAa9B;;;;OAIG;YACW,kBAAkB;IAuChC;;;;OAIG;IACH,KAAK,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI;IA6B/B;;;;;OAKG;IACH,OAAO,CAAC,cAAc;IAKtB;;;;;OAKG;YACW,UAAU;IAwBxB;;;;OAIG;YACW,SAAS;CAiDxB"}
@@ -1,4 +1,4 @@
1
- import type { Compiler, WebpackPluginInstance, default as Webpack } from "webpack";
1
+ import type { Compiler, WebpackPluginInstance } from "webpack";
2
2
  export interface ChildCompilationPluginOptions {
3
3
  src: string;
4
4
  dest: string;
@@ -13,26 +13,12 @@ export declare class ChildCompilationPlugin implements WebpackPluginInstance {
13
13
  src: string;
14
14
  dest: string;
15
15
  plugins: WebpackPluginInstance[] | undefined;
16
- webpack: typeof Webpack;
17
16
  constructor({ src, dest, plugins }: ChildCompilationPluginOptions);
18
- /**
19
- * @param compiler default compiler object passed from webpack
20
- *
21
- * @private
22
- */
23
- private propagateWebpackConfig;
24
17
  /**
25
18
  * @param compiler default compiler object passed from webpack
26
19
  *
27
20
  * @private
28
21
  */
29
22
  apply(compiler: Compiler): void;
30
- /**
31
- * @param compilation The webpack compilation.
32
- * @param parentCompiler The webpack parent compiler.
33
- *
34
- * @private
35
- */
36
- private performChildCompilation;
37
23
  }
38
24
  //# sourceMappingURL=child-compilation-plugin.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"child-compilation-plugin.d.ts","sourceRoot":"","sources":["../../src/lib/child-compilation-plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAe,QAAQ,EAAgB,qBAAqB,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,SAAS,CAAC;AAI9G,MAAM,WAAW,6BAA6B;IAC5C,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,qBAAqB,EAAE,CAAC;CACnC;AAED;;;;GAIG;AACH,qBAAa,sBAAuB,YAAW,qBAAqB;IAClE,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,qBAAqB,EAAE,GAAG,SAAS,CAAC;IAC7C,OAAO,EAAE,OAAO,OAAO,CAAC;gBACZ,EAAE,GAAG,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,6BAA6B;IAMjE;;;;OAIG;IACH,OAAO,CAAC,sBAAsB;IAG9B;;;;OAIG;IACH,KAAK,CAAC,QAAQ,EAAE,QAAQ;IASxB;;;;;OAKG;YACW,uBAAuB;CAiCtC"}
1
+ {"version":3,"file":"child-compilation-plugin.d.ts","sourceRoot":"","sources":["../../src/lib/child-compilation-plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAgB,qBAAqB,EAAE,MAAM,SAAS,CAAC;AAK7E,MAAM,WAAW,6BAA6B;IAC5C,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,qBAAqB,EAAE,CAAC;CACnC;AAED;;;;GAIG;AACH,qBAAa,sBAAuB,YAAW,qBAAqB;IAClE,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,qBAAqB,EAAE,GAAG,SAAS,CAAC;gBACjC,EAAE,GAAG,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,6BAA6B;IAKjE;;;;OAIG;IACH,KAAK,CAAC,QAAQ,EAAE,QAAQ;CAczB"}
@@ -1 +1 @@
1
- {"version":3,"file":"get-script-files-for-chunks.d.ts","sourceRoot":"","sources":["../../src/lib/get-script-files-for-chunks.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,WAAW,EAAgB,MAAM,SAAS,CAAC;AAIzD,eAAO,MAAM,uBAAuB,gBAAiB,WAAW,cAAc,MAAM,EAAE,KAAG,MAAM,EAwB9F,CAAC"}
1
+ {"version":3,"file":"get-script-files-for-chunks.d.ts","sourceRoot":"","sources":["../../src/lib/get-script-files-for-chunks.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,WAAW,EAAgB,MAAM,SAAS,CAAC;AAIzD,eAAO,MAAM,uBAAuB,gBAAiB,WAAW,cAAc,MAAM,EAAE,KAAG,MAAM,EAwB9F,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"get-sourcemap-asset-name.d.ts","sourceRoot":"","sources":["../../src/lib/get-sourcemap-asset-name.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,qBAAqB,gBAAiB,WAAW,cAAc,MAAM,UAAU,MAAM,KAAG,MAAM,GAAG,SAiB7G,CAAC"}
1
+ {"version":3,"file":"get-sourcemap-asset-name.d.ts","sourceRoot":"","sources":["../../src/lib/get-sourcemap-asset-name.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,qBAAqB,gBAAiB,WAAW,cAAc,MAAM,UAAU,MAAM,KAAG,MAAM,GAAG,SAgB7G,CAAC"}
@@ -0,0 +1,16 @@
1
+ import type { Compilation, Compiler } from "webpack";
2
+ import type { WebpackPlugin } from "./types.js";
3
+ /**
4
+ * Perform a child compilation.
5
+ *
6
+ * @param compiler The parent webpack compiler.
7
+ * @param compilation The webpack compilation.
8
+ * @param name The name of the child compiler.
9
+ * @param src The source file. Should be absolute.
10
+ * @param dest The destination file. Should be relative to the compilation.
11
+ * @param plugins Additional webpack plugins.
12
+ *
13
+ * @private
14
+ */
15
+ export declare const performChildCompilation: (compiler: Compiler, compilation: Compilation, name: string, src: string, dest: string, plugins: WebpackPlugin[] | undefined) => Promise<void>;
16
+ //# sourceMappingURL=perform-child-compilation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"perform-child-compilation.d.ts","sourceRoot":"","sources":["../../src/lib/perform-child-compilation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACrD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEhD;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,uBAAuB,aACxB,QAAQ,eACL,WAAW,QAClB,MAAM,OACP,MAAM,QACL,MAAM,WACH,aAAa,EAAE,GAAG,SAAS,kBAuBrC,CAAC"}
@@ -1,12 +1,12 @@
1
1
  import type { Compilation } from "webpack";
2
2
  /**
3
3
  * @param compilation The webpack compilation.
4
- * @param path The original path value.
4
+ * @param originalPath The original path value.
5
5
  *
6
6
  * @returns If path was not absolute, the returns path as-is.
7
7
  * Otherwise, returns path relative to the compilation's output path.
8
8
  *
9
9
  * @private
10
10
  */
11
- export declare const relativeToOutputPath: (compilation: Compilation, path: string) => string;
11
+ export declare const relativeToOutputPath: (compilation: Compilation, originalPath: string) => string;
12
12
  //# sourceMappingURL=relative-to-output-path.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"relative-to-output-path.d.ts","sourceRoot":"","sources":["../../src/lib/relative-to-output-path.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C;;;;;;;;GAQG;AACH,eAAO,MAAM,oBAAoB,gBAAiB,WAAW,QAAQ,MAAM,KAAG,MAQ7E,CAAC"}
1
+ {"version":3,"file":"relative-to-output-path.d.ts","sourceRoot":"","sources":["../../src/lib/relative-to-output-path.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C;;;;;;;;GAQG;AACH,eAAO,MAAM,oBAAoB,gBAAiB,WAAW,gBAAgB,MAAM,KAAG,MAQrF,CAAC"}
@@ -10,8 +10,8 @@ export declare const webpackPartial: z.ZodObject<{
10
10
  excludeChunks?: string[] | undefined;
11
11
  include?: (string | RegExp | ((args_0: any) => boolean))[] | undefined;
12
12
  }, {
13
- chunks?: string[] | undefined;
14
13
  exclude?: (string | RegExp | ((args_0: any) => boolean))[] | undefined;
14
+ chunks?: string[] | undefined;
15
15
  excludeChunks?: string[] | undefined;
16
16
  include?: (string | RegExp | ((args_0: any) => boolean))[] | undefined;
17
17
  }>;
@@ -21,12 +21,12 @@ export declare const injectPartial: z.ZodObject<{
21
21
  webpackCompilationPlugins: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
22
22
  }, "strict", z.ZodTypeAny, {
23
23
  compileSrc: boolean;
24
- swDest?: string | undefined;
25
24
  webpackCompilationPlugins?: any[] | undefined;
25
+ swDest?: string | undefined;
26
26
  }, {
27
27
  compileSrc?: boolean | undefined;
28
- swDest?: string | undefined;
29
28
  webpackCompilationPlugins?: any[] | undefined;
29
+ swDest?: string | undefined;
30
30
  }>;
31
31
  export declare const injectManifestOptions: z.ZodObject<z.objectUtil.extendShape<z.objectUtil.extendShape<z.objectUtil.extendShape<z.objectUtil.extendShape<{
32
32
  additionalPrecacheEntries: z.ZodOptional<z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodObject<{
@@ -157,7 +157,6 @@ export declare const injectManifestOptions: z.ZodObject<z.objectUtil.extendShape
157
157
  chunks?: string[] | undefined;
158
158
  excludeChunks?: string[] | undefined;
159
159
  include?: (string | RegExp | ((args_0: any) => boolean))[] | undefined;
160
- swDest?: string | undefined;
161
160
  webpackCompilationPlugins?: any[] | undefined;
162
161
  additionalPrecacheEntries?: (string | {
163
162
  url: string;
@@ -188,14 +187,14 @@ export declare const injectManifestOptions: z.ZodObject<z.objectUtil.extendShape
188
187
  warnings?: string[] | undefined;
189
188
  }>)[] | undefined;
190
189
  modifyURLPrefix?: Record<string, string> | undefined;
190
+ swDest?: string | undefined;
191
191
  }, {
192
192
  swSrc: string;
193
- chunks?: string[] | undefined;
194
193
  exclude?: (string | RegExp | ((args_0: any) => boolean))[] | undefined;
194
+ chunks?: string[] | undefined;
195
195
  excludeChunks?: string[] | undefined;
196
196
  include?: (string | RegExp | ((args_0: any) => boolean))[] | undefined;
197
197
  compileSrc?: boolean | undefined;
198
- swDest?: string | undefined;
199
198
  webpackCompilationPlugins?: any[] | undefined;
200
199
  additionalPrecacheEntries?: (string | {
201
200
  url: string;
@@ -229,5 +228,6 @@ export declare const injectManifestOptions: z.ZodObject<z.objectUtil.extendShape
229
228
  maximumFileSizeToCacheInBytes?: number | undefined;
230
229
  modifyURLPrefix?: Record<string, string> | undefined;
231
230
  injectionPoint?: string | undefined;
231
+ swDest?: string | undefined;
232
232
  }>;
233
233
  //# sourceMappingURL=schema.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/lib/schema.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;EASmC,CAAC;AAE/D,eAAO,MAAM,aAAa;;;;;;;;;;;;EAMkD,CAAC;AAE7E,eAAO,MAAM,qBAAqB;;;;;;;iBAdL,CAAC;gBACV,CAAC;;;iBACyB,CAAC;gBAAsC,CAAC;;;;;;;;;;;;;iBAiBnC,CAAC;gBACxC,CAAC;;;;iBAAkG,CAAC;gBAAsC,CAAC;;;;;;;;;;;qBAAye,CAAC;oBAA0C,CAAC;;;;qBAAkH,CAAC;oBAA0C,CAAC;;;;;;;qBAAiP,CAAC;oBAA0C,CAAC;;gBAA0D,CAAC;;;;;qBAA6H,CAAC;oBAA0C,CAAC;;gBAA0D,CAAC;;;;;;;;;;;qBAA8Z,CAAC;oBAA0C,CAAC;;;;qBAAkH,CAAC;oBAA0C,CAAC;;;;;;;qBAAiP,CAAC;oBAA0C,CAAC;;gBAA0D,CAAC;;;;;qBAA6H,CAAC;oBAA0C,CAAC;;gBAA0D,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAA3U,CAAC;oBAA0C,CAAC;;;;;;;qBAAhtC,CAAC;oBAA0C,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAA41C,CAAC;oBAA0C,CAAC;;;;;;;qBAAhtC,CAAC;oBAA0C,CAAC;;;;;;;EAD/vC,CAAC"}
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/lib/schema.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;EASmC,CAAC;AAE/D,eAAO,MAAM,aAAa;;;;;;;;;;;;EAMkD,CAAC;AAE7E,eAAO,MAAM,qBAAqB;;;;;;;iBAdL,CAAC;gBACV,CAAC;;;iBACyB,CAAC;gBAAsC,CAAC;;;;;;;;;;;;;iBAiBnC,CAAC;gBACxC,CAAC;;;;iBAAkG,CAAC;gBAAsC,CAAC;;;;;;;;;;;qBAAye,CAAC;oBAA0C,CAAC;;;;qBAAkH,CAAC;oBAA0C,CAAC;;;;;;;qBAAiP,CAAC;oBAA0C,CAAC;;gBAA0D,CAAC;;;;;qBAA6H,CAAC;oBAA0C,CAAC;;gBAA0D,CAAC;;;;;;;;;;;qBAA8Z,CAAC;oBAA0C,CAAC;;;;qBAAkH,CAAC;oBAA0C,CAAC;;;;;;;qBAAiP,CAAC;oBAA0C,CAAC;;gBAA0D,CAAC;;;;;qBAA6H,CAAC;oBAA0C,CAAC;;gBAA0D,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAA3U,CAAC;oBAA0C,CAAC;;;;;;;qBAAhtC,CAAC;oBAA0C,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAA41C,CAAC;oBAA0C,CAAC;;;;;;;qBAAhtC,CAAC;oBAA0C,CAAC;;;;;;;;EAD/vC,CAAC"}
@@ -1,5 +1,6 @@
1
1
  import type { BasePartial, BaseResolved, InjectPartial as BaseInjectPartial, InjectResolved as BaseInjectResolved, OptionalSwDestPartial, OptionalSwDestResolved } from "@serwist/build";
2
2
  import type { Require } from "@serwist/utils";
3
+ import type { WebpackPluginFunction, WebpackPluginInstance } from "webpack";
3
4
  export interface WebpackPartial {
4
5
  /**
5
6
  * One or more chunk names whose corresponding output files should be included
@@ -44,11 +45,12 @@ export interface InjectPartial {
44
45
  * Optional webpack plugins that will be used when compiling the `swSrc`
45
46
  * file. Only valid if `compileSrc` is `true`.
46
47
  */
47
- webpackCompilationPlugins?: any[];
48
+ webpackCompilationPlugins?: WebpackPlugin[];
48
49
  }
49
50
  export type InjectResolved = Require<InjectPartial, "compileSrc">;
50
51
  export interface InjectManifestOptions extends BasePartial, WebpackPartial, BaseInjectPartial, OptionalSwDestPartial, InjectPartial {
51
52
  }
52
53
  export interface InjectManifestOptionsComplete extends BaseResolved, WebpackResolved, BaseInjectResolved, OptionalSwDestResolved, InjectResolved {
53
54
  }
55
+ export type WebpackPlugin = WebpackPluginFunction | WebpackPluginInstance;
54
56
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lib/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,YAAY,EACZ,aAAa,IAAI,iBAAiB,EAClC,cAAc,IAAI,kBAAkB,EACpC,qBAAqB,EACrB,sBAAsB,EACvB,MAAM,gBAAgB,CAAC;AACxB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAE9C,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB;;;;;;;;;OASG;IACH,OAAO,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC,CAAC,EAAE,CAAC;IACzD;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB;;;;;OAKG;IACH,OAAO,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC,CAAC,EAAE,CAAC;CAC1D;AAED,MAAM,MAAM,eAAe,GAAG,OAAO,CAAC,cAAc,EAAE,SAAS,CAAC,CAAC;AAEjE,MAAM,WAAW,aAAa;IAC5B;;;;;;OAMG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IAIrB;;;OAGG;IACH,yBAAyB,CAAC,EAAE,GAAG,EAAE,CAAC;CACnC;AAED,MAAM,MAAM,cAAc,GAAG,OAAO,CAAC,aAAa,EAAE,YAAY,CAAC,CAAC;AAElE,MAAM,WAAW,qBAAsB,SAAQ,WAAW,EAAE,cAAc,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,aAAa;CAAG;AAEtI,MAAM,WAAW,6BAA8B,SAAQ,YAAY,EAAE,eAAe,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,cAAc;CAAG"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lib/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,YAAY,EACZ,aAAa,IAAI,iBAAiB,EAClC,cAAc,IAAI,kBAAkB,EACpC,qBAAqB,EACrB,sBAAsB,EACvB,MAAM,gBAAgB,CAAC;AACxB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,KAAK,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AAE5E,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB;;;;;;;;;OASG;IACH,OAAO,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC,CAAC,EAAE,CAAC;IACzD;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB;;;;;OAKG;IACH,OAAO,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC,CAAC,EAAE,CAAC;CAC1D;AAED,MAAM,MAAM,eAAe,GAAG,OAAO,CAAC,cAAc,EAAE,SAAS,CAAC,CAAC;AAEjE,MAAM,WAAW,aAAa;IAC5B;;;;;;OAMG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IAIrB;;;OAGG;IACH,yBAAyB,CAAC,EAAE,aAAa,EAAE,CAAC;CAC7C;AAED,MAAM,MAAM,cAAc,GAAG,OAAO,CAAC,aAAa,EAAE,YAAY,CAAC,CAAC;AAElE,MAAM,WAAW,qBAAsB,SAAQ,WAAW,EAAE,cAAc,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,aAAa;CAAG;AAEtI,MAAM,WAAW,6BAA8B,SAAQ,YAAY,EAAE,eAAe,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,cAAc;CAAG;AAEnJ,MAAM,MAAM,aAAa,GAAG,qBAAqB,GAAG,qBAAqB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serwist/webpack-plugin",
3
- "version": "9.0.5",
3
+ "version": "9.0.7",
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": [
@@ -22,7 +22,10 @@
22
22
  "engines": {
23
23
  "node": ">=18.0.0"
24
24
  },
25
- "author": "Google's Web DevRel Team, Serwist's Team",
25
+ "author": "Google's Web DevRel Team",
26
+ "contributors": [
27
+ "Serwist <ducanh2912.rusty@gmail.com> (https://serwist.pages.dev/)"
28
+ ],
26
29
  "license": "MIT",
27
30
  "repository": "https://github.com/serwist/serwist",
28
31
  "bugs": "https://github.com/serwist/serwist/issues",
@@ -56,18 +59,17 @@
56
59
  },
57
60
  "dependencies": {
58
61
  "pretty-bytes": "6.1.1",
59
- "upath": "2.0.1",
60
62
  "zod": "3.23.8",
61
- "@serwist/build": "9.0.5"
63
+ "@serwist/build": "9.0.7"
62
64
  },
63
65
  "devDependencies": {
64
- "@types/node": "20.14.10",
66
+ "@types/node": "22.5.0",
65
67
  "@types/webpack": "5.28.5",
66
- "rollup": "4.18.1",
67
- "typescript": "5.5.3",
68
+ "rollup": "4.21.0",
69
+ "typescript": "5.5.4",
68
70
  "webpack": "5.93.0",
69
- "@serwist/configs": "9.0.5",
70
- "@serwist/utils": "9.0.5"
71
+ "@serwist/configs": "9.0.7",
72
+ "@serwist/utils": "9.0.7"
71
73
  },
72
74
  "peerDependencies": {
73
75
  "typescript": ">=5.0.0",
@@ -1,13 +1,14 @@
1
+ import path from "node:path";
1
2
  import { escapeRegExp, replaceAndUpdateSourceMap, stringify } from "@serwist/build";
3
+ import { toUnix } from "@serwist/utils";
2
4
  import prettyBytes from "pretty-bytes";
3
- import upath from "upath";
4
5
  import type { Compilation, Compiler, WebpackError, default as Webpack } from "webpack";
5
6
  import type { InjectManifestOptions, InjectManifestOptionsComplete } from "./lib/types.js";
6
7
  import { validateInjectManifestOptions } from "./lib/validator.js";
7
-
8
8
  import { getManifestEntriesFromCompilation } from "./lib/get-manifest-entries-from-compilation.js";
9
9
  import { getSourcemapAssetName } from "./lib/get-sourcemap-asset-name.js";
10
10
  import { relativeToOutputPath } from "./lib/relative-to-output-path.js";
11
+ import { performChildCompilation } from "./lib/perform-child-compilation.js";
11
12
 
12
13
  // Used to keep track of swDest files written by *any* instance of this plugin.
13
14
  // See https://github.com/GoogleChrome/workbox/issues/2181
@@ -44,7 +45,8 @@ export class InjectManifest {
44
45
  * Creates an instance of InjectManifest.
45
46
  */
46
47
  constructor(config: InjectManifestOptions) {
47
- // We are essentially lying to TypeScript.
48
+ // We are essentially lying to TypeScript. When `handleMake`
49
+ // is called, `this.config` will be replaced by a validated config.
48
50
  this.config = config as InjectManifestOptionsComplete;
49
51
  this.alreadyCalled = false;
50
52
  this.webpack = null!;
@@ -58,7 +60,7 @@ export class InjectManifest {
58
60
  private propagateWebpackConfig(compiler: Compiler): void {
59
61
  this.webpack = compiler.webpack;
60
62
 
61
- const parsedSwSrc = upath.parse(this.config.swSrc);
63
+ const parsedSwSrc = path.parse(this.config.swSrc);
62
64
  // Because this.config is listed last, properties that are already set
63
65
  // there take precedence over derived properties from the compiler.
64
66
  this.config = {
@@ -95,8 +97,7 @@ export class InjectManifest {
95
97
 
96
98
  // Ensure that we don't precache any of the assets generated by *any*
97
99
  // instance of this plugin.
98
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
99
- config.exclude!.push(({ asset }) => _generatedAssetNames.has(asset.name));
100
+ config.exclude.push(({ asset }) => _generatedAssetNames.has(asset.name));
100
101
 
101
102
  const { size, sortedEntries } = await getManifestEntriesFromCompilation(compilation, config);
102
103
 
@@ -122,7 +123,7 @@ export class InjectManifest {
122
123
  this.propagateWebpackConfig(compiler);
123
124
 
124
125
  compiler.hooks.make.tapPromise(this.constructor.name, (compilation) =>
125
- this.handleMake(compilation, compiler).catch((error: WebpackError) => {
126
+ this.handleMake(compiler, compilation).catch((error: WebpackError) => {
126
127
  compilation.errors.push(error);
127
128
  }),
128
129
  );
@@ -148,73 +149,38 @@ export class InjectManifest {
148
149
  }
149
150
 
150
151
  /**
152
+ * @param compiler The webpack parent compiler.
151
153
  * @param compilation The webpack compilation.
152
- * @param parentCompiler The webpack parent compiler.
153
154
  *
154
155
  * @private
155
156
  */
156
- private async performChildCompilation(compilation: Compilation, parentCompiler: Compiler): Promise<void> {
157
- const outputOptions: Parameters<Compilation["createChildCompiler"]>["1"] = {
158
- filename: this.config.swDest,
159
- };
160
-
161
- const childCompiler = compilation.createChildCompiler(this.constructor.name, outputOptions, []);
162
-
163
- childCompiler.context = parentCompiler.context;
164
- childCompiler.inputFileSystem = parentCompiler.inputFileSystem;
165
- childCompiler.outputFileSystem = parentCompiler.outputFileSystem;
166
-
167
- if (Array.isArray(this.config.webpackCompilationPlugins)) {
168
- for (const plugin of this.config.webpackCompilationPlugins) {
169
- plugin.apply(childCompiler);
170
- }
171
- }
172
-
173
- new this.webpack.EntryPlugin(parentCompiler.context, this.config.swSrc, this.constructor.name).apply(childCompiler);
174
-
175
- await new Promise<void>((resolve, reject) => {
176
- childCompiler.runAsChild((error, _entries, childCompilation) => {
177
- if (error) {
178
- reject(error);
179
- } else {
180
- if (childCompilation?.warnings) {
181
- compilation.warnings.push(...childCompilation.warnings);
182
- }
183
- if (childCompilation?.errors) {
184
- compilation.errors.push(...childCompilation.errors);
185
- }
186
- resolve();
187
- }
188
- });
189
- });
190
- }
191
-
192
- /**
193
- * @param compilation The webpack compilation.
194
- * @param parentCompiler The webpack parent compiler.
195
- *
196
- * @private
197
- */
198
- private addSrcToAssets(compilation: Compilation, parentCompiler: Compiler): void {
199
- const source = (parentCompiler.inputFileSystem as any).readFileSync(this.config.swSrc);
157
+ private addSrcToAssets(compiler: Compiler, compilation: Compilation): void {
158
+ const source = compiler.inputFileSystem!.readFileSync!(this.config.swSrc);
200
159
  compilation.emitAsset(this.config.swDest!, new this.webpack.sources.RawSource(source));
201
160
  }
202
161
 
203
162
  /**
163
+ * @param compiler The webpack parent compiler.
204
164
  * @param compilation The webpack compilation.
205
- * @param parentCompiler The webpack parent compiler.
206
165
  *
207
166
  * @private
208
167
  */
209
- private async handleMake(compilation: Compilation, parentCompiler: Compiler): Promise<void> {
168
+ private async handleMake(compiler: Compiler, compilation: Compilation): Promise<void> {
210
169
  this.config = await validateInjectManifestOptions(this.config);
211
170
  this.config.swDest = relativeToOutputPath(compilation, this.config.swDest!);
212
171
  _generatedAssetNames.add(this.config.swDest);
213
172
 
214
173
  if (this.config.compileSrc) {
215
- await this.performChildCompilation(compilation, parentCompiler);
174
+ await performChildCompilation(
175
+ compiler,
176
+ compilation,
177
+ this.constructor.name,
178
+ this.config.swSrc,
179
+ this.config.swDest,
180
+ this.config.webpackCompilationPlugins,
181
+ );
216
182
  } else {
217
- this.addSrcToAssets(compilation, parentCompiler);
183
+ this.addSrcToAssets(compiler, compilation);
218
184
  // This used to be a fatal error, but just warn at runtime because we
219
185
  // can't validate it easily.
220
186
  if (Array.isArray(this.config.webpackCompilationPlugins) && this.config.webpackCompilationPlugins.length > 0) {
@@ -234,10 +200,10 @@ export class InjectManifest {
234
200
  const { size, sortedEntries, manifestString } = await this.getManifestEntries(compilation, config);
235
201
 
236
202
  // See https://webpack.js.org/contribute/plugin-patterns/#monitoring-the-watch-graph
237
- const absoluteSwSrc = upath.resolve(config.swSrc);
238
- compilation.fileDependencies.add(absoluteSwSrc);
203
+ compilation.fileDependencies.add(path.resolve(config.swSrc));
239
204
 
240
205
  const swAsset = compilation.getAsset(config.swDest!);
206
+
241
207
  const swAssetString = swAsset!.source.source().toString();
242
208
 
243
209
  const globalRegexp = new RegExp(escapeRegExp(config.injectionPoint), "g");
@@ -258,20 +224,18 @@ export class InjectManifest {
258
224
  _generatedAssetNames.add(sourcemapAssetName);
259
225
  const sourcemapAsset = compilation.getAsset(sourcemapAssetName);
260
226
  const { source, map } = await replaceAndUpdateSourceMap({
261
- jsFilename: config.swDest!,
262
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
227
+ jsFilename: toUnix(config.swDest!),
263
228
  originalMap: JSON.parse(sourcemapAsset!.source.source().toString()),
264
229
  originalSource: swAssetString,
265
230
  replaceString: manifestString,
266
- searchString: config.injectionPoint!,
231
+ searchString: config.injectionPoint,
267
232
  });
268
-
269
233
  compilation.updateAsset(sourcemapAssetName, new this.webpack.sources.RawSource(map));
270
234
  compilation.updateAsset(config.swDest!, new this.webpack.sources.RawSource(source));
271
235
  } else {
272
236
  // If there's no sourcemap associated with swDest, a simple string
273
237
  // replacement will suffice.
274
- compilation.updateAsset(config.swDest!, new this.webpack.sources.RawSource(swAssetString.replace(config.injectionPoint!, manifestString)));
238
+ compilation.updateAsset(config.swDest!, new this.webpack.sources.RawSource(swAssetString.replace(config.injectionPoint, manifestString)));
275
239
  }
276
240
 
277
241
  if (compilation.getLogger) {
@@ -1,6 +1,7 @@
1
- import type { Compilation, Compiler, WebpackError, WebpackPluginInstance, default as Webpack } from "webpack";
1
+ import type { Compiler, WebpackError, WebpackPluginInstance } from "webpack";
2
2
 
3
3
  import { relativeToOutputPath } from "./relative-to-output-path.js";
4
+ import { performChildCompilation } from "./perform-child-compilation.js";
4
5
 
5
6
  export interface ChildCompilationPluginOptions {
6
7
  src: string;
@@ -17,20 +18,10 @@ export class ChildCompilationPlugin implements WebpackPluginInstance {
17
18
  src: string;
18
19
  dest: string;
19
20
  plugins: WebpackPluginInstance[] | undefined;
20
- webpack: typeof Webpack;
21
21
  constructor({ src, dest, plugins }: ChildCompilationPluginOptions) {
22
22
  this.src = src;
23
23
  this.dest = dest;
24
24
  this.plugins = plugins;
25
- this.webpack = null!;
26
- }
27
- /**
28
- * @param compiler default compiler object passed from webpack
29
- *
30
- * @private
31
- */
32
- private propagateWebpackConfig(compiler: Compiler): void {
33
- this.webpack = compiler.webpack;
34
25
  }
35
26
  /**
36
27
  * @param compiler default compiler object passed from webpack
@@ -38,51 +29,17 @@ export class ChildCompilationPlugin implements WebpackPluginInstance {
38
29
  * @private
39
30
  */
40
31
  apply(compiler: Compiler) {
41
- this.propagateWebpackConfig(compiler);
42
-
43
32
  compiler.hooks.make.tapPromise(this.constructor.name, (compilation) =>
44
- this.performChildCompilation(compilation, compiler).catch((error: WebpackError) => {
33
+ performChildCompilation(
34
+ compiler,
35
+ compilation,
36
+ this.constructor.name,
37
+ this.src,
38
+ relativeToOutputPath(compilation, this.dest),
39
+ this.plugins,
40
+ ).catch((error: WebpackError) => {
45
41
  compilation.errors.push(error);
46
42
  }),
47
43
  );
48
44
  }
49
- /**
50
- * @param compilation The webpack compilation.
51
- * @param parentCompiler The webpack parent compiler.
52
- *
53
- * @private
54
- */
55
- private async performChildCompilation(compilation: Compilation, parentCompiler: Compiler): Promise<void> {
56
- const resolvedDest = relativeToOutputPath(compilation, this.dest);
57
- const outputOptions: Parameters<Compilation["createChildCompiler"]>["1"] = {
58
- filename: resolvedDest,
59
- };
60
-
61
- const childCompiler = compilation.createChildCompiler(this.constructor.name, outputOptions, []);
62
- childCompiler.options.target = "webworker";
63
- childCompiler.context = parentCompiler.context;
64
- childCompiler.inputFileSystem = parentCompiler.inputFileSystem;
65
- childCompiler.outputFileSystem = parentCompiler.outputFileSystem;
66
-
67
- if (this.plugins !== undefined) {
68
- for (const plugin of this.plugins) {
69
- plugin?.apply(childCompiler);
70
- }
71
- }
72
-
73
- new this.webpack.EntryPlugin(parentCompiler.context, this.src, this.constructor.name).apply(childCompiler);
74
-
75
- await new Promise<void>((resolve, reject) => {
76
- childCompiler.runAsChild((error, _entries, childCompilation) => {
77
- if (error) {
78
- reject(error);
79
- } else {
80
- compilation.warnings = compilation.warnings.concat(childCompilation?.warnings ?? []);
81
- compilation.errors = compilation.errors.concat(childCompilation?.errors ?? []);
82
-
83
- resolve();
84
- }
85
- });
86
- });
87
- }
88
45
  }
@@ -5,8 +5,7 @@
5
5
  license that can be found in the LICENSE file or at
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
-
9
- import upath from "upath";
8
+ import path from "node:path";
10
9
  import type { Compilation, WebpackError } from "webpack";
11
10
 
12
11
  import { resolveWebpackURL } from "./resolve-webpack-url.js";
@@ -21,7 +20,7 @@ export const getScriptFilesForChunks = (compilation: Compilation, chunkNames: st
21
20
  if (chunk) {
22
21
  for (const file of chunk?.files ?? []) {
23
22
  // See https://github.com/GoogleChrome/workbox/issues/2161
24
- if (upath.extname(file) === ".js") {
23
+ if (path.extname(file) === ".js") {
25
24
  scriptFiles.add(resolveWebpackURL(publicPath as string, file));
26
25
  }
27
26
  }
@@ -5,9 +5,8 @@
5
5
  license that can be found in the LICENSE file or at
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
-
8
+ import path from "node:path";
9
9
  import { getSourceMapURL } from "@serwist/build";
10
- import upath from "upath";
11
10
  import type { Compilation } from "webpack";
12
11
 
13
12
  /**
@@ -35,9 +34,8 @@ export const getSourcemapAssetName = (compilation: Compilation, swContents: stri
35
34
  // This *might* not be a valid asset if the sourcemap URL that was found
36
35
  // was added by another module incidentally.
37
36
  // See https://github.com/GoogleChrome/workbox/issues/2250
38
- const swAssetDirname = upath.dirname(swDest);
39
- const sourcemapURLAssetName = upath.normalize(upath.join(swAssetDirname, url));
40
-
37
+ const swAssetDirname = path.dirname(swDest);
38
+ const sourcemapURLAssetName = path.normalize(path.join(swAssetDirname, url));
41
39
  // Not sure if there's a better way to check for asset existence?
42
40
  if (compilation.getAsset(sourcemapURLAssetName)) {
43
41
  return sourcemapURLAssetName;
@@ -0,0 +1,45 @@
1
+ import type { Compilation, Compiler } from "webpack";
2
+ import type { WebpackPlugin } from "./types.js";
3
+
4
+ /**
5
+ * Perform a child compilation.
6
+ *
7
+ * @param compiler The parent webpack compiler.
8
+ * @param compilation The webpack compilation.
9
+ * @param name The name of the child compiler.
10
+ * @param src The source file. Should be absolute.
11
+ * @param dest The destination file. Should be relative to the compilation.
12
+ * @param plugins Additional webpack plugins.
13
+ *
14
+ * @private
15
+ */
16
+ export const performChildCompilation = async (
17
+ compiler: Compiler,
18
+ compilation: Compilation,
19
+ name: string,
20
+ src: string,
21
+ dest: string,
22
+ plugins: WebpackPlugin[] | undefined,
23
+ ) => {
24
+ const childCompiler = compilation.createChildCompiler(name, { filename: dest }, plugins);
25
+
26
+ new compiler.webpack.webworker.WebWorkerTemplatePlugin().apply(childCompiler);
27
+
28
+ new compiler.webpack.EntryPlugin(compiler.context, src, name).apply(childCompiler);
29
+
30
+ await new Promise<void>((resolve, reject) => {
31
+ childCompiler.runAsChild((error, _entries, childCompilation) => {
32
+ if (error) {
33
+ reject(error);
34
+ } else {
35
+ if (childCompilation?.warnings) {
36
+ compilation.warnings.push(...childCompilation.warnings);
37
+ }
38
+ if (childCompilation?.errors) {
39
+ compilation.errors.push(...childCompilation.errors);
40
+ }
41
+ resolve();
42
+ }
43
+ });
44
+ });
45
+ };
@@ -5,25 +5,24 @@
5
5
  license that can be found in the LICENSE file or at
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
-
9
- import upath from "upath";
8
+ import path from "node:path";
10
9
  import type { Compilation } from "webpack";
11
10
 
12
11
  /**
13
12
  * @param compilation The webpack compilation.
14
- * @param path The original path value.
13
+ * @param originalPath The original path value.
15
14
  *
16
15
  * @returns If path was not absolute, the returns path as-is.
17
16
  * Otherwise, returns path relative to the compilation's output path.
18
17
  *
19
18
  * @private
20
19
  */
21
- export const relativeToOutputPath = (compilation: Compilation, path: string): string => {
20
+ export const relativeToOutputPath = (compilation: Compilation, originalPath: string): string => {
22
21
  // See https://github.com/jantimon/html-webpack-plugin/pull/266/files#diff-168726dbe96b3ce427e7fedce31bb0bcR38
23
- if (upath.resolve(path) === upath.normalize(path)) {
24
- return upath.relative(compilation.options.output.path!, path);
22
+ if (path.resolve(originalPath) === path.normalize(originalPath)) {
23
+ return path.relative(compilation.options.output.path!, originalPath);
25
24
  }
26
25
 
27
26
  // Otherwise, return swDest as-is.
28
- return path;
27
+ return originalPath;
29
28
  };
package/src/lib/types.ts CHANGED
@@ -7,6 +7,7 @@ import type {
7
7
  OptionalSwDestResolved,
8
8
  } from "@serwist/build";
9
9
  import type { Require } from "@serwist/utils";
10
+ import type { WebpackPluginFunction, WebpackPluginInstance } from "webpack";
10
11
 
11
12
  export interface WebpackPartial {
12
13
  /**
@@ -57,7 +58,7 @@ export interface InjectPartial {
57
58
  * Optional webpack plugins that will be used when compiling the `swSrc`
58
59
  * file. Only valid if `compileSrc` is `true`.
59
60
  */
60
- webpackCompilationPlugins?: any[];
61
+ webpackCompilationPlugins?: WebpackPlugin[];
61
62
  }
62
63
 
63
64
  export type InjectResolved = Require<InjectPartial, "compileSrc">;
@@ -65,3 +66,5 @@ export type InjectResolved = Require<InjectPartial, "compileSrc">;
65
66
  export interface InjectManifestOptions extends BasePartial, WebpackPartial, BaseInjectPartial, OptionalSwDestPartial, InjectPartial {}
66
67
 
67
68
  export interface InjectManifestOptionsComplete extends BaseResolved, WebpackResolved, BaseInjectResolved, OptionalSwDestResolved, InjectResolved {}
69
+
70
+ export type WebpackPlugin = WebpackPluginFunction | WebpackPluginInstance;
@@ -1,10 +0,0 @@
1
- import upath from 'upath';
2
-
3
- const relativeToOutputPath = (compilation, path)=>{
4
- if (upath.resolve(path) === upath.normalize(path)) {
5
- return upath.relative(compilation.options.output.path, path);
6
- }
7
- return path;
8
- };
9
-
10
- export { relativeToOutputPath as r };