get-file-manifest-entries.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
  4. /*
  5. Copyright 2018 Google LLC
  6. Use of this source code is governed by an MIT-style
  7. license that can be found in the LICENSE file or at
  8. https://opensource.org/licenses/MIT.
  9. */
  10. const assert = require('assert');
  11. const path = require('path');
  12. const errors = require('./errors');
  13. const filterFiles = require('./filter-files');
  14. const getCompositeDetails = require('./get-composite-details');
  15. const getFileDetails = require('./get-file-details');
  16. const getStringDetails = require('./get-string-details');
  17. module.exports =
  18. /*#__PURE__*/
  19. function () {
  20. var _ref = (0, _asyncToGenerator2.default)(function* ({
  21. dontCacheBustURLsMatching,
  22. globDirectory,
  23. globFollow,
  24. globIgnores,
  25. globPatterns,
  26. globStrict,
  27. manifestTransforms,
  28. maximumFileSizeToCacheInBytes,
  29. modifyURLPrefix,
  30. swDest,
  31. templatedURLs
  32. }) {
  33. const warnings = []; // Initialize to an empty array so that we can still pass something to
  34. // filterFiles() and get a normalized output.
  35. let fileDetails = [];
  36. const fileSet = new Set();
  37. if (globDirectory) {
  38. if (swDest) {
  39. // Ensure that we ignore the SW file we're eventually writing to disk.
  40. globIgnores.push(`**/${path.basename(swDest)}`);
  41. }
  42. try {
  43. fileDetails = globPatterns.reduce((accumulated, globPattern) => {
  44. const globbedFileDetails = getFileDetails({
  45. globDirectory,
  46. globFollow,
  47. globIgnores,
  48. globPattern,
  49. globStrict
  50. });
  51. globbedFileDetails.forEach(fileDetails => {
  52. if (fileSet.has(fileDetails.file)) {
  53. return;
  54. }
  55. fileSet.add(fileDetails.file);
  56. accumulated.push(fileDetails);
  57. });
  58. return accumulated;
  59. }, []);
  60. } catch (error) {
  61. // If there's an exception thrown while globbing, then report
  62. // it back as a warning, and don't consider it fatal.
  63. warnings.push(error.message);
  64. }
  65. }
  66. if (templatedURLs) {
  67. var _arr = Object.keys(templatedURLs);
  68. for (var _i = 0; _i < _arr.length; _i++) {
  69. let url = _arr[_i];
  70. assert(!fileSet.has(url), errors['templated-url-matches-glob']);
  71. const dependencies = templatedURLs[url];
  72. if (Array.isArray(dependencies)) {
  73. const details = dependencies.reduce((previous, globPattern) => {
  74. try {
  75. const globbedFileDetails = getFileDetails({
  76. globDirectory,
  77. globFollow,
  78. globIgnores,
  79. globPattern,
  80. globStrict
  81. });
  82. return previous.concat(globbedFileDetails);
  83. } catch (error) {
  84. const debugObj = {};
  85. debugObj[url] = dependencies;
  86. throw new Error(`${errors['bad-template-urls-asset']} ` + `'${globPattern}' from '${JSON.stringify(debugObj)}':\n` + error);
  87. }
  88. }, []);
  89. fileDetails.push(getCompositeDetails(url, details));
  90. } else if (typeof dependencies === 'string') {
  91. fileDetails.push(getStringDetails(url, dependencies));
  92. }
  93. }
  94. }
  95. const filteredFiles = filterFiles({
  96. fileDetails,
  97. maximumFileSizeToCacheInBytes,
  98. modifyURLPrefix,
  99. dontCacheBustURLsMatching,
  100. manifestTransforms
  101. });
  102. if (warnings.length > 0) {
  103. filteredFiles.warnings.push(...warnings);
  104. }
  105. return filteredFiles;
  106. });
  107. return function (_x) {
  108. return _ref.apply(this, arguments);
  109. };
  110. }();