Skip to content

Commit 666f7e4

Browse files
authoredNov 13, 2020
Make use of AssetInfo in webpack build (#2673)
* Make use of AssetInfo * Updated JSDoc
1 parent 06e51db commit 666f7e4

File tree

7 files changed

+98
-93
lines changed

7 files changed

+98
-93
lines changed
 

‎packages/workbox-webpack-plugin/src/generate-sw.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ class GenerateSW {
7575
*
7676
* @param {RegExp} [config.dontCacheBustURLsMatching] Assets that match this will be
7777
* assumed to be uniquely versioned via their URL, and exempted from the normal
78-
* HTTP cache-busting that's done when populating the precache. While not
79-
* required, it's recommended that if your existing build process already
80-
* inserts a `[hash]` value into each filename, you provide a RegExp that will
81-
* detect that, as it will reduce the bandwidth consumed when precaching.
78+
* HTTP cache-busting that's done when populating the precache. (As of Workbox
79+
* v6, this option is usually not needed, as each
80+
* [asset's metadata](https://github.com/webpack/webpack/issues/9038) is used
81+
* to determine whether it's immutable or not.)
8282
*
8383
* @param {Array<string|RegExp|Function>} [config.exclude=[/\.map$/, /^manifest.*\.js$]]
8484
* One or more specifiers used to exclude assets from the precache manifest.
@@ -304,7 +304,10 @@ class GenerateSW {
304304
});
305305

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

‎packages/workbox-webpack-plugin/src/inject-manifest.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ class InjectManifest {
6767
*
6868
* @param {RegExp} [config.dontCacheBustURLsMatching] Assets that match this will be
6969
* assumed to be uniquely versioned via their URL, and exempted from the normal
70-
* HTTP cache-busting that's done when populating the precache. While not
71-
* required, it's recommended that if your existing build process already
72-
* inserts a `[hash]` value into each filename, you provide a RegExp that will
73-
* detect that, as it will reduce the bandwidth consumed when precaching.
70+
* HTTP cache-busting that's done when populating the precache. (As of Workbox
71+
* v6, this option is usually not needed, as each
72+
* [asset's metadata](https://github.com/webpack/webpack/issues/9038) is used
73+
* to determine whether it's immutable or not.)
7474
*
7575
* @param {Array<string|RegExp|Function>} [config.exclude=[/\.map$/, /^manifest.*\.js$]]
7676
* One or more specifiers used to exclude assets from the precache manifest.

‎packages/workbox-webpack-plugin/src/lib/get-asset-hash.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ const crypto = require('crypto');
1515
* @private
1616
*/
1717
module.exports = (asset) => {
18-
// TODO: Check asset.info.immutable and use null when set.
19-
// if (asset.info.immutable) {
20-
// return null;
21-
// }
18+
// If webpack has the asset marked as immutable, then we don't need to
19+
// use an out-of-band revision for it.
20+
// See https://github.com/webpack/webpack/issues/9038
21+
if (asset.info && asset.info.immutable) {
22+
return null;
23+
}
2224

2325
return crypto.createHash('md5')
2426
.update(Buffer.from(asset.source.source()))

‎test/workbox-webpack-plugin/node/v4/generate-sw.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
109109
importScripts: [[/^\.\/workbox-[0-9a-f]{8}$/]],
110110
precacheAndRoute: [[[
111111
{
112-
revision: /^[0-9a-f]{32}$/,
112+
revision: null,
113113
url: /^entry1-[0-9a-f]{20}\.js$/,
114114
}, {
115-
revision: /^[0-9a-f]{32}$/,
115+
revision: null,
116116
url: /^entry2-[0-9a-f]{20}\.js$/,
117117
},
118118
], {}]],
@@ -171,7 +171,7 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
171171
],
172172
// imported-[chunkhash].js should *not* be included.
173173
precacheAndRoute: [[[{
174-
revision: /^[0-9a-f]{32}$/,
174+
revision: null,
175175
url: /^main-[0-9a-f]{20}\.js$/,
176176
}], {}]],
177177
},
@@ -224,10 +224,10 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
224224
importScripts: [[/^\.\/workbox-[0-9a-f]{8}$/]],
225225
precacheAndRoute: [[[
226226
{
227-
revision: /^[0-9a-f]{32}$/,
227+
revision: null,
228228
url: /^entry1-[0-9a-f]{20}\.js$/,
229229
}, {
230-
revision: /^[0-9a-f]{32}$/,
230+
revision: null,
231231
url: /^entry2-[0-9a-f]{20}\.js$/,
232232
}, {
233233
revision: null,
@@ -283,10 +283,10 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
283283
importScripts: [[/^\.\/workbox-[0-9a-f]{8}$/]],
284284
precacheAndRoute: [[[
285285
{
286-
revision: /^[0-9a-f]{32}$/,
286+
revision: null,
287287
url: /^entry1-[0-9a-f]{20}\.js$/,
288288
}, {
289-
revision: /^[0-9a-f]{32}$/,
289+
revision: null,
290290
url: /^entry2-[0-9a-f]{20}\.js$/,
291291
},
292292
], {}]],
@@ -385,10 +385,10 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
385385
importScripts: [[/^\.\/workbox-[0-9a-f]{8}$/]],
386386
precacheAndRoute: [[[
387387
{
388-
revision: /^[0-9a-f]{32}$/,
388+
revision: null,
389389
url: /^entry1-[0-9a-f]{20}\.js$/,
390390
}, {
391-
revision: /^[0-9a-f]{32}$/,
391+
revision: null,
392392
url: /^entry2-[0-9a-f]{20}\.js$/,
393393
},
394394
], {}]],
@@ -435,7 +435,7 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
435435
importScripts: [[/^\.\/workbox-[0-9a-f]{8}$/]],
436436
precacheAndRoute: [[[
437437
{
438-
revision: /^[0-9a-f]{32}$/,
438+
revision: null,
439439
url: /^entry1-[0-9a-f]{20}\.js$/,
440440
},
441441
], {}]],
@@ -480,10 +480,10 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
480480
await validateServiceWorkerRuntime({swFile, expectedMethodCalls: {
481481
importScripts: [[/^\.\/workbox-[0-9a-f]{8}$/]],
482482
precacheAndRoute: [[[{
483-
revision: /^[0-9a-f]{32}$/,
483+
revision: null,
484484
url: /^entry1-[0-9a-f]{20}\.js$/,
485485
}, {
486-
revision: /^[0-9a-f]{32}$/,
486+
revision: null,
487487
url: /^entry2-[0-9a-f]{20}\.js$/,
488488
}, {
489489
revision: /^[0-9a-f]{32}$/,
@@ -852,7 +852,7 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
852852
importScripts: [[/^\.\/workbox-[0-9a-f]{8}$/]],
853853
precacheAndRoute: [[[
854854
{
855-
revision: /^[0-9a-f]{32}$/,
855+
revision: null,
856856
url: /^entry1-[0-9a-f]{20}\.js$/,
857857
},
858858
], {}]],
@@ -910,7 +910,7 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
910910
importScripts: [[/^\.\/workbox-[0-9a-f]{8}$/]],
911911
precacheAndRoute: [[[
912912
{
913-
revision: /^[0-9a-f]{32}$/,
913+
revision: null,
914914
url: /^entry1-[0-9a-f]{20}\.js$/,
915915
},
916916
{
@@ -988,7 +988,7 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
988988
importScripts: [[/^\.\/workbox-[0-9a-f]{8}$/]],
989989
precacheAndRoute: [[[
990990
{
991-
revision: /^[0-9a-f]{32}$/,
991+
revision: null,
992992
url: /^\/testing\/entry1-[0-9a-f]{20}\.js$/,
993993
},
994994
], {}]],
@@ -1078,7 +1078,7 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
10781078
await validateServiceWorkerRuntime({swString, expectedMethodCalls: {
10791079
importScripts: [[/^\.\/workbox-[0-9a-f]{8}$/]],
10801080
precacheAndRoute: [[[{
1081-
revision: /^[0-9a-f]{32}$/,
1081+
revision: null,
10821082
url: /^entry1-[0-9a-f]{20}\.js$/,
10831083
}], {}]],
10841084
}});
@@ -1384,7 +1384,7 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
13841384
await validateServiceWorkerRuntime({swFile, expectedMethodCalls: {
13851385
importScripts: [[/^\.\/workbox-[0-9a-f]{8}$/]],
13861386
precacheAndRoute: [[[{
1387-
revision: /^[0-9a-f]{32}$/,
1387+
revision: null,
13881388
url: /^https:\/\/example\.org\/main\.[0-9a-f]{20}\.js/,
13891389
}], {}]],
13901390
}});
@@ -1412,7 +1412,7 @@ describe(`[workbox-webpack-plugin] GenerateSW with webpack v4`, function() {
14121412
expect(manifest).to.have.lengthOf(1);
14131413
expect(manifest[0].size).to.eql(959);
14141414
expect(manifest[0].url.startsWith('main.')).to.be.true;
1415-
expect(manifest[0].revision).to.have.lengthOf(32);
1415+
expect(manifest[0].revision).to.be.null;
14161416
expect(compilation).to.exist;
14171417

14181418
manifest = manifest.map((entry) => {

‎test/workbox-webpack-plugin/node/v4/inject-manifest.js

+20-20
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
119119
expectedMethodCalls: {
120120
precacheAndRoute: [[[
121121
{
122-
revision: /^[0-9a-f]{32}$/,
122+
revision: null,
123123
url: /^entry1-[0-9a-f]{20}\.js$/,
124124
}, {
125-
revision: /^[0-9a-f]{32}$/,
125+
revision: null,
126126
url: /^entry2-[0-9a-f]{20}\.js$/,
127127
},
128128
], {}]],
@@ -173,10 +173,10 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
173173
expectedMethodCalls: {
174174
precacheAndRoute: [[[
175175
{
176-
revision: /^[0-9a-f]{32}$/,
176+
revision: null,
177177
url: /^entry1-[0-9a-f]{20}\.js$/,
178178
}, {
179-
revision: /^[0-9a-f]{32}$/,
179+
revision: null,
180180
url: /^entry2-[0-9a-f]{20}\.js$/,
181181
},
182182
], {}]],
@@ -285,10 +285,10 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
285285
expectedMethodCalls: {
286286
precacheAndRoute: [[[
287287
{
288-
revision: /^[0-9a-f]{32}$/,
288+
revision: null,
289289
url: /^entry1-[0-9a-f]{20}\.js$/,
290290
}, {
291-
revision: /^[0-9a-f]{32}$/,
291+
revision: null,
292292
url: /^entry2-[0-9a-f]{20}\.js$/,
293293
},
294294
], {}]],
@@ -340,7 +340,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
340340
expectedMethodCalls: {
341341
precacheAndRoute: [[[
342342
{
343-
revision: /^[0-9a-f]{32}$/,
343+
revision: null,
344344
url: /^entry1-[0-9a-f]{20}\.js$/,
345345
},
346346
], {}]],
@@ -391,10 +391,10 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
391391
entryPoint: 'injectManifest',
392392
expectedMethodCalls: {
393393
precacheAndRoute: [[[{
394-
revision: /^[0-9a-f]{32}$/,
394+
revision: null,
395395
url: /^entry1-[0-9a-f]{20}\.js$/,
396396
}, {
397-
revision: /^[0-9a-f]{32}$/,
397+
revision: null,
398398
url: /^entry2-[0-9a-f]{20}\.js$/,
399399
}, {
400400
revision: /^[0-9a-f]{32}$/,
@@ -958,7 +958,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
958958
expectedMethodCalls: {
959959
precacheAndRoute: [[[
960960
{
961-
revision: /^[0-9a-f]{32}$/,
961+
revision: null,
962962
url: /^entry1-[0-9a-f]{20}\.js$/,
963963
},
964964
], {}]],
@@ -1020,7 +1020,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
10201020
expectedMethodCalls: {
10211021
precacheAndRoute: [[[
10221022
{
1023-
revision: /^[0-9a-f]{32}$/,
1023+
revision: null,
10241024
url: /^entry1-[0-9a-f]{20}\.js$/,
10251025
},
10261026
{
@@ -1104,7 +1104,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
11041104
expectedMethodCalls: {
11051105
precacheAndRoute: [[[
11061106
{
1107-
revision: /^[0-9a-f]{32}$/,
1107+
revision: null,
11081108
url: /^\/testing\/entry1-[0-9a-f]{20}\.js$/,
11091109
},
11101110
], {}]],
@@ -1244,7 +1244,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
12441244
entryPoint: 'injectManifest',
12451245
expectedMethodCalls: {
12461246
precacheAndRoute: [[[{
1247-
revision: /^[0-9a-f]{32}$/,
1247+
revision: null,
12481248
url: /^https:\/\/example\.org\/main\.[0-9a-f]{20}\.js/,
12491249
}], {}]],
12501250
},
@@ -1295,7 +1295,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
12951295
expectedMethodCalls: {
12961296
setCacheNameDetails: [[{prefix}]],
12971297
precacheAndRoute: [[[{
1298-
revision: /^[0-9a-f]{32}$/,
1298+
revision: null,
12991299
url: /^main\.[0-9a-f]{20}\.js$/,
13001300
}], {}]],
13011301
},
@@ -1326,7 +1326,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
13261326
expect(manifest).to.have.lengthOf(1);
13271327
expect(manifest[0].size).to.eql(959);
13281328
expect(manifest[0].url.startsWith('main.')).to.be.true;
1329-
expect(manifest[0].revision).to.have.lengthOf(32);
1329+
expect(manifest[0].revision).to.be.null;
13301330
expect(compilation).to.exist;
13311331

13321332
manifest = manifest.map((entry) => {
@@ -1554,7 +1554,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
15541554
entryPoint: 'injectManifest',
15551555
expectedMethodCalls: {
15561556
precacheAndRoute: [[[{
1557-
revision: /^[0-9a-f]{32}$/,
1557+
revision: null,
15581558
url: /^main\.[0-9a-f]{20}\.js$/,
15591559
}], {}]],
15601560
},
@@ -1565,7 +1565,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
15651565
entryPoint: 'injectManifest',
15661566
expectedMethodCalls: {
15671567
precacheAndRoute: [[[{
1568-
revision: /^[0-9a-f]{32}$/,
1568+
revision: null,
15691569
url: /^main\.[0-9a-f]{20}\.js$/,
15701570
}], {}]],
15711571
},
@@ -1613,7 +1613,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
16131613
entryPoint: 'injectManifest',
16141614
expectedMethodCalls: {
16151615
precacheAndRoute: [[[{
1616-
revision: /^[0-9a-f]{32}$/,
1616+
revision: null,
16171617
url: /^main\.[0-9a-f]{20}\.js$/,
16181618
}], {}]],
16191619
},
@@ -1694,7 +1694,7 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
16941694

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

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

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

0 commit comments

Comments
 (0)