populate-sw-template.js 3.2 KB

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