From cb87fbe3237e6b83d658164cbc3852bcd3f89b3f Mon Sep 17 00:00:00 2001 From: Prateek Bhatnagar Date: Tue, 10 Mar 2020 11:00:07 -0700 Subject: [PATCH 1/6] fixing application of plugins in inject-manifest-plugin --- package-lock.json | 12 ++--- .../src/inject-manifest.js | 7 ++- .../node/inject-manifest.js | 51 +++++++++++++++++++ .../static/sw-src-define-plugin.js | 12 +++++ 4 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 test/workbox-webpack-plugin/static/sw-src-define-plugin.js diff --git a/package-lock.json b/package-lock.json index 6e6f28922..934660062 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10078,7 +10078,7 @@ }, "html-webpack-plugin": { "version": "3.2.0", - "resolved": "https://registry.npmjs.org/html-webpack-plugin/-/html-webpack-plugin-3.2.0.tgz", + "resolved": "http://registry.npmjs.org/html-webpack-plugin/-/html-webpack-plugin-3.2.0.tgz", "integrity": "sha1-sBq71yOsqqeze2r0SS69oD2d03s=", "dev": true, "requires": { @@ -12242,7 +12242,7 @@ }, "minimist": { "version": "1.2.0", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "resolved": "http://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", "dev": true }, @@ -12401,7 +12401,7 @@ }, "mkdirp": { "version": "0.5.1", - "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "resolved": "http://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", "dev": true, "requires": { @@ -12410,7 +12410,7 @@ "dependencies": { "minimist": { "version": "0.0.8", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "resolved": "http://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", "dev": true } @@ -13609,7 +13609,7 @@ "dependencies": { "minimist": { "version": "0.0.10", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.10.tgz", + "resolved": "http://registry.npmjs.org/minimist/-/minimist-0.0.10.tgz", "integrity": "sha1-3j+YVD2/lggr5IrRoMfNqDYwHc8=", "dev": true } @@ -14145,7 +14145,7 @@ }, "pretty-hrtime": { "version": "1.0.3", - "resolved": "https://registry.npmjs.org/pretty-hrtime/-/pretty-hrtime-1.0.3.tgz", + "resolved": "http://registry.npmjs.org/pretty-hrtime/-/pretty-hrtime-1.0.3.tgz", "integrity": "sha1-t+PqQkNaTJsnWdmeDyAesZWALuE=", "dev": true }, 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..f40f65f74 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(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..07f2245bc --- /dev/null +++ b/test/workbox-webpack-plugin/static/sw-src-define-plugin.js @@ -0,0 +1,12 @@ +/* + Copyright 2018 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, {}); \ No newline at end of file From d60d9a89c8ceca3370c25ccc13939fc642140d12 Mon Sep 17 00:00:00 2001 From: Prateek Bhatnagar Date: Tue, 10 Mar 2020 11:01:14 -0700 Subject: [PATCH 2/6] lint fixes --- test/workbox-webpack-plugin/static/sw-src-define-plugin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/workbox-webpack-plugin/static/sw-src-define-plugin.js b/test/workbox-webpack-plugin/static/sw-src-define-plugin.js index 07f2245bc..64810bc95 100644 --- a/test/workbox-webpack-plugin/static/sw-src-define-plugin.js +++ b/test/workbox-webpack-plugin/static/sw-src-define-plugin.js @@ -9,4 +9,4 @@ const prefix = __PREFIX__; workbox.core.setCacheNameDetails({prefix}); -workbox.precaching.precacheAndRoute(self.__WB_MANIFEST, {}); \ No newline at end of file +workbox.precaching.precacheAndRoute(self.__WB_MANIFEST, {}); From 305dcac6ad9f47754111376015f7cc52bbba2b5f Mon Sep 17 00:00:00 2001 From: Prateek Bhatnagar Date: Tue, 10 Mar 2020 11:08:15 -0700 Subject: [PATCH 3/6] Update inject-manifest.js --- test/workbox-webpack-plugin/node/inject-manifest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/workbox-webpack-plugin/node/inject-manifest.js b/test/workbox-webpack-plugin/node/inject-manifest.js index f40f65f74..c6da511fd 100644 --- a/test/workbox-webpack-plugin/node/inject-manifest.js +++ b/test/workbox-webpack-plugin/node/inject-manifest.js @@ -1239,7 +1239,7 @@ describe(`[workbox-webpack-plugin] InjectManifest (End to End)`, function() { }); }); - it(`•should use webpackCompilationPlugins with DefinePlugin`, function(done) { + 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(); From 60b9483a183dcb167d8afeaf37353f14d048f667 Mon Sep 17 00:00:00 2001 From: Prateek Bhatnagar Date: Tue, 10 Mar 2020 13:00:44 -0700 Subject: [PATCH 4/6] fixing windows and reverting package-lock.json --- package-lock.json | 14 +++++++------- .../workbox-webpack-plugin/node/inject-manifest.js | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 934660062..04b83cba3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10078,7 +10078,7 @@ }, "html-webpack-plugin": { "version": "3.2.0", - "resolved": "http://registry.npmjs.org/html-webpack-plugin/-/html-webpack-plugin-3.2.0.tgz", + "resolved": "https://registry.npmjs.org/html-webpack-plugin/-/html-webpack-plugin-3.2.0.tgz", "integrity": "sha1-sBq71yOsqqeze2r0SS69oD2d03s=", "dev": true, "requires": { @@ -12242,7 +12242,7 @@ }, "minimist": { "version": "1.2.0", - "resolved": "http://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", "dev": true }, @@ -12401,7 +12401,7 @@ }, "mkdirp": { "version": "0.5.1", - "resolved": "http://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", "dev": true, "requires": { @@ -12410,7 +12410,7 @@ "dependencies": { "minimist": { "version": "0.0.8", - "resolved": "http://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", "dev": true } @@ -13609,7 +13609,7 @@ "dependencies": { "minimist": { "version": "0.0.10", - "resolved": "http://registry.npmjs.org/minimist/-/minimist-0.0.10.tgz", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.10.tgz", "integrity": "sha1-3j+YVD2/lggr5IrRoMfNqDYwHc8=", "dev": true } @@ -14145,7 +14145,7 @@ }, "pretty-hrtime": { "version": "1.0.3", - "resolved": "http://registry.npmjs.org/pretty-hrtime/-/pretty-hrtime-1.0.3.tgz", + "resolved": "https://registry.npmjs.org/pretty-hrtime/-/pretty-hrtime-1.0.3.tgz", "integrity": "sha1-t+PqQkNaTJsnWdmeDyAesZWALuE=", "dev": true }, @@ -18252,4 +18252,4 @@ } } } -} +} \ No newline at end of file diff --git a/test/workbox-webpack-plugin/node/inject-manifest.js b/test/workbox-webpack-plugin/node/inject-manifest.js index f40f65f74..c6955ce74 100644 --- a/test/workbox-webpack-plugin/node/inject-manifest.js +++ b/test/workbox-webpack-plugin/node/inject-manifest.js @@ -1269,7 +1269,7 @@ describe(`[workbox-webpack-plugin] InjectManifest (End to End)`, function() { try { webpackBuildCheck(webpackError, stats); - const files = await globby(outputDir); + const files = await globby('**', {cwd: outputDir}); expect(files).to.have.length(2); await validateServiceWorkerRuntime({ swFile, From 3e0848ec607e43a8bf0c43aee4361d3b95241944 Mon Sep 17 00:00:00 2001 From: Prateek Bhatnagar Date: Tue, 10 Mar 2020 14:00:12 -0700 Subject: [PATCH 5/6] Update sw-src-define-plugin.js --- test/workbox-webpack-plugin/static/sw-src-define-plugin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/workbox-webpack-plugin/static/sw-src-define-plugin.js b/test/workbox-webpack-plugin/static/sw-src-define-plugin.js index 64810bc95..4063cbfde 100644 --- a/test/workbox-webpack-plugin/static/sw-src-define-plugin.js +++ b/test/workbox-webpack-plugin/static/sw-src-define-plugin.js @@ -1,5 +1,5 @@ /* - Copyright 2018 Google LLC + 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. From 795f27e68bc1d852441a272a30264ac2382cc178 Mon Sep 17 00:00:00 2001 From: Prateek Bhatnagar Date: Tue, 10 Mar 2020 14:02:11 -0700 Subject: [PATCH 6/6] lint fixes --- package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 04b83cba3..6e6f28922 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18252,4 +18252,4 @@ } } } -} \ No newline at end of file +}