| 1 | /*
|
|---|
| 2 | Copyright 2018 Google LLC
|
|---|
| 3 |
|
|---|
| 4 | Use of this source code is governed by an MIT-style
|
|---|
| 5 | license that can be found in the LICENSE file or at
|
|---|
| 6 | https://opensource.org/licenses/MIT.
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 | import {
|
|---|
| 10 | BasePartial,
|
|---|
| 11 | FileDetails,
|
|---|
| 12 | ManifestEntry,
|
|---|
| 13 | ManifestTransform,
|
|---|
| 14 | } from '../types';
|
|---|
| 15 | import {additionalManifestEntriesTransform} from './additional-manifest-entries-transform';
|
|---|
| 16 | import {errors} from './errors';
|
|---|
| 17 | import {maximumSizeTransform} from './maximum-size-transform';
|
|---|
| 18 | import {modifyURLPrefixTransform} from './modify-url-prefix-transform';
|
|---|
| 19 | import {noRevisionForURLsMatchingTransform} from './no-revision-for-urls-matching-transform';
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * A `ManifestTransform` function can be used to modify the modify the `url` or
|
|---|
| 23 | * `revision` properties of some or all of the
|
|---|
| 24 | * {@link workbox-build.ManifestEntry} in the manifest.
|
|---|
| 25 | *
|
|---|
| 26 | * Deleting the `revision` property of an entry will cause
|
|---|
| 27 | * the corresponding `url` to be precached without cache-busting parameters
|
|---|
| 28 | * applied, which is to say, it implies that the URL itself contains
|
|---|
| 29 | * proper versioning info. If the `revision` property is present, it must be
|
|---|
| 30 | * set to a string.
|
|---|
| 31 | *
|
|---|
| 32 | * @example A transformation that prepended the origin of a CDN for any
|
|---|
| 33 | * URL starting with '/assets/' could be implemented as:
|
|---|
| 34 | *
|
|---|
| 35 | * const cdnTransform = async (manifestEntries) => {
|
|---|
| 36 | * const manifest = manifestEntries.map(entry => {
|
|---|
| 37 | * const cdnOrigin = 'https://example.com';
|
|---|
| 38 | * if (entry.url.startsWith('/assets/')) {
|
|---|
| 39 | * entry.url = cdnOrigin + entry.url;
|
|---|
| 40 | * }
|
|---|
| 41 | * return entry;
|
|---|
| 42 | * });
|
|---|
| 43 | * return {manifest, warnings: []};
|
|---|
| 44 | * };
|
|---|
| 45 | *
|
|---|
| 46 | * @example A transformation that nulls the revision field when the
|
|---|
| 47 | * URL contains an 8-character hash surrounded by '.', indicating that it
|
|---|
| 48 | * already contains revision information:
|
|---|
| 49 | *
|
|---|
| 50 | * const removeRevisionTransform = async (manifestEntries) => {
|
|---|
| 51 | * const manifest = manifestEntries.map(entry => {
|
|---|
| 52 | * const hashRegExp = /\.\w{8}\./;
|
|---|
| 53 | * if (entry.url.match(hashRegExp)) {
|
|---|
| 54 | * entry.revision = null;
|
|---|
| 55 | * }
|
|---|
| 56 | * return entry;
|
|---|
| 57 | * });
|
|---|
| 58 | * return {manifest, warnings: []};
|
|---|
| 59 | * };
|
|---|
| 60 | *
|
|---|
| 61 | * @callback ManifestTransform
|
|---|
| 62 | * @param {Array<workbox-build.ManifestEntry>} manifestEntries The full
|
|---|
| 63 | * array of entries, prior to the current transformation.
|
|---|
| 64 | * @param {Object} [compilation] When used in the webpack plugins, this param
|
|---|
| 65 | * will be set to the current `compilation`.
|
|---|
| 66 | * @return {Promise<workbox-build.ManifestTransformResult>}
|
|---|
| 67 | * The array of entries with the transformation applied, and optionally, any
|
|---|
| 68 | * warnings that should be reported back to the build tool.
|
|---|
| 69 | *
|
|---|
| 70 | * @memberof workbox-build
|
|---|
| 71 | */
|
|---|
| 72 |
|
|---|
| 73 | interface ManifestTransformResultWithWarnings {
|
|---|
| 74 | count: number;
|
|---|
| 75 | size: number;
|
|---|
| 76 | manifestEntries: ManifestEntry[];
|
|---|
| 77 | warnings: string[];
|
|---|
| 78 | }
|
|---|
| 79 | export async function transformManifest({
|
|---|
| 80 | additionalManifestEntries,
|
|---|
| 81 | dontCacheBustURLsMatching,
|
|---|
| 82 | fileDetails,
|
|---|
| 83 | manifestTransforms,
|
|---|
| 84 | maximumFileSizeToCacheInBytes,
|
|---|
| 85 | modifyURLPrefix,
|
|---|
| 86 | transformParam,
|
|---|
| 87 | }: BasePartial & {
|
|---|
| 88 | fileDetails: Array<FileDetails>;
|
|---|
| 89 | // When this is called by the webpack plugin, transformParam will be the
|
|---|
| 90 | // current webpack compilation.
|
|---|
| 91 | transformParam?: unknown;
|
|---|
| 92 | }): Promise<ManifestTransformResultWithWarnings> {
|
|---|
| 93 | const allWarnings: Array<string> = [];
|
|---|
| 94 |
|
|---|
| 95 | // Take the array of fileDetail objects and convert it into an array of
|
|---|
| 96 | // {url, revision, size} objects, with \ replaced with /.
|
|---|
| 97 | const normalizedManifest = fileDetails.map((fileDetails) => {
|
|---|
| 98 | return {
|
|---|
| 99 | url: fileDetails.file.replace(/\\/g, '/'),
|
|---|
| 100 | revision: fileDetails.hash,
|
|---|
| 101 | size: fileDetails.size,
|
|---|
| 102 | };
|
|---|
| 103 | });
|
|---|
| 104 |
|
|---|
| 105 | const transformsToApply: Array<ManifestTransform> = [];
|
|---|
| 106 |
|
|---|
| 107 | if (maximumFileSizeToCacheInBytes) {
|
|---|
| 108 | transformsToApply.push(maximumSizeTransform(maximumFileSizeToCacheInBytes));
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | if (modifyURLPrefix) {
|
|---|
| 112 | transformsToApply.push(modifyURLPrefixTransform(modifyURLPrefix));
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | if (dontCacheBustURLsMatching) {
|
|---|
| 116 | transformsToApply.push(
|
|---|
| 117 | noRevisionForURLsMatchingTransform(dontCacheBustURLsMatching),
|
|---|
| 118 | );
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | // Run any manifestTransforms functions second-to-last.
|
|---|
| 122 | if (manifestTransforms) {
|
|---|
| 123 | transformsToApply.push(...manifestTransforms);
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | // Run additionalManifestEntriesTransform last.
|
|---|
| 127 | if (additionalManifestEntries) {
|
|---|
| 128 | transformsToApply.push(
|
|---|
| 129 | additionalManifestEntriesTransform(additionalManifestEntries),
|
|---|
| 130 | );
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | let transformedManifest: Array<ManifestEntry & {size: number}> =
|
|---|
| 134 | normalizedManifest;
|
|---|
| 135 | for (const transform of transformsToApply) {
|
|---|
| 136 | const result = await transform(transformedManifest, transformParam);
|
|---|
| 137 | if (!('manifest' in result)) {
|
|---|
| 138 | throw new Error(errors['bad-manifest-transforms-return-value']);
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | transformedManifest = result.manifest;
|
|---|
| 142 | allWarnings.push(...(result.warnings || []));
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | // Generate some metadata about the manifest before we clear out the size
|
|---|
| 146 | // properties from each entry.
|
|---|
| 147 | const count = transformedManifest.length;
|
|---|
| 148 | let size = 0;
|
|---|
| 149 | for (const manifestEntry of transformedManifest as Array<
|
|---|
| 150 | ManifestEntry & {size?: number}
|
|---|
| 151 | >) {
|
|---|
| 152 | size += manifestEntry.size || 0;
|
|---|
| 153 | delete manifestEntry.size;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | return {
|
|---|
| 157 | count,
|
|---|
| 158 | size,
|
|---|
| 159 | manifestEntries: transformedManifest as Array<ManifestEntry>,
|
|---|
| 160 | warnings: allWarnings,
|
|---|
| 161 | };
|
|---|
| 162 | }
|
|---|