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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
  4. /*
  5. Copyright 2018 Google LLC
  6. Use of this source code is governed by an MIT-style
  7. license that can be found in the LICENSE file or at
  8. https://opensource.org/licenses/MIT.
  9. */
  10. const fse = require('fs-extra');
  11. const path = require('path');
  12. const errors = require('./errors');
  13. const populateSWTemplate = require('./populate-sw-template');
  14. module.exports =
  15. /*#__PURE__*/
  16. function () {
  17. var _ref = (0, _asyncToGenerator2.default)(function* ({
  18. cacheId,
  19. cleanupOutdatedCaches,
  20. clientsClaim,
  21. directoryIndex,
  22. handleFetch,
  23. ignoreURLParametersMatching,
  24. importScripts,
  25. manifestEntries,
  26. modulePathPrefix,
  27. navigateFallback,
  28. navigateFallbackBlacklist,
  29. navigateFallbackWhitelist,
  30. navigationPreload,
  31. offlineGoogleAnalytics,
  32. runtimeCaching,
  33. skipWaiting,
  34. swDest,
  35. workboxSWImport
  36. }) {
  37. try {
  38. yield fse.mkdirp(path.dirname(swDest));
  39. } catch (error) {
  40. throw new Error(`${errors['unable-to-make-sw-directory']}. ` + `'${error.message}'`);
  41. }
  42. const populatedTemplate = populateSWTemplate({
  43. cacheId,
  44. cleanupOutdatedCaches,
  45. clientsClaim,
  46. directoryIndex,
  47. handleFetch,
  48. ignoreURLParametersMatching,
  49. importScripts,
  50. manifestEntries,
  51. modulePathPrefix,
  52. navigateFallback,
  53. navigateFallbackBlacklist,
  54. navigateFallbackWhitelist,
  55. navigationPreload,
  56. offlineGoogleAnalytics,
  57. runtimeCaching,
  58. skipWaiting,
  59. workboxSWImport
  60. });
  61. try {
  62. yield fse.writeFile(swDest, populatedTemplate);
  63. } catch (error) {
  64. if (error.code === 'EISDIR') {
  65. // See https://github.com/GoogleChrome/workbox/issues/612
  66. throw new Error(errors['sw-write-failure-directory']);
  67. }
  68. throw new Error(`${errors['sw-write-failure']}. '${error.message}'`);
  69. }
  70. });
  71. return function (_x) {
  72. return _ref.apply(this, arguments);
  73. };
  74. }();