generate-sw.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.generateSW = void 0;
  13. const upath_1 = __importDefault(require("upath"));
  14. const get_file_manifest_entries_1 = require("./lib/get-file-manifest-entries");
  15. const rebase_path_1 = require("./lib/rebase-path");
  16. const validate_options_1 = require("./lib/validate-options");
  17. const write_sw_using_default_template_1 = require("./lib/write-sw-using-default-template");
  18. /**
  19. * This method creates a list of URLs to precache, referred to as a "precache
  20. * manifest", based on the options you provide.
  21. *
  22. * It also takes in additional options that configures the service worker's
  23. * behavior, like any `runtimeCaching` rules it should use.
  24. *
  25. * Based on the precache manifest and the additional configuration, it writes
  26. * a ready-to-use service worker file to disk at `swDest`.
  27. *
  28. * ```
  29. * // The following lists some common options; see the rest of the documentation
  30. * // for the full set of options and defaults.
  31. * const {count, size, warnings} = await generateSW({
  32. * dontCacheBustURLsMatching: [new RegExp('...')],
  33. * globDirectory: '...',
  34. * globPatterns: ['...', '...'],
  35. * maximumFileSizeToCacheInBytes: ...,
  36. * navigateFallback: '...',
  37. * runtimeCaching: [{
  38. * // Routing via a matchCallback function:
  39. * urlPattern: ({request, url}) => ...,
  40. * handler: '...',
  41. * options: {
  42. * cacheName: '...',
  43. * expiration: {
  44. * maxEntries: ...,
  45. * },
  46. * },
  47. * }, {
  48. * // Routing via a RegExp:
  49. * urlPattern: new RegExp('...'),
  50. * handler: '...',
  51. * options: {
  52. * cacheName: '...',
  53. * plugins: [..., ...],
  54. * },
  55. * }],
  56. * skipWaiting: ...,
  57. * swDest: '...',
  58. * });
  59. * ```
  60. *
  61. * @memberof workbox-build
  62. */
  63. async function generateSW(config) {
  64. const options = (0, validate_options_1.validateGenerateSWOptions)(config);
  65. let entriesResult;
  66. if (options.globDirectory) {
  67. // Make sure we leave swDest out of the precache manifest.
  68. options.globIgnores.push((0, rebase_path_1.rebasePath)({
  69. baseDirectory: options.globDirectory,
  70. file: options.swDest,
  71. }));
  72. // If we create an extra external runtime file, ignore that, too.
  73. // See https://rollupjs.org/guide/en/#outputchunkfilenames for naming.
  74. if (!options.inlineWorkboxRuntime) {
  75. const swDestDir = upath_1.default.dirname(options.swDest);
  76. const workboxRuntimeFile = upath_1.default.join(swDestDir, 'workbox-*.js');
  77. options.globIgnores.push((0, rebase_path_1.rebasePath)({
  78. baseDirectory: options.globDirectory,
  79. file: workboxRuntimeFile,
  80. }));
  81. }
  82. // We've previously asserted that options.globDirectory is set, so this
  83. // should be a safe cast.
  84. entriesResult = await (0, get_file_manifest_entries_1.getFileManifestEntries)(options);
  85. }
  86. else {
  87. entriesResult = {
  88. count: 0,
  89. manifestEntries: [],
  90. size: 0,
  91. warnings: [],
  92. };
  93. }
  94. const filePaths = await (0, write_sw_using_default_template_1.writeSWUsingDefaultTemplate)(Object.assign({
  95. manifestEntries: entriesResult.manifestEntries,
  96. }, options));
  97. return {
  98. filePaths,
  99. count: entriesResult.count,
  100. size: entriesResult.size,
  101. warnings: entriesResult.warnings,
  102. };
  103. }
  104. exports.generateSW = generateSW;