Skip to content

Commit

Permalink
Misc. webpack changes (#2683)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffposnick committed Nov 30, 2020
1 parent f2ef912 commit e2bae90
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 241 deletions.
377 changes: 149 additions & 228 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -104,7 +104,7 @@
"typescript": "^3.9.7",
"upath": "^1.2.0",
"webpack-v4": "npm:webpack@^4.44.2",
"webpack-v5": "npm:webpack@^5.4.0",
"webpack-v5": "npm:webpack@^5.9.0",
"worker-plugin": "^5.0.0"
}
}
2 changes: 1 addition & 1 deletion packages/workbox-webpack-plugin/package.json
Expand Up @@ -31,7 +31,7 @@
"workbox-build": "^6.0.0-rc.0"
},
"peerDependencies": {
"webpack": "^4.4.0 || ^5.4.0"
"webpack": "^4.4.0 || ^5.9.0"
},
"author": "Google's Web DevRel Team",
"license": "MIT",
Expand Down
9 changes: 7 additions & 2 deletions packages/workbox-webpack-plugin/src/generate-sw.js
Expand Up @@ -253,11 +253,16 @@ class GenerateSW {
async addAssets(compilation) {
// See https://github.com/GoogleChrome/workbox/issues/1790
if (this.alreadyCalled) {
compilation.warnings.push(`${this.constructor.name} has been called ` +
const warningMessage = `${this.constructor.name} has been called ` +
`multiple times, perhaps due to running webpack in --watch mode. The ` +
`precache manifest generated after the first call may be inaccurate! ` +
`Please see https://github.com/GoogleChrome/workbox/issues/1790 for ` +
`more information.`);
`more information.`;

if (!compilation.warnings.some((warning) => warning instanceof Error &&
warning.message === warningMessage)) {
compilation.warnings.push(new Error(warningMessage));
}
} else {
this.alreadyCalled = true;
}
Expand Down
16 changes: 13 additions & 3 deletions packages/workbox-webpack-plugin/src/inject-manifest.js
Expand Up @@ -6,6 +6,7 @@
https://opensource.org/licenses/MIT.
*/

const escapeRegexp = require('workbox-build/build/lib/escape-regexp');
const prettyBytes = require('pretty-bytes');
const replaceAndUpdateSourceMap = require(
'workbox-build/build/lib/replace-and-update-source-map');
Expand Down Expand Up @@ -279,8 +280,9 @@ class InjectManifest {
`Please see https://github.com/GoogleChrome/workbox/issues/1790 for ` +
`more information.`;

if (!compilation.warnings.includes(warningMessage)) {
compilation.warnings.push(warningMessage);
if (!compilation.warnings.some((warning) => warning instanceof Error &&
warning.message === warningMessage)) {
compilation.warnings.push(new Error(warningMessage));
}
} else {
this.alreadyCalled = true;
Expand All @@ -299,9 +301,17 @@ class InjectManifest {
const swAsset = compilation.getAsset(config.swDest);
const swAssetString = swAsset.source.source();

if (!swAssetString.includes(config.injectionPoint)) {
const globalRegexp = new RegExp(escapeRegexp(config.injectionPoint), 'g');
const injectionResults = swAssetString.match(globalRegexp);

if (!injectionResults) {
throw new Error(`Can't find ${config.injectionPoint} in your SW source.`);
}
if (injectionResults.length !== 1) {
throw new Error(`Multiple instances of ${config.injectionPoint} were ` +
`found in your SW source. Include it only once. For more info, see ` +
`https://github.com/GoogleChrome/workbox/issues/2681`);
}

const {size, sortedEntries} = await getManifestEntriesFromCompilation(
compilation, config);
Expand Down
Expand Up @@ -84,9 +84,9 @@ function filterAssets(compilation, config) {
allowedAssetNames.add(assetName);
}
} else {
compilation.warnings.push(`The chunk '${chunkName}' was ` +
compilation.warnings.push(new Error(`The chunk '${chunkName}' was ` +
`provided in your Workbox chunks config, but was not found in the ` +
`compilation.`);
`compilation.`));
}
}
}
Expand Down
Expand Up @@ -25,14 +25,14 @@ module.exports = (compilation, chunkNames) => {
}
}
} else {
compilation.warnings.push(`${chunkName} was provided to ` +
`importScriptsViaChunks, but didn't match any named chunks.`);
compilation.warnings.push(new Error(`${chunkName} was provided to ` +
`importScriptsViaChunks, but didn't match any named chunks.`));
}
}

if (scriptFiles.size === 0) {
compilation.warnings.push(`There were no assets matching ` +
`importScriptsViaChunks: [${chunkNames}].`);
compilation.warnings.push(new Error(`There were no assets matching ` +
`importScriptsViaChunks: [${chunkNames}].`));
}

return Array.from(scriptFiles);
Expand Down
35 changes: 35 additions & 0 deletions test/workbox-webpack-plugin/node/v4/inject-manifest.js
Expand Up @@ -81,6 +81,41 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
}
});
});

it(`should lead to a webpack compilation error when the swSrc contains multiple injection points`, function(done) {
const outputDir = tempy.directory();
const config = {
mode: 'production',
entry: {
entry1: upath.join(SRC_DIR, WEBPACK_ENTRY_FILENAME),
},
output: {
filename: '[name]-[chunkhash].js',
path: outputDir,
},
plugins: [
new InjectManifest({
swSrc: upath.join(__dirname, '..', '..', 'static', 'bad-multiple-injection.js'),
}),
],
};

const compiler = webpack(config);
compiler.run((webpackError, stats) => {
try {
expect(webpackError).not.to.exist;
const statsJson = stats.toJson();
expect(statsJson.warnings).to.be.empty;
expect(statsJson.errors).to.have.members([
`Multiple instances of self.__WB_MANIFEST were found in your SW source. Include it only once. For more info, see https://github.com/GoogleChrome/workbox/issues/2681`,
]);

done();
} catch (error) {
done(error);
}
});
});
});

describe(`[workbox-webpack-plugin] Multiple chunks`, function() {
Expand Down
35 changes: 35 additions & 0 deletions test/workbox-webpack-plugin/node/v5/inject-manifest.js
Expand Up @@ -80,6 +80,41 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v5`, function() {
}
});
});

it(`should lead to a webpack compilation error when the swSrc contains multiple injection points`, function(done) {
const outputDir = tempy.directory();
const config = {
mode: 'production',
entry: {
entry1: upath.join(SRC_DIR, WEBPACK_ENTRY_FILENAME),
},
output: {
filename: '[name]-[chunkhash].js',
path: outputDir,
},
plugins: [
new InjectManifest({
swSrc: upath.join(__dirname, '..', '..', 'static', 'bad-multiple-injection.js'),
}),
],
};

const compiler = webpack(config);
compiler.run((webpackError, stats) => {
try {
expect(webpackError).not.to.exist;
const statsJson = stats.toJson();
expect(statsJson.warnings).to.be.empty;
expect(statsJson.errors[0].message).to.eql(
`Multiple instances of self.__WB_MANIFEST were found in your SW source. Include it only once. For more info, see https://github.com/GoogleChrome/workbox/issues/2681`,
);

done();
} catch (error) {
done(error);
}
});
});
});

describe(`[workbox-webpack-plugin] Multiple chunks`, function() {
Expand Down
11 changes: 11 additions & 0 deletions test/workbox-webpack-plugin/static/bad-multiple-injection.js
@@ -0,0 +1,11 @@
/*
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.
*/

// Multiple entries.
workbox.precaching.precacheAndRoute(self.__WB_MANIFEST);
workbox.precaching.precacheAndRoute(self.__WB_MANIFEST);

0 comments on commit e2bae90

Please sign in to comment.