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

Make use of AssetInfo in webpack build #2673

Merged
merged 2 commits into from
Nov 13, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions packages/workbox-webpack-plugin/src/generate-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ class GenerateSW {
*
* @param {RegExp} [config.dontCacheBustURLsMatching] Assets that match this will be
* assumed to be uniquely versioned via their URL, and exempted from the normal
* HTTP cache-busting that's done when populating the precache. While not
* required, it's recommended that if your existing build process already
* inserts a `[hash]` value into each filename, you provide a RegExp that will
* detect that, as it will reduce the bandwidth consumed when precaching.
* HTTP cache-busting that's done when populating the precache. (As of Workbox
* v6, this option is usually not needed, as each
* [asset's metadata](https://github.com/webpack/webpack/issues/9038) is used
* to determine whether it's immutable or not.)
*
* @param {Array<string|RegExp|Function>} [config.exclude=[/\.map$/, /^manifest.*\.js$]]
* One or more specifiers used to exclude assets from the precache manifest.
Expand Down Expand Up @@ -304,7 +304,10 @@ class GenerateSW {
});

for (const file of files) {
compilation.emitAsset(file.name, new RawSource(file.contents));
compilation.emitAsset(file.name, new RawSource(file.contents), {
// See https://github.com/webpack-contrib/compression-webpack-plugin/issues/218#issuecomment-726196160
minimized: config.mode === 'production',
});
_generatedAssetNames.add(file.name);
}

Expand Down
8 changes: 4 additions & 4 deletions packages/workbox-webpack-plugin/src/inject-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ class InjectManifest {
*
* @param {RegExp} [config.dontCacheBustURLsMatching] Assets that match this will be
* assumed to be uniquely versioned via their URL, and exempted from the normal
* HTTP cache-busting that's done when populating the precache. While not
* required, it's recommended that if your existing build process already
* inserts a `[hash]` value into each filename, you provide a RegExp that will
* detect that, as it will reduce the bandwidth consumed when precaching.
* HTTP cache-busting that's done when populating the precache. (As of Workbox
* v6, this option is usually not needed, as each
* [asset's metadata](https://github.com/webpack/webpack/issues/9038) is used
* to determine whether it's immutable or not.)
*
* @param {Array<string|RegExp|Function>} [config.exclude=[/\.map$/, /^manifest.*\.js$]]
* One or more specifiers used to exclude assets from the precache manifest.
Expand Down
10 changes: 6 additions & 4 deletions packages/workbox-webpack-plugin/src/lib/get-asset-hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ const crypto = require('crypto');
* @private
*/
module.exports = (asset) => {
// TODO: Check asset.info.immutable and use null when set.
// if (asset.info.immutable) {
// return null;
// }
// If webpack has the asset marked as immutable, then we don't need to
// use an out-of-band revision for it.
// See https://github.com/webpack/webpack/issues/9038
if (asset.info && asset.info.immutable) {
return null;
}

return crypto.createHash('md5')
.update(Buffer.from(asset.source.source()))
Expand Down
36 changes: 18 additions & 18 deletions test/workbox-webpack-plugin/node/v4/generate-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
importScripts: [[/^\.\/workbox-[0-9a-f]{8}$/]],
precacheAndRoute: [[[
{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry1-[0-9a-f]{20}\.js$/,
}, {
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry2-[0-9a-f]{20}\.js$/,
},
], {}]],
Expand Down Expand Up @@ -171,7 +171,7 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
],
// imported-[chunkhash].js should *not* be included.
precacheAndRoute: [[[{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^main-[0-9a-f]{20}\.js$/,
}], {}]],
},
Expand Down Expand Up @@ -224,10 +224,10 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
importScripts: [[/^\.\/workbox-[0-9a-f]{8}$/]],
precacheAndRoute: [[[
{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry1-[0-9a-f]{20}\.js$/,
}, {
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry2-[0-9a-f]{20}\.js$/,
}, {
revision: null,
Expand Down Expand Up @@ -283,10 +283,10 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
importScripts: [[/^\.\/workbox-[0-9a-f]{8}$/]],
precacheAndRoute: [[[
{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry1-[0-9a-f]{20}\.js$/,
}, {
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry2-[0-9a-f]{20}\.js$/,
},
], {}]],
Expand Down Expand Up @@ -385,10 +385,10 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
importScripts: [[/^\.\/workbox-[0-9a-f]{8}$/]],
precacheAndRoute: [[[
{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry1-[0-9a-f]{20}\.js$/,
}, {
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry2-[0-9a-f]{20}\.js$/,
},
], {}]],
Expand Down Expand Up @@ -435,7 +435,7 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
importScripts: [[/^\.\/workbox-[0-9a-f]{8}$/]],
precacheAndRoute: [[[
{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry1-[0-9a-f]{20}\.js$/,
},
], {}]],
Expand Down Expand Up @@ -480,10 +480,10 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
await validateServiceWorkerRuntime({swFile, expectedMethodCalls: {
importScripts: [[/^\.\/workbox-[0-9a-f]{8}$/]],
precacheAndRoute: [[[{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry1-[0-9a-f]{20}\.js$/,
}, {
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry2-[0-9a-f]{20}\.js$/,
}, {
revision: /^[0-9a-f]{32}$/,
Expand Down Expand Up @@ -852,7 +852,7 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
importScripts: [[/^\.\/workbox-[0-9a-f]{8}$/]],
precacheAndRoute: [[[
{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry1-[0-9a-f]{20}\.js$/,
},
], {}]],
Expand Down Expand Up @@ -910,7 +910,7 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
importScripts: [[/^\.\/workbox-[0-9a-f]{8}$/]],
precacheAndRoute: [[[
{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry1-[0-9a-f]{20}\.js$/,
},
{
Expand Down Expand Up @@ -988,7 +988,7 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
importScripts: [[/^\.\/workbox-[0-9a-f]{8}$/]],
precacheAndRoute: [[[
{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^\/testing\/entry1-[0-9a-f]{20}\.js$/,
},
], {}]],
Expand Down Expand Up @@ -1078,7 +1078,7 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
await validateServiceWorkerRuntime({swString, expectedMethodCalls: {
importScripts: [[/^\.\/workbox-[0-9a-f]{8}$/]],
precacheAndRoute: [[[{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry1-[0-9a-f]{20}\.js$/,
}], {}]],
}});
Expand Down Expand Up @@ -1384,7 +1384,7 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
await validateServiceWorkerRuntime({swFile, expectedMethodCalls: {
importScripts: [[/^\.\/workbox-[0-9a-f]{8}$/]],
precacheAndRoute: [[[{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^https:\/\/example\.org\/main\.[0-9a-f]{20}\.js/,
}], {}]],
}});
Expand Down Expand Up @@ -1412,7 +1412,7 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
expect(manifest).to.have.lengthOf(1);
expect(manifest[0].size).to.eql(959);
expect(manifest[0].url.startsWith('main.')).to.be.true;
expect(manifest[0].revision).to.have.lengthOf(32);
expect(manifest[0].revision).to.be.null;
expect(compilation).to.exist;

manifest = manifest.map((entry) => {
Expand Down
40 changes: 20 additions & 20 deletions test/workbox-webpack-plugin/node/v4/inject-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
expectedMethodCalls: {
precacheAndRoute: [[[
{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry1-[0-9a-f]{20}\.js$/,
}, {
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry2-[0-9a-f]{20}\.js$/,
},
], {}]],
Expand Down Expand Up @@ -173,10 +173,10 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
expectedMethodCalls: {
precacheAndRoute: [[[
{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry1-[0-9a-f]{20}\.js$/,
}, {
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry2-[0-9a-f]{20}\.js$/,
},
], {}]],
Expand Down Expand Up @@ -285,10 +285,10 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
expectedMethodCalls: {
precacheAndRoute: [[[
{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry1-[0-9a-f]{20}\.js$/,
}, {
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry2-[0-9a-f]{20}\.js$/,
},
], {}]],
Expand Down Expand Up @@ -340,7 +340,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
expectedMethodCalls: {
precacheAndRoute: [[[
{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry1-[0-9a-f]{20}\.js$/,
},
], {}]],
Expand Down Expand Up @@ -391,10 +391,10 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
entryPoint: 'injectManifest',
expectedMethodCalls: {
precacheAndRoute: [[[{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry1-[0-9a-f]{20}\.js$/,
}, {
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry2-[0-9a-f]{20}\.js$/,
}, {
revision: /^[0-9a-f]{32}$/,
Expand Down Expand Up @@ -958,7 +958,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
expectedMethodCalls: {
precacheAndRoute: [[[
{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry1-[0-9a-f]{20}\.js$/,
},
], {}]],
Expand Down Expand Up @@ -1020,7 +1020,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
expectedMethodCalls: {
precacheAndRoute: [[[
{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^entry1-[0-9a-f]{20}\.js$/,
},
{
Expand Down Expand Up @@ -1104,7 +1104,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
expectedMethodCalls: {
precacheAndRoute: [[[
{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^\/testing\/entry1-[0-9a-f]{20}\.js$/,
},
], {}]],
Expand Down Expand Up @@ -1244,7 +1244,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
entryPoint: 'injectManifest',
expectedMethodCalls: {
precacheAndRoute: [[[{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^https:\/\/example\.org\/main\.[0-9a-f]{20}\.js/,
}], {}]],
},
Expand Down Expand Up @@ -1295,7 +1295,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
expectedMethodCalls: {
setCacheNameDetails: [[{prefix}]],
precacheAndRoute: [[[{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^main\.[0-9a-f]{20}\.js$/,
}], {}]],
},
Expand Down Expand Up @@ -1326,7 +1326,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
expect(manifest).to.have.lengthOf(1);
expect(manifest[0].size).to.eql(959);
expect(manifest[0].url.startsWith('main.')).to.be.true;
expect(manifest[0].revision).to.have.lengthOf(32);
expect(manifest[0].revision).to.be.null;
expect(compilation).to.exist;

manifest = manifest.map((entry) => {
Expand Down Expand Up @@ -1554,7 +1554,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
entryPoint: 'injectManifest',
expectedMethodCalls: {
precacheAndRoute: [[[{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^main\.[0-9a-f]{20}\.js$/,
}], {}]],
},
Expand All @@ -1565,7 +1565,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
entryPoint: 'injectManifest',
expectedMethodCalls: {
precacheAndRoute: [[[{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^main\.[0-9a-f]{20}\.js$/,
}], {}]],
},
Expand Down Expand Up @@ -1613,7 +1613,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
entryPoint: 'injectManifest',
expectedMethodCalls: {
precacheAndRoute: [[[{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^main\.[0-9a-f]{20}\.js$/,
}], {}]],
},
Expand Down Expand Up @@ -1694,7 +1694,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {

const manifest = await fse.readJSON(upath.join(outputDir, 'injected-manifest.json'));
expect(manifest).to.matchPattern([{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^main\.[0-9a-f]{20}\.js$/,
}]);

Expand Down Expand Up @@ -1735,7 +1735,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {

const manifest = require(upath.join(outputDir, 'injected-manifest.js'));
expect(manifest).to.matchPattern([{
revision: /^[0-9a-f]{32}$/,
revision: null,
url: /^main\.[0-9a-f]{20}\.js$/,
}]);

Expand Down