populate-sw-template.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 runtimeCachingConverter = require('./runtime-caching-converter');
  12. const stringifyWithoutComments = require('./stringify-without-comments');
  13. module.exports = ({
  14. cacheId,
  15. cleanupOutdatedCaches,
  16. clientsClaim,
  17. directoryIndex,
  18. ignoreURLParametersMatching,
  19. importScripts,
  20. manifestEntries,
  21. modulePathPrefix,
  22. navigateFallback,
  23. navigateFallbackBlacklist,
  24. navigateFallbackWhitelist,
  25. navigationPreload,
  26. offlineGoogleAnalytics,
  27. runtimeCaching,
  28. skipWaiting,
  29. workboxSWImport
  30. }) => {
  31. // These are all options that can be passed to the precacheAndRoute() method.
  32. const precacheOptions = {
  33. directoryIndex,
  34. // An array of RegExp objects can't be serialized by JSON.stringify()'s
  35. // default behavior, so if it's given, convert it manually.
  36. ignoreURLParametersMatching: ignoreURLParametersMatching ? [] : undefined
  37. };
  38. let precacheOptionsString = JSON.stringify(precacheOptions, null, 2);
  39. if (ignoreURLParametersMatching) {
  40. precacheOptionsString = precacheOptionsString.replace(`"ignoreURLParametersMatching": []`, `"ignoreURLParametersMatching": [` + `${ignoreURLParametersMatching.join(', ')}]`);
  41. }
  42. let offlineAnalyticsConfigString;
  43. if (offlineGoogleAnalytics) {
  44. // If offlineGoogleAnalytics is a truthy value, we need to convert it to the
  45. // format expected by the template.
  46. offlineAnalyticsConfigString = offlineGoogleAnalytics === true ? // If it's the literal value true, then use an empty config string.
  47. '{}' : // Otherwise, convert the config object into a more complex string, taking
  48. // into account the fact that functions might need to be stringified.
  49. stringifyWithoutComments(offlineGoogleAnalytics);
  50. }
  51. try {
  52. const populatedTemplate = template(swTemplate)({
  53. cacheId,
  54. cleanupOutdatedCaches,
  55. clientsClaim,
  56. importScripts,
  57. manifestEntries,
  58. modulePathPrefix,
  59. navigateFallback,
  60. navigateFallbackBlacklist,
  61. navigateFallbackWhitelist,
  62. navigationPreload,
  63. offlineAnalyticsConfigString,
  64. precacheOptionsString,
  65. skipWaiting,
  66. runtimeCaching: runtimeCachingConverter(runtimeCaching),
  67. workboxSWImport
  68. }); // Clean up multiple blank lines.
  69. return populatedTemplate.replace(/\n{3,}/g, '\n\n').trim() + '\n';
  70. } catch (error) {
  71. throw new Error(`${errors['populating-sw-tmpl-failed']} '${error.message}'`);
  72. }
  73. };