generate-sw.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 _require = require('workbox-build'),
  11. generateSWString = _require.generateSWString;
  12. const path = require('path');
  13. const convertStringToAsset = require('./lib/convert-string-to-asset');
  14. const getDefaultConfig = require('./lib/get-default-config');
  15. const formatManifestFilename = require('./lib/format-manifest-filename');
  16. const getAssetHash = require('./lib/get-asset-hash');
  17. const getManifestEntriesFromCompilation = require('./lib/get-manifest-entries-from-compilation');
  18. const getWorkboxSWImports = require('./lib/get-workbox-sw-imports');
  19. const relativeToOutputPath = require('./lib/relative-to-output-path');
  20. const sanitizeConfig = require('./lib/sanitize-config');
  21. const stringifyManifest = require('./lib/stringify-manifest');
  22. const warnAboutConfig = require('./lib/warn-about-config');
  23. /**
  24. * This class supports creating a new, ready-to-use service worker file as
  25. * part of the webpack compilation process.
  26. *
  27. * Use an instance of `GenerateSW` in the
  28. * [`plugins` array](https://webpack.js.org/concepts/plugins/#usage) of a
  29. * webpack config.
  30. *
  31. * @module workbox-webpack-plugin
  32. */
  33. class GenerateSW {
  34. /**
  35. * Creates an instance of GenerateSW.
  36. *
  37. * @param {Object} [config] See the
  38. * [configuration guide](https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin#configuration)
  39. * for all supported options and defaults.
  40. */
  41. constructor(config = {}) {
  42. this.config = Object.assign(getDefaultConfig(), {
  43. // Hardcode this default filename, since we don't have swSrc to read from
  44. // (like we do in InjectManifest).
  45. swDest: 'service-worker.js'
  46. }, config);
  47. }
  48. /**
  49. * @param {Object} compilation The webpack compilation.
  50. * @private
  51. */
  52. handleEmit(compilation) {
  53. var _this = this;
  54. return (0, _asyncToGenerator2.default)(function* () {
  55. const configWarning = warnAboutConfig(_this.config);
  56. if (configWarning) {
  57. compilation.warnings.push(configWarning);
  58. }
  59. const workboxSWImports = yield getWorkboxSWImports(compilation, _this.config);
  60. const entries = getManifestEntriesFromCompilation(compilation, _this.config);
  61. const importScriptsArray = [].concat(_this.config.importScripts);
  62. const manifestString = stringifyManifest(entries);
  63. const manifestAsset = convertStringToAsset(manifestString);
  64. const manifestHash = getAssetHash(manifestAsset);
  65. const manifestFilename = formatManifestFilename(_this.config.precacheManifestFilename, manifestHash);
  66. const pathToManifestFile = relativeToOutputPath(compilation, path.join(_this.config.importsDirectory, manifestFilename));
  67. compilation.assets[pathToManifestFile] = manifestAsset;
  68. importScriptsArray.push((compilation.options.output.publicPath || '') + pathToManifestFile.split(path.sep).join('/')); // workboxSWImports might be null if importWorkboxFrom is 'disabled'.
  69. let workboxSWImport;
  70. if (workboxSWImports) {
  71. if (workboxSWImports.length === 1) {
  72. // When importWorkboxFrom is 'cdn' or 'local', or a chunk name
  73. // that only contains one JavaScript asset, then this will be a one
  74. // element array, containing just the Workbox SW code.
  75. workboxSWImport = workboxSWImports[0];
  76. } else {
  77. // If importWorkboxFrom was a chunk name that contained multiple
  78. // JavaScript assets, then we don't know which contains the Workbox SW
  79. // code. Just import them first as part of the "main" importScripts().
  80. importScriptsArray.unshift(...workboxSWImports);
  81. }
  82. }
  83. const sanitizedConfig = sanitizeConfig.forGenerateSWString(_this.config); // If globPatterns isn't explicitly set, then default to [], instead of
  84. // the workbox-build.generateSWString() default.
  85. sanitizedConfig.globPatterns = sanitizedConfig.globPatterns || [];
  86. sanitizedConfig.importScripts = importScriptsArray;
  87. sanitizedConfig.workboxSWImport = workboxSWImport;
  88. const _ref = yield generateSWString(sanitizedConfig),
  89. swString = _ref.swString,
  90. warnings = _ref.warnings;
  91. compilation.warnings = compilation.warnings.concat(warnings || []);
  92. const relSwDest = relativeToOutputPath(compilation, _this.config.swDest);
  93. compilation.assets[relSwDest] = convertStringToAsset(swString);
  94. })();
  95. }
  96. /**
  97. * @param {Object} [compiler] default compiler object passed from webpack
  98. *
  99. * @private
  100. */
  101. apply(compiler) {
  102. if ('hooks' in compiler) {
  103. // We're in webpack 4+.
  104. compiler.hooks.emit.tapPromise(this.constructor.name, compilation => this.handleEmit(compilation));
  105. } else {
  106. // We're in webpack 2 or 3.
  107. compiler.plugin('emit', (compilation, callback) => {
  108. this.handleEmit(compilation).then(callback).catch(callback);
  109. });
  110. }
  111. }
  112. }
  113. module.exports = GenerateSW;