write-sw-using-default-template.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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.writeSWUsingDefaultTemplate = void 0;
  13. const fs_extra_1 = __importDefault(require("fs-extra"));
  14. const upath_1 = __importDefault(require("upath"));
  15. const bundle_1 = require("./bundle");
  16. const errors_1 = require("./errors");
  17. const populate_sw_template_1 = require("./populate-sw-template");
  18. async function writeSWUsingDefaultTemplate({ babelPresetEnvTargets, cacheId, cleanupOutdatedCaches, clientsClaim, directoryIndex, disableDevLogs, ignoreURLParametersMatching, importScripts, inlineWorkboxRuntime, manifestEntries, mode, navigateFallback, navigateFallbackDenylist, navigateFallbackAllowlist, navigationPreload, offlineGoogleAnalytics, runtimeCaching, skipWaiting, sourcemap, swDest, }) {
  19. const outputDir = upath_1.default.dirname(swDest);
  20. try {
  21. await fs_extra_1.default.mkdirp(outputDir);
  22. }
  23. catch (error) {
  24. throw new Error(`${errors_1.errors['unable-to-make-sw-directory']}. ` +
  25. `'${error instanceof Error && error.message ? error.message : ''}'`);
  26. }
  27. const unbundledCode = (0, populate_sw_template_1.populateSWTemplate)({
  28. cacheId,
  29. cleanupOutdatedCaches,
  30. clientsClaim,
  31. directoryIndex,
  32. disableDevLogs,
  33. ignoreURLParametersMatching,
  34. importScripts,
  35. manifestEntries,
  36. navigateFallback,
  37. navigateFallbackDenylist,
  38. navigateFallbackAllowlist,
  39. navigationPreload,
  40. offlineGoogleAnalytics,
  41. runtimeCaching,
  42. skipWaiting,
  43. });
  44. try {
  45. const files = await (0, bundle_1.bundle)({
  46. babelPresetEnvTargets,
  47. inlineWorkboxRuntime,
  48. mode,
  49. sourcemap,
  50. swDest,
  51. unbundledCode,
  52. });
  53. const filePaths = [];
  54. for (const file of files) {
  55. const filePath = upath_1.default.resolve(file.name);
  56. filePaths.push(filePath);
  57. await fs_extra_1.default.writeFile(filePath, file.contents);
  58. }
  59. return filePaths;
  60. }
  61. catch (error) {
  62. const err = error;
  63. if (err.code === 'EISDIR') {
  64. // See https://github.com/GoogleChrome/workbox/issues/612
  65. throw new Error(errors_1.errors['sw-write-failure-directory']);
  66. }
  67. throw new Error(`${errors_1.errors['sw-write-failure']} '${err.message}'`);
  68. }
  69. }
  70. exports.writeSWUsingDefaultTemplate = writeSWUsingDefaultTemplate;