generate-sw.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 path = require('path');
  11. const cdnUtils = require('../lib/cdn-utils');
  12. const checkForDeprecatedOptions = require('../lib/check-for-deprecated-options');
  13. const copyWorkboxLibraries = require('../lib/copy-workbox-libraries');
  14. const generateSWSchema = require('./options/generate-sw-schema');
  15. const getFileManifestEntries = require('../lib/get-file-manifest-entries');
  16. const validate = require('./options/validate');
  17. const writeServiceWorkerUsingDefaultTemplate = 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. * @param {Object} config Please refer to the
  29. * [configuration guide](https://developers.google.com/web/tools/workbox/modules/workbox-build#full_generatesw_config).
  30. * @return {Promise<{count: number, size: number, warnings: Array<string>}>}
  31. * A promise that resolves once the service worker file has been written to
  32. * `swDest`. The `size` property contains the aggregate size of all the
  33. * precached entries, in bytes, and the `count` property contains the total
  34. * number of precached entries. Any non-fatal warning messages will be returned
  35. * via `warnings`.
  36. *
  37. * @memberof module:workbox-build
  38. */
  39. function generateSW(_x) {
  40. return _generateSW.apply(this, arguments);
  41. }
  42. function _generateSW() {
  43. _generateSW = (0, _asyncToGenerator2.default)(function* (config) {
  44. // This check needs to be done before validation, since the deprecated options
  45. // will be renamed.
  46. const deprecationWarnings = checkForDeprecatedOptions(config);
  47. const options = validate(config, generateSWSchema);
  48. const destDirectory = path.dirname(options.swDest); // Do nothing if importWorkboxFrom is set to 'disabled'. Otherwise, check:
  49. if (options.importWorkboxFrom === 'cdn') {
  50. const cdnURL = cdnUtils.getModuleURL('workbox-sw');
  51. options.workboxSWImport = cdnURL;
  52. } else if (options.importWorkboxFrom === 'local') {
  53. // Copy over the dev + prod version of all of the core libraries.
  54. const workboxDirectoryName = yield copyWorkboxLibraries(destDirectory); // The Workbox library files should not be precached, since they're cached
  55. // automatically by virtue of being used with importScripts().
  56. options.globIgnores = [`**/${workboxDirectoryName}/*.+(js|mjs)*`].concat(options.globIgnores || []);
  57. const workboxSWPkg = require(`workbox-sw/package.json`);
  58. const workboxSWFilename = path.basename(workboxSWPkg.main);
  59. options.workboxSWImport = `${workboxDirectoryName}/${workboxSWFilename}`;
  60. options.modulePathPrefix = workboxDirectoryName;
  61. }
  62. const _ref = yield getFileManifestEntries(options),
  63. count = _ref.count,
  64. size = _ref.size,
  65. manifestEntries = _ref.manifestEntries,
  66. warnings = _ref.warnings;
  67. yield writeServiceWorkerUsingDefaultTemplate(Object.assign({
  68. manifestEntries
  69. }, options)); // Add in any deprecation warnings.
  70. warnings.push(...deprecationWarnings);
  71. return {
  72. count,
  73. size,
  74. warnings
  75. };
  76. });
  77. return _generateSW.apply(this, arguments);
  78. }
  79. module.exports = generateSW;