Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/plugins workbox webpack plugin #2400

Merged
merged 8 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/workbox-webpack-plugin/src/inject-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
51 changes: 51 additions & 0 deletions test/workbox-webpack-plugin/node/inject-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
12 changes: 12 additions & 0 deletions test/workbox-webpack-plugin/static/sw-src-define-plugin.js
Original file line number Diff line number Diff line change
@@ -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, {});