@serwist/webpack-plugin 9.0.6 → 9.0.8
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/perform-child-compilation.js +33 -0
- package/dist/index.internal.js +3 -37
- package/dist/index.js +17 -36
- package/dist/inject-manifest.d.ts +2 -9
- package/dist/inject-manifest.d.ts.map +1 -1
- package/dist/lib/child-compilation-plugin.d.ts +1 -15
- package/dist/lib/child-compilation-plugin.d.ts.map +1 -1
- package/dist/lib/get-script-files-for-chunks.d.ts.map +1 -1
- package/dist/lib/get-sourcemap-asset-name.d.ts.map +1 -1
- package/dist/lib/perform-child-compilation.d.ts +16 -0
- package/dist/lib/perform-child-compilation.d.ts.map +1 -0
- package/dist/lib/relative-to-output-path.d.ts +2 -2
- package/dist/lib/relative-to-output-path.d.ts.map +1 -1
- package/dist/lib/schema.d.ts +6 -6
- package/dist/lib/schema.d.ts.map +1 -1
- package/dist/lib/types.d.ts +2 -1
- package/dist/lib/types.d.ts.map +1 -1
- package/package.json +8 -9
- package/src/inject-manifest.ts +27 -57
- package/src/lib/child-compilation-plugin.ts +10 -53
- package/src/lib/get-script-files-for-chunks.ts +2 -3
- package/src/lib/get-sourcemap-asset-name.ts +3 -5
- package/src/lib/perform-child-compilation.ts +45 -0
- package/src/lib/relative-to-output-path.ts +6 -7
- package/src/lib/types.ts +3 -1
- package/dist/chunks/relative-to-output-path.js +0 -10
|
@@ -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 };
|
package/dist/index.internal.js
CHANGED
|
@@ -1,54 +1,20 @@
|
|
|
1
|
-
import { r as relativeToOutputPath } from './chunks/
|
|
2
|
-
import '
|
|
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.
|
|
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/
|
|
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 =
|
|
155
|
-
const sourcemapURLAssetName =
|
|
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 =
|
|
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(
|
|
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,40 +225,18 @@ class InjectManifest {
|
|
|
221
225
|
}));
|
|
222
226
|
});
|
|
223
227
|
}
|
|
224
|
-
|
|
225
|
-
const
|
|
226
|
-
filename: this.config.swDest
|
|
227
|
-
}, this.config.webpackCompilationPlugins);
|
|
228
|
-
new this.webpack.webworker.WebWorkerTemplatePlugin().apply(childCompiler);
|
|
229
|
-
new this.webpack.EntryPlugin(parentCompiler.context, this.config.swSrc, this.constructor.name).apply(childCompiler);
|
|
230
|
-
await new Promise((resolve, reject)=>{
|
|
231
|
-
childCompiler.runAsChild((error, _entries, childCompilation)=>{
|
|
232
|
-
if (error) {
|
|
233
|
-
reject(error);
|
|
234
|
-
} else {
|
|
235
|
-
if (childCompilation?.warnings) {
|
|
236
|
-
compilation.warnings.push(...childCompilation.warnings);
|
|
237
|
-
}
|
|
238
|
-
if (childCompilation?.errors) {
|
|
239
|
-
compilation.errors.push(...childCompilation.errors);
|
|
240
|
-
}
|
|
241
|
-
resolve();
|
|
242
|
-
}
|
|
243
|
-
});
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
addSrcToAssets(compilation, parentCompiler) {
|
|
247
|
-
const source = parentCompiler.inputFileSystem.readFileSync(this.config.swSrc);
|
|
228
|
+
addSrcToAssets(compiler, compilation) {
|
|
229
|
+
const source = compiler.inputFileSystem.readFileSync(this.config.swSrc);
|
|
248
230
|
compilation.emitAsset(this.config.swDest, new this.webpack.sources.RawSource(source));
|
|
249
231
|
}
|
|
250
|
-
async handleMake(
|
|
232
|
+
async handleMake(compiler, compilation) {
|
|
251
233
|
this.config = await validateInjectManifestOptions(this.config);
|
|
252
234
|
this.config.swDest = relativeToOutputPath(compilation, this.config.swDest);
|
|
253
235
|
_generatedAssetNames.add(this.config.swDest);
|
|
254
236
|
if (this.config.compileSrc) {
|
|
255
|
-
await
|
|
237
|
+
await performChildCompilation(compiler, compilation, this.constructor.name, this.config.swSrc, this.config.swDest, this.config.webpackCompilationPlugins);
|
|
256
238
|
} else {
|
|
257
|
-
this.addSrcToAssets(
|
|
239
|
+
this.addSrcToAssets(compiler, compilation);
|
|
258
240
|
if (Array.isArray(this.config.webpackCompilationPlugins) && this.config.webpackCompilationPlugins.length > 0) {
|
|
259
241
|
compilation.warnings.push(new Error("'compileSrc' is 'false', so the 'webpackCompilationPlugins' option will be ignored."));
|
|
260
242
|
}
|
|
@@ -263,8 +245,7 @@ class InjectManifest {
|
|
|
263
245
|
async addAssets(compilation) {
|
|
264
246
|
const config = Object.assign({}, this.config);
|
|
265
247
|
const { size, sortedEntries, manifestString } = await this.getManifestEntries(compilation, config);
|
|
266
|
-
|
|
267
|
-
compilation.fileDependencies.add(absoluteSwSrc);
|
|
248
|
+
compilation.fileDependencies.add(path.resolve(config.swSrc));
|
|
268
249
|
const swAsset = compilation.getAsset(config.swDest);
|
|
269
250
|
const swAssetString = swAsset.source.source().toString();
|
|
270
251
|
const globalRegexp = new RegExp(escapeRegExp(config.injectionPoint), "g");
|
|
@@ -280,7 +261,7 @@ class InjectManifest {
|
|
|
280
261
|
_generatedAssetNames.add(sourcemapAssetName);
|
|
281
262
|
const sourcemapAsset = compilation.getAsset(sourcemapAssetName);
|
|
282
263
|
const { source, map } = await replaceAndUpdateSourceMap({
|
|
283
|
-
jsFilename: config.swDest,
|
|
264
|
+
jsFilename: toUnix(config.swDest),
|
|
284
265
|
originalMap: JSON.parse(sourcemapAsset.source.source().toString()),
|
|
285
266
|
originalSource: swAssetString,
|
|
286
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":"
|
|
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
|
|
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,
|
|
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":"
|
|
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":"
|
|
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
|
|
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,
|
|
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":"
|
|
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"}
|
package/dist/lib/schema.d.ts
CHANGED
|
@@ -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
|
package/dist/lib/schema.d.ts.map
CHANGED
|
@@ -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
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/lib/schema.ts"],"names":[],"mappings":"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"}
|
package/dist/lib/types.d.ts
CHANGED
|
@@ -45,11 +45,12 @@ export interface InjectPartial {
|
|
|
45
45
|
* Optional webpack plugins that will be used when compiling the `swSrc`
|
|
46
46
|
* file. Only valid if `compileSrc` is `true`.
|
|
47
47
|
*/
|
|
48
|
-
webpackCompilationPlugins?:
|
|
48
|
+
webpackCompilationPlugins?: WebpackPlugin[];
|
|
49
49
|
}
|
|
50
50
|
export type InjectResolved = Require<InjectPartial, "compileSrc">;
|
|
51
51
|
export interface InjectManifestOptions extends BasePartial, WebpackPartial, BaseInjectPartial, OptionalSwDestPartial, InjectPartial {
|
|
52
52
|
}
|
|
53
53
|
export interface InjectManifestOptionsComplete extends BaseResolved, WebpackResolved, BaseInjectResolved, OptionalSwDestResolved, InjectResolved {
|
|
54
54
|
}
|
|
55
|
+
export type WebpackPlugin = WebpackPluginFunction | WebpackPluginInstance;
|
|
55
56
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/lib/types.d.ts.map
CHANGED
|
@@ -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;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,
|
|
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.
|
|
3
|
+
"version": "9.0.8",
|
|
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": [
|
|
@@ -59,18 +59,17 @@
|
|
|
59
59
|
},
|
|
60
60
|
"dependencies": {
|
|
61
61
|
"pretty-bytes": "6.1.1",
|
|
62
|
-
"upath": "2.0.1",
|
|
63
62
|
"zod": "3.23.8",
|
|
64
|
-
"@serwist/build": "9.0.
|
|
63
|
+
"@serwist/build": "9.0.8"
|
|
65
64
|
},
|
|
66
65
|
"devDependencies": {
|
|
67
|
-
"@types/node": "22.5.
|
|
66
|
+
"@types/node": "22.5.5",
|
|
68
67
|
"@types/webpack": "5.28.5",
|
|
69
|
-
"rollup": "4.21.
|
|
70
|
-
"typescript": "5.
|
|
71
|
-
"webpack": "5.
|
|
72
|
-
"@serwist/
|
|
73
|
-
"@serwist/
|
|
68
|
+
"rollup": "4.21.3",
|
|
69
|
+
"typescript": "5.6.2",
|
|
70
|
+
"webpack": "5.94.0",
|
|
71
|
+
"@serwist/configs": "9.0.8",
|
|
72
|
+
"@serwist/utils": "9.0.8"
|
|
74
73
|
},
|
|
75
74
|
"peerDependencies": {
|
|
76
75
|
"typescript": ">=5.0.0",
|
package/src/inject-manifest.ts
CHANGED
|
@@ -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 =
|
|
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
|
-
|
|
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(
|
|
126
|
+
this.handleMake(compiler, compilation).catch((error: WebpackError) => {
|
|
126
127
|
compilation.errors.push(error);
|
|
127
128
|
}),
|
|
128
129
|
);
|
|
@@ -148,67 +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
|
|
157
|
-
const
|
|
158
|
-
this.constructor.name,
|
|
159
|
-
{
|
|
160
|
-
filename: this.config.swDest,
|
|
161
|
-
},
|
|
162
|
-
this.config.webpackCompilationPlugins,
|
|
163
|
-
);
|
|
164
|
-
|
|
165
|
-
new this.webpack.webworker.WebWorkerTemplatePlugin().apply(childCompiler);
|
|
166
|
-
|
|
167
|
-
new this.webpack.EntryPlugin(parentCompiler.context, this.config.swSrc, this.constructor.name).apply(childCompiler);
|
|
168
|
-
|
|
169
|
-
await new Promise<void>((resolve, reject) => {
|
|
170
|
-
childCompiler.runAsChild((error, _entries, childCompilation) => {
|
|
171
|
-
if (error) {
|
|
172
|
-
reject(error);
|
|
173
|
-
} else {
|
|
174
|
-
if (childCompilation?.warnings) {
|
|
175
|
-
compilation.warnings.push(...childCompilation.warnings);
|
|
176
|
-
}
|
|
177
|
-
if (childCompilation?.errors) {
|
|
178
|
-
compilation.errors.push(...childCompilation.errors);
|
|
179
|
-
}
|
|
180
|
-
resolve();
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* @param compilation The webpack compilation.
|
|
188
|
-
* @param parentCompiler The webpack parent compiler.
|
|
189
|
-
*
|
|
190
|
-
* @private
|
|
191
|
-
*/
|
|
192
|
-
private addSrcToAssets(compilation: Compilation, parentCompiler: Compiler): void {
|
|
193
|
-
const source = parentCompiler.inputFileSystem!.readFileSync!(this.config.swSrc);
|
|
157
|
+
private addSrcToAssets(compiler: Compiler, compilation: Compilation): void {
|
|
158
|
+
const source = compiler.inputFileSystem!.readFileSync!(this.config.swSrc);
|
|
194
159
|
compilation.emitAsset(this.config.swDest!, new this.webpack.sources.RawSource(source));
|
|
195
160
|
}
|
|
196
161
|
|
|
197
162
|
/**
|
|
163
|
+
* @param compiler The webpack parent compiler.
|
|
198
164
|
* @param compilation The webpack compilation.
|
|
199
|
-
* @param parentCompiler The webpack parent compiler.
|
|
200
165
|
*
|
|
201
166
|
* @private
|
|
202
167
|
*/
|
|
203
|
-
private async handleMake(
|
|
168
|
+
private async handleMake(compiler: Compiler, compilation: Compilation): Promise<void> {
|
|
204
169
|
this.config = await validateInjectManifestOptions(this.config);
|
|
205
170
|
this.config.swDest = relativeToOutputPath(compilation, this.config.swDest!);
|
|
206
171
|
_generatedAssetNames.add(this.config.swDest);
|
|
207
172
|
|
|
208
173
|
if (this.config.compileSrc) {
|
|
209
|
-
await
|
|
174
|
+
await performChildCompilation(
|
|
175
|
+
compiler,
|
|
176
|
+
compilation,
|
|
177
|
+
this.constructor.name,
|
|
178
|
+
this.config.swSrc,
|
|
179
|
+
this.config.swDest,
|
|
180
|
+
this.config.webpackCompilationPlugins,
|
|
181
|
+
);
|
|
210
182
|
} else {
|
|
211
|
-
this.addSrcToAssets(
|
|
183
|
+
this.addSrcToAssets(compiler, compilation);
|
|
212
184
|
// This used to be a fatal error, but just warn at runtime because we
|
|
213
185
|
// can't validate it easily.
|
|
214
186
|
if (Array.isArray(this.config.webpackCompilationPlugins) && this.config.webpackCompilationPlugins.length > 0) {
|
|
@@ -228,10 +200,10 @@ export class InjectManifest {
|
|
|
228
200
|
const { size, sortedEntries, manifestString } = await this.getManifestEntries(compilation, config);
|
|
229
201
|
|
|
230
202
|
// See https://webpack.js.org/contribute/plugin-patterns/#monitoring-the-watch-graph
|
|
231
|
-
|
|
232
|
-
compilation.fileDependencies.add(absoluteSwSrc);
|
|
203
|
+
compilation.fileDependencies.add(path.resolve(config.swSrc));
|
|
233
204
|
|
|
234
205
|
const swAsset = compilation.getAsset(config.swDest!);
|
|
206
|
+
|
|
235
207
|
const swAssetString = swAsset!.source.source().toString();
|
|
236
208
|
|
|
237
209
|
const globalRegexp = new RegExp(escapeRegExp(config.injectionPoint), "g");
|
|
@@ -252,20 +224,18 @@ export class InjectManifest {
|
|
|
252
224
|
_generatedAssetNames.add(sourcemapAssetName);
|
|
253
225
|
const sourcemapAsset = compilation.getAsset(sourcemapAssetName);
|
|
254
226
|
const { source, map } = await replaceAndUpdateSourceMap({
|
|
255
|
-
jsFilename: config.swDest
|
|
256
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
227
|
+
jsFilename: toUnix(config.swDest!),
|
|
257
228
|
originalMap: JSON.parse(sourcemapAsset!.source.source().toString()),
|
|
258
229
|
originalSource: swAssetString,
|
|
259
230
|
replaceString: manifestString,
|
|
260
|
-
searchString: config.injectionPoint
|
|
231
|
+
searchString: config.injectionPoint,
|
|
261
232
|
});
|
|
262
|
-
|
|
263
233
|
compilation.updateAsset(sourcemapAssetName, new this.webpack.sources.RawSource(map));
|
|
264
234
|
compilation.updateAsset(config.swDest!, new this.webpack.sources.RawSource(source));
|
|
265
235
|
} else {
|
|
266
236
|
// If there's no sourcemap associated with swDest, a simple string
|
|
267
237
|
// replacement will suffice.
|
|
268
|
-
compilation.updateAsset(config.swDest!, new this.webpack.sources.RawSource(swAssetString.replace(config.injectionPoint
|
|
238
|
+
compilation.updateAsset(config.swDest!, new this.webpack.sources.RawSource(swAssetString.replace(config.injectionPoint, manifestString)));
|
|
269
239
|
}
|
|
270
240
|
|
|
271
241
|
if (compilation.getLogger) {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import type {
|
|
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
|
-
|
|
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 (
|
|
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 =
|
|
39
|
-
const sourcemapURLAssetName =
|
|
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
|
|
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,
|
|
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 (
|
|
24
|
-
return
|
|
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
|
|
27
|
+
return originalPath;
|
|
29
28
|
};
|
package/src/lib/types.ts
CHANGED
|
@@ -58,7 +58,7 @@ export interface InjectPartial {
|
|
|
58
58
|
* Optional webpack plugins that will be used when compiling the `swSrc`
|
|
59
59
|
* file. Only valid if `compileSrc` is `true`.
|
|
60
60
|
*/
|
|
61
|
-
webpackCompilationPlugins?:
|
|
61
|
+
webpackCompilationPlugins?: WebpackPlugin[];
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
export type InjectResolved = Require<InjectPartial, "compileSrc">;
|
|
@@ -66,3 +66,5 @@ export type InjectResolved = Require<InjectPartial, "compileSrc">;
|
|
|
66
66
|
export interface InjectManifestOptions extends BasePartial, WebpackPartial, BaseInjectPartial, OptionalSwDestPartial, InjectPartial {}
|
|
67
67
|
|
|
68
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 };
|