diff --git a/packages/workbox-webpack-plugin/src/inject-manifest.js b/packages/workbox-webpack-plugin/src/inject-manifest.js index 7565feeab..fee1af5c9 100644 --- a/packages/workbox-webpack-plugin/src/inject-manifest.js +++ b/packages/workbox-webpack-plugin/src/inject-manifest.js @@ -177,13 +177,18 @@ class InjectManifest { const childCompiler = compilation.createChildCompiler( this.constructor.name, outputOptions, - this.config.webpackCompilationPlugins, ); childCompiler.context = parentCompiler.context; childCompiler.inputFileSystem = parentCompiler.inputFileSystem; childCompiler.outputFileSystem = parentCompiler.outputFileSystem; + if (Array.isArray(this.config.webpackCompilationPlugins)) { + for (const plugin of this.config.webpackCompilationPlugins) { + plugin.apply(childCompiler); + } + } + new SingleEntryPlugin( parentCompiler.context, this.config.swSrc, diff --git a/test/workbox-webpack-plugin/node/inject-manifest.js b/test/workbox-webpack-plugin/node/inject-manifest.js index 06185a550..e23dc32f2 100644 --- a/test/workbox-webpack-plugin/node/inject-manifest.js +++ b/test/workbox-webpack-plugin/node/inject-manifest.js @@ -1239,6 +1239,57 @@ describe(`[workbox-webpack-plugin] InjectManifest (End to End)`, function() { }); }); + it(`should use webpackCompilationPlugins with DefinePlugin`, function(done) { + const prefix = 'replaced-by-define-plugin'; + const swSrc = upath.join(__dirname, '..', 'static', 'sw-src-define-plugin.js'); + const outputDir = tempy.directory(); + const config = { + mode: 'production', + entry: upath.join(SRC_DIR, WEBPACK_ENTRY_FILENAME), + output: { + filename: '[name].[hash:20].js', + path: outputDir, + }, + plugins: [ + new InjectManifest({ + swSrc, + swDest: 'service-worker.js', + webpackCompilationPlugins: [ + new webpack.DefinePlugin({ + __PREFIX__: JSON.stringify(prefix), + }), + ], + }), + ], + }; + + const compiler = webpack(config); + compiler.run(async (webpackError, stats) => { + const swFile = upath.join(outputDir, 'service-worker.js'); + try { + webpackBuildCheck(webpackError, stats); + + const files = await globby('**', {cwd: outputDir}); + expect(files).to.have.length(2); + await validateServiceWorkerRuntime({ + swFile, + entryPoint: 'injectManifest', + expectedMethodCalls: { + setCacheNameDetails: [[{prefix}]], + precacheAndRoute: [[[{ + revision: /^[0-9a-f]{32}$/, + url: /^main\.[0-9a-f]{20}\.js$/, + }], {}]], + }, + }); + + done(); + } catch (error) { + done(error); + } + }); + }); + it(`should use manifestTransforms`, function(done) { const outputDir = tempy.directory(); const warningMessage = 'test warning'; diff --git a/test/workbox-webpack-plugin/static/sw-src-define-plugin.js b/test/workbox-webpack-plugin/static/sw-src-define-plugin.js new file mode 100644 index 000000000..4063cbfde --- /dev/null +++ b/test/workbox-webpack-plugin/static/sw-src-define-plugin.js @@ -0,0 +1,12 @@ +/* + Copyright 2020 Google LLC + Use of this source code is governed by an MIT-style + license that can be found in the LICENSE file or at + https://opensource.org/licenses/MIT. +*/ + +// This is used to validate the DefinePlugin's replacement. +const prefix = __PREFIX__; +workbox.core.setCacheNameDetails({prefix}); + +workbox.precaching.precacheAndRoute(self.__WB_MANIFEST, {});