get-file-manifest-entries.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 assert = require('assert');
  9. const errors = require('./errors');
  10. const transformManifest = require('./transform-manifest');
  11. const getCompositeDetails = require('./get-composite-details');
  12. const getFileDetails = require('./get-file-details');
  13. const getStringDetails = require('./get-string-details');
  14. module.exports = async ({
  15. additionalManifestEntries,
  16. dontCacheBustURLsMatching,
  17. globDirectory,
  18. globFollow,
  19. globIgnores,
  20. globPatterns,
  21. globStrict,
  22. manifestTransforms,
  23. maximumFileSizeToCacheInBytes,
  24. modifyURLPrefix,
  25. swDest,
  26. templatedURLs
  27. }) => {
  28. const warnings = []; // Initialize to an empty array so that we can still pass something to
  29. // transformManifest() and get a normalized output.
  30. let fileDetails = [];
  31. const fileSet = new Set();
  32. if (globDirectory) {
  33. try {
  34. fileDetails = globPatterns.reduce((accumulated, globPattern) => {
  35. const {
  36. globbedFileDetails,
  37. warning
  38. } = getFileDetails({
  39. globDirectory,
  40. globFollow,
  41. globIgnores,
  42. globPattern,
  43. globStrict
  44. });
  45. if (warning) {
  46. warnings.push(warning);
  47. }
  48. globbedFileDetails.forEach(fileDetails => {
  49. if (fileSet.has(fileDetails.file)) {
  50. return;
  51. }
  52. fileSet.add(fileDetails.file);
  53. accumulated.push(fileDetails);
  54. });
  55. return accumulated;
  56. }, []);
  57. } catch (error) {
  58. // If there's an exception thrown while globbing, then report
  59. // it back as a warning, and don't consider it fatal.
  60. warnings.push(error.message);
  61. }
  62. }
  63. if (templatedURLs) {
  64. for (const url of Object.keys(templatedURLs)) {
  65. assert(!fileSet.has(url), errors['templated-url-matches-glob']);
  66. const dependencies = templatedURLs[url];
  67. if (Array.isArray(dependencies)) {
  68. const details = dependencies.reduce((previous, globPattern) => {
  69. try {
  70. const {
  71. globbedFileDetails,
  72. warning
  73. } = getFileDetails({
  74. globDirectory,
  75. globFollow,
  76. globIgnores,
  77. globPattern,
  78. globStrict
  79. });
  80. if (warning) {
  81. warnings.push(warning);
  82. }
  83. return previous.concat(globbedFileDetails);
  84. } catch (error) {
  85. const debugObj = {};
  86. debugObj[url] = dependencies;
  87. throw new Error(`${errors['bad-template-urls-asset']} ` + `'${globPattern}' from '${JSON.stringify(debugObj)}':\n` + error);
  88. }
  89. }, []);
  90. fileDetails.push(getCompositeDetails(url, details));
  91. } else if (typeof dependencies === 'string') {
  92. fileDetails.push(getStringDetails(url, dependencies));
  93. }
  94. }
  95. }
  96. const transformedManifest = await transformManifest({
  97. additionalManifestEntries,
  98. dontCacheBustURLsMatching,
  99. fileDetails,
  100. manifestTransforms,
  101. maximumFileSizeToCacheInBytes,
  102. modifyURLPrefix
  103. });
  104. if (warnings.length > 0) {
  105. transformedManifest.warnings.push(...warnings);
  106. }
  107. return transformedManifest;
  108. };