populate-sw-template.js 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. var __importDefault = (this && this.__importDefault) || function (mod) {
  9. return (mod && mod.__esModule) ? mod : { "default": mod };
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. exports.populateSWTemplate = void 0;
  13. const template_1 = __importDefault(require("lodash/template"));
  14. const errors_1 = require("./errors");
  15. const module_registry_1 = require("./module-registry");
  16. const runtime_caching_converter_1 = require("./runtime-caching-converter");
  17. const stringify_without_comments_1 = require("./stringify-without-comments");
  18. const sw_template_1 = require("../templates/sw-template");
  19. function populateSWTemplate({ cacheId, cleanupOutdatedCaches, clientsClaim, directoryIndex, disableDevLogs, ignoreURLParametersMatching, importScripts, manifestEntries = [], navigateFallback, navigateFallbackDenylist, navigateFallbackAllowlist, navigationPreload, offlineGoogleAnalytics, runtimeCaching = [], skipWaiting, }) {
  20. // There needs to be at least something to precache, or else runtime caching.
  21. if (!((manifestEntries === null || manifestEntries === void 0 ? void 0 : manifestEntries.length) > 0 || runtimeCaching.length > 0)) {
  22. throw new Error(errors_1.errors['no-manifest-entries-or-runtime-caching']);
  23. }
  24. // These are all options that can be passed to the precacheAndRoute() method.
  25. const precacheOptions = {
  26. directoryIndex,
  27. // An array of RegExp objects can't be serialized by JSON.stringify()'s
  28. // default behavior, so if it's given, convert it manually.
  29. ignoreURLParametersMatching: ignoreURLParametersMatching
  30. ? []
  31. : undefined,
  32. };
  33. let precacheOptionsString = JSON.stringify(precacheOptions, null, 2);
  34. if (ignoreURLParametersMatching) {
  35. precacheOptionsString = precacheOptionsString.replace(`"ignoreURLParametersMatching": []`, `"ignoreURLParametersMatching": [` +
  36. `${ignoreURLParametersMatching.join(', ')}]`);
  37. }
  38. let offlineAnalyticsConfigString = undefined;
  39. if (offlineGoogleAnalytics) {
  40. // If offlineGoogleAnalytics is a truthy value, we need to convert it to the
  41. // format expected by the template.
  42. offlineAnalyticsConfigString =
  43. offlineGoogleAnalytics === true
  44. ? // If it's the literal value true, then use an empty config string.
  45. '{}'
  46. : // Otherwise, convert the config object into a more complex string, taking
  47. // into account the fact that functions might need to be stringified.
  48. (0, stringify_without_comments_1.stringifyWithoutComments)(offlineGoogleAnalytics);
  49. }
  50. const moduleRegistry = new module_registry_1.ModuleRegistry();
  51. try {
  52. const populatedTemplate = (0, template_1.default)(sw_template_1.swTemplate)({
  53. cacheId,
  54. cleanupOutdatedCaches,
  55. clientsClaim,
  56. disableDevLogs,
  57. importScripts,
  58. manifestEntries,
  59. navigateFallback,
  60. navigateFallbackDenylist,
  61. navigateFallbackAllowlist,
  62. navigationPreload,
  63. offlineAnalyticsConfigString,
  64. precacheOptionsString,
  65. runtimeCaching: (0, runtime_caching_converter_1.runtimeCachingConverter)(moduleRegistry, runtimeCaching),
  66. skipWaiting,
  67. use: moduleRegistry.use.bind(moduleRegistry),
  68. });
  69. const workboxImportStatements = moduleRegistry.getImportStatements();
  70. // We need the import statements for all of the Workbox runtime modules
  71. // prepended, so that the correct bundle can be created.
  72. return workboxImportStatements.join('\n') + populatedTemplate;
  73. }
  74. catch (error) {
  75. throw new Error(`${errors_1.errors['populating-sw-tmpl-failed']} '${error instanceof Error && error.message ? error.message : ''}'`);
  76. }
  77. }
  78. exports.populateSWTemplate = populateSWTemplate;