Skip to content

Commit f11499c

Browse files
jeffposnickphilipwalton
authored andcommitted
async manifestTransforms (#2195)
1 parent bd01715 commit f11499c

File tree

6 files changed

+41
-15
lines changed

6 files changed

+41
-15
lines changed

packages/workbox-build/src/lib/get-file-manifest-entries.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ module.exports = async ({
108108
}
109109
}
110110

111-
const transformedManifest = transformManifest({
111+
const transformedManifest = await transformManifest({
112112
additionalManifestEntries,
113113
dontCacheBustURLsMatching,
114114
fileDetails,

packages/workbox-build/src/lib/transform-manifest.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const noRevisionForURLsMatchingTransform =
2828
* @example <caption>A transformation that prepended the origin of a CDN for any
2929
* URL starting with '/assets/' could be implemented as:</caption>
3030
*
31-
* const cdnTransform = (manifestEntries) => {
31+
* const cdnTransform = async (manifestEntries) => {
3232
* const manifest = manifestEntries.map(entry => {
3333
* const cdnOrigin = 'https://example.com';
3434
* if (entry.url.startsWith('/assets/')) {
@@ -43,7 +43,7 @@ const noRevisionForURLsMatchingTransform =
4343
* URL contains an 8-character hash surrounded by '.', indicating that it
4444
* already contains revision information:</caption>
4545
*
46-
* const removeRevisionTransform = (manifestEntries) => {
46+
* const removeRevisionTransform = async (manifestEntries) => {
4747
* const manifest = manifestEntries.map(entry => {
4848
* const hashRegExp = /\.\w{8}\./;
4949
* if (entry.url.match(hashRegExp)) {
@@ -59,14 +59,14 @@ const noRevisionForURLsMatchingTransform =
5959
* array of entries, prior to the current transformation.
6060
* @param {Object} [compilation] When used in the webpack plugins, this param
6161
* will be set to the current `compilation`.
62-
* @return {module:workbox-build.ManifestTransformResult}
62+
* @return {Promise<module:workbox-build.ManifestTransformResult>}
6363
* The array of entries with the transformation applied, and optionally, any
6464
* warnings that should be reported back to the build tool.
6565
*
6666
* @memberof module:workbox-build
6767
*/
6868

69-
module.exports = ({
69+
module.exports = async ({
7070
additionalManifestEntries,
7171
dontCacheBustURLsMatching,
7272
fileDetails,
@@ -115,7 +115,7 @@ module.exports = ({
115115

116116
let transformedManifest = normalizedManifest;
117117
for (const transform of transformsToApply) {
118-
const result = transform(transformedManifest, transformParam);
118+
const result = await transform(transformedManifest, transformParam);
119119
if (!('manifest' in result)) {
120120
throw new Error(errors['bad-manifest-transforms-return-value']);
121121
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class GenerateSW {
108108
.concat(scripts);
109109
}
110110

111-
config.manifestEntries = getManifestEntriesFromCompilation(
111+
config.manifestEntries = await getManifestEntriesFromCompilation(
112112
compilation, config);
113113

114114
const unbundledCode = populateSWTemplate(config);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class InjectManifest {
152152
const swAsset = compilation.assets[config.swDest];
153153
delete compilation.assets[config.swDest];
154154

155-
const manifestEntries = getManifestEntriesFromCompilation(
155+
const manifestEntries = await getManifestEntriesFromCompilation(
156156
compilation, config);
157157

158158
const manifestDeclaration = stringifyManifest(manifestEntries,

packages/workbox-webpack-plugin/src/lib/get-manifest-entries-from-compilation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ function filterAssets(compilation, config) {
150150
return filteredAssets;
151151
}
152152

153-
module.exports = (compilation, config) => {
153+
module.exports = async (compilation, config) => {
154154
const filteredAssets = filterAssets(compilation, config);
155155

156156
const {publicPath} = compilation.options.output;
@@ -177,7 +177,7 @@ module.exports = (compilation, config) => {
177177
// We also get back `size` and `count`, and it would be nice to log that
178178
// somewhere, but... webpack doesn't offer info-level logs?
179179
// https://github.com/webpack/webpack/issues/3996
180-
const {manifestEntries, warnings} = transformManifest({
180+
const {manifestEntries, warnings} = await transformManifest({
181181
fileDetails,
182182
additionalManifestEntries: config.additionalManifestEntries,
183183
dontCacheBustURLsMatching: config.dontCacheBustURLsMatching,

test/workbox-build/node/lib/transform-manifest.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe(`[workbox-build] lib/transform-manifest.js`, function() {
3030
const FILE_DETAILS = [ENTRY1, ENTRY2, ENTRY3];
3131

3232
it(`should filter out files above maximumFileSizeToCacheInBytes`, async function() {
33-
const {size, count, manifestEntries} = transformManifest({
33+
const {size, count, manifestEntries} = await transformManifest({
3434
maximumFileSizeToCacheInBytes: MAXIMUM_FILE_SIZE,
3535
fileDetails: FILE_DETAILS,
3636
});
@@ -47,7 +47,7 @@ describe(`[workbox-build] lib/transform-manifest.js`, function() {
4747
});
4848

4949
it(`should remove revision info based on dontCacheBustURLsMatching`, async function() {
50-
const {size, count, manifestEntries} = transformManifest({
50+
const {size, count, manifestEntries} = await transformManifest({
5151
dontCacheBustURLsMatching: new RegExp(ENTRY1.file),
5252
fileDetails: FILE_DETAILS,
5353
});
@@ -68,7 +68,7 @@ describe(`[workbox-build] lib/transform-manifest.js`, function() {
6868
it(`should modify the URLs based on modifyURLPrefix`, async function() {
6969
const prefix = 'prefix/';
7070

71-
const {size, count, manifestEntries} = transformManifest({
71+
const {size, count, manifestEntries} = await transformManifest({
7272
modifyURLPrefix: {
7373
'': prefix,
7474
},
@@ -89,7 +89,7 @@ describe(`[workbox-build] lib/transform-manifest.js`, function() {
8989
}]);
9090
});
9191

92-
it(`should use custom manifestTransforms`, function() {
92+
it(`should use custom manifestTransforms`, async function() {
9393
const prefix1 = 'prefix1/';
9494
const prefix2 = 'prefix2/';
9595

@@ -118,7 +118,7 @@ describe(`[workbox-build] lib/transform-manifest.js`, function() {
118118
return {manifest, warnings: [warning2]};
119119
};
120120

121-
const {size, count, manifestEntries, warnings} = transformManifest({
121+
const {size, count, manifestEntries, warnings} = await transformManifest({
122122
fileDetails: FILE_DETAILS,
123123
manifestTransforms: [transform1, transform2],
124124
transformParam,
@@ -138,4 +138,30 @@ describe(`[workbox-build] lib/transform-manifest.js`, function() {
138138
revision: ENTRY3.hash,
139139
}]);
140140
});
141+
142+
it(`should support an async manifestTransform`, async function() {
143+
const asyncTransform = async (manifest) => {
144+
await Promise.resolve();
145+
return {manifest, warnings: []};
146+
};
147+
148+
const {size, count, manifestEntries, warnings} = await transformManifest({
149+
fileDetails: FILE_DETAILS,
150+
manifestTransforms: [asyncTransform],
151+
});
152+
153+
expect(warnings).to.be.empty;
154+
expect(size).to.eql(ENTRY1.size + ENTRY2.size + ENTRY3.size);
155+
expect(count).to.eql(3);
156+
expect(manifestEntries).to.deep.equal([{
157+
url: ENTRY1.file,
158+
revision: ENTRY1.hash,
159+
}, {
160+
url: ENTRY2.file,
161+
revision: ENTRY2.hash,
162+
}, {
163+
url: ENTRY3.file,
164+
revision: ENTRY3.hash,
165+
}]);
166+
});
141167
});

0 commit comments

Comments
 (0)