transform-manifest.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. "use strict";
  2. /*
  3. Copyright 2018 Google LLC
  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. const errors = require('./errors');
  9. const additionalManifestEntriesTransform = require('./additional-manifest-entries-transform');
  10. const maximumSizeTransform = require('./maximum-size-transform');
  11. const modifyURLPrefixTransform = require('./modify-url-prefix-transform');
  12. const noRevisionForURLsMatchingTransform = require('./no-revision-for-urls-matching-transform');
  13. /**
  14. * A `ManifestTransform` function can be used to modify the modify the `url` or
  15. * `revision` properties of some or all of the
  16. * {@link module:workbox-build.ManifestEntry|ManifestEntries} in the manifest.
  17. *
  18. * Deleting the `revision` property of an entry will cause
  19. * the corresponding `url` to be precached without cache-busting parameters
  20. * applied, which is to say, it implies that the URL itself contains
  21. * proper versioning info. If the `revision` property is present, it must be
  22. * set to a string.
  23. *
  24. * @example A transformation that prepended the origin of a CDN for any
  25. * URL starting with '/assets/' could be implemented as:
  26. *
  27. * const cdnTransform = async (manifestEntries) => {
  28. * const manifest = manifestEntries.map(entry => {
  29. * const cdnOrigin = 'https://example.com';
  30. * if (entry.url.startsWith('/assets/')) {
  31. * entry.url = cdnOrigin + entry.url;
  32. * }
  33. * return entry;
  34. * });
  35. * return {manifest, warnings: []};
  36. * };
  37. *
  38. * @example A transformation that nulls the revision field when the
  39. * URL contains an 8-character hash surrounded by '.', indicating that it
  40. * already contains revision information:
  41. *
  42. * const removeRevisionTransform = async (manifestEntries) => {
  43. * const manifest = manifestEntries.map(entry => {
  44. * const hashRegExp = /\.\w{8}\./;
  45. * if (entry.url.match(hashRegExp)) {
  46. * entry.revision = null;
  47. * }
  48. * return entry;
  49. * });
  50. * return {manifest, warnings: []};
  51. * };
  52. *
  53. * @callback ManifestTransform
  54. * @param {Array<module:workbox-build.ManifestEntry>} manifestEntries The full
  55. * array of entries, prior to the current transformation.
  56. * @param {Object} [compilation] When used in the webpack plugins, this param
  57. * will be set to the current `compilation`.
  58. * @return {Promise<module:workbox-build.ManifestTransformResult>}
  59. * The array of entries with the transformation applied, and optionally, any
  60. * warnings that should be reported back to the build tool.
  61. *
  62. * @memberof module:workbox-build
  63. */
  64. module.exports = async ({
  65. additionalManifestEntries,
  66. dontCacheBustURLsMatching,
  67. fileDetails,
  68. manifestTransforms,
  69. maximumFileSizeToCacheInBytes,
  70. modifyURLPrefix,
  71. transformParam
  72. }) => {
  73. let allWarnings = []; // Take the array of fileDetail objects and convert it into an array of
  74. // {url, revision, size} objects, with \ replaced with /.
  75. const normalizedManifest = fileDetails.map(fileDetails => {
  76. return {
  77. url: fileDetails.file.replace(/\\/g, '/'),
  78. revision: fileDetails.hash,
  79. size: fileDetails.size
  80. };
  81. });
  82. const transformsToApply = [];
  83. if (maximumFileSizeToCacheInBytes) {
  84. transformsToApply.push(maximumSizeTransform(maximumFileSizeToCacheInBytes));
  85. }
  86. if (modifyURLPrefix) {
  87. transformsToApply.push(modifyURLPrefixTransform(modifyURLPrefix));
  88. }
  89. if (dontCacheBustURLsMatching) {
  90. transformsToApply.push(noRevisionForURLsMatchingTransform(dontCacheBustURLsMatching));
  91. } // Run any manifestTransforms functions second-to-last.
  92. if (manifestTransforms) {
  93. transformsToApply.push(...manifestTransforms);
  94. } // Run additionalManifestEntriesTransform last.
  95. if (additionalManifestEntries) {
  96. transformsToApply.push(additionalManifestEntriesTransform(additionalManifestEntries));
  97. }
  98. let transformedManifest = normalizedManifest;
  99. for (const transform of transformsToApply) {
  100. const result = await transform(transformedManifest, transformParam);
  101. if (!('manifest' in result)) {
  102. throw new Error(errors['bad-manifest-transforms-return-value']);
  103. }
  104. transformedManifest = result.manifest;
  105. allWarnings = allWarnings.concat(result.warnings || []);
  106. } // Generate some metadata about the manifest before we clear out the size
  107. // properties from each entry.
  108. const count = transformedManifest.length;
  109. let size = 0;
  110. for (const manifestEntry of transformedManifest) {
  111. size += manifestEntry.size || 0;
  112. delete manifestEntry.size;
  113. }
  114. return {
  115. count,
  116. size,
  117. manifestEntries: transformedManifest,
  118. warnings: allWarnings
  119. };
  120. };