Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

generate-sw.js 7.8 KiB

3 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. 'use strict';
  2. var _regenerator = require('babel-runtime/regenerator');
  3. var _regenerator2 = _interopRequireDefault(_regenerator);
  4. var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');
  5. var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);
  6. var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
  7. var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
  8. var _assign = require('babel-runtime/core-js/object/assign');
  9. var _assign2 = _interopRequireDefault(_assign);
  10. var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
  11. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  12. var _createClass2 = require('babel-runtime/helpers/createClass');
  13. var _createClass3 = _interopRequireDefault(_createClass2);
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. /*
  16. Copyright 2017 Google Inc.
  17. Licensed under the Apache License, Version 2.0 (the "License");
  18. you may not use this file except in compliance with the License.
  19. You may obtain a copy of the License at
  20. https://www.apache.org/licenses/LICENSE-2.0
  21. Unless required by applicable law or agreed to in writing, software
  22. distributed under the License is distributed on an "AS IS" BASIS,
  23. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. See the License for the specific language governing permissions and
  25. limitations under the License.
  26. */
  27. var _require = require('workbox-build'),
  28. generateSWString = _require.generateSWString;
  29. var path = require('path');
  30. var convertStringToAsset = require('./lib/convert-string-to-asset');
  31. var getDefaultConfig = require('./lib/get-default-config');
  32. var formatManifestFilename = require('./lib/format-manifest-filename');
  33. var getAssetHash = require('./lib/get-asset-hash');
  34. var getManifestEntriesFromCompilation = require('./lib/get-manifest-entries-from-compilation');
  35. var getWorkboxSWImports = require('./lib/get-workbox-sw-imports');
  36. var relativeToOutputPath = require('./lib/relative-to-output-path');
  37. var sanitizeConfig = require('./lib/sanitize-config');
  38. var stringifyManifest = require('./lib/stringify-manifest');
  39. var warnAboutConfig = require('./lib/warn-about-config');
  40. /**
  41. * This class supports creating a new, ready-to-use service worker file as
  42. * part of the webpack compilation process.
  43. *
  44. * Use an instance of `GenerateSW` in the
  45. * [`plugins` array](https://webpack.js.org/concepts/plugins/#usage) of a
  46. * webpack config.
  47. *
  48. * @module workbox-webpack-plugin
  49. */
  50. var GenerateSW = function () {
  51. /**
  52. * Creates an instance of GenerateSW.
  53. *
  54. * @param {Object} [config] See the
  55. * [configuration guide](https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin#configuration)
  56. * for all supported options and defaults.
  57. */
  58. function GenerateSW() {
  59. var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  60. (0, _classCallCheck3.default)(this, GenerateSW);
  61. this.config = (0, _assign2.default)(getDefaultConfig(), {
  62. // Hardcode this default filename, since we don't have swSrc to read from
  63. // (like we do in InjectManifest).
  64. swDest: 'service-worker.js'
  65. }, config);
  66. }
  67. /**
  68. * @param {Object} compilation The webpack compilation.
  69. * @private
  70. */
  71. (0, _createClass3.default)(GenerateSW, [{
  72. key: 'handleEmit',
  73. value: function () {
  74. var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(compilation) {
  75. var configWarning, workboxSWImports, entries, importScriptsArray, manifestString, manifestAsset, manifestHash, manifestFilename, pathToManifestFile, workboxSWImport, sanitizedConfig, _ref2, swString, warnings, relSwDest;
  76. return _regenerator2.default.wrap(function _callee$(_context) {
  77. while (1) {
  78. switch (_context.prev = _context.next) {
  79. case 0:
  80. configWarning = warnAboutConfig(this.config);
  81. if (configWarning) {
  82. compilation.warnings.push(configWarning);
  83. }
  84. _context.next = 4;
  85. return getWorkboxSWImports(compilation, this.config);
  86. case 4:
  87. workboxSWImports = _context.sent;
  88. entries = getManifestEntriesFromCompilation(compilation, this.config);
  89. importScriptsArray = [].concat(this.config.importScripts);
  90. manifestString = stringifyManifest(entries);
  91. manifestAsset = convertStringToAsset(manifestString);
  92. manifestHash = getAssetHash(manifestAsset);
  93. manifestFilename = formatManifestFilename(this.config.precacheManifestFilename, manifestHash);
  94. pathToManifestFile = relativeToOutputPath(compilation, path.join(this.config.importsDirectory, manifestFilename));
  95. compilation.assets[pathToManifestFile] = manifestAsset;
  96. importScriptsArray.push((compilation.options.output.publicPath || '') + pathToManifestFile.split(path.sep).join('/'));
  97. // workboxSWImports might be null if importWorkboxFrom is 'disabled'.
  98. workboxSWImport = void 0;
  99. if (workboxSWImports) {
  100. if (workboxSWImports.length === 1) {
  101. // When importWorkboxFrom is 'cdn' or 'local', or a chunk name
  102. // that only contains one JavaScript asset, then this will be a one
  103. // element array, containing just the Workbox SW code.
  104. workboxSWImport = workboxSWImports[0];
  105. } else {
  106. // If importWorkboxFrom was a chunk name that contained multiple
  107. // JavaScript assets, then we don't know which contains the Workbox SW
  108. // code. Just import them first as part of the "main" importScripts().
  109. importScriptsArray.unshift.apply(importScriptsArray, (0, _toConsumableArray3.default)(workboxSWImports));
  110. }
  111. }
  112. sanitizedConfig = sanitizeConfig.forGenerateSWString(this.config);
  113. // If globPatterns isn't explicitly set, then default to [], instead of
  114. // the workbox-build.generateSWString() default.
  115. sanitizedConfig.globPatterns = sanitizedConfig.globPatterns || [];
  116. sanitizedConfig.importScripts = importScriptsArray;
  117. sanitizedConfig.workboxSWImport = workboxSWImport;
  118. _context.next = 22;
  119. return generateSWString(sanitizedConfig);
  120. case 22:
  121. _ref2 = _context.sent;
  122. swString = _ref2.swString;
  123. warnings = _ref2.warnings;
  124. compilation.warnings = compilation.warnings.concat(warnings || []);
  125. relSwDest = relativeToOutputPath(compilation, this.config.swDest);
  126. compilation.assets[relSwDest] = convertStringToAsset(swString);
  127. case 28:
  128. case 'end':
  129. return _context.stop();
  130. }
  131. }
  132. }, _callee, this);
  133. }));
  134. function handleEmit(_x2) {
  135. return _ref.apply(this, arguments);
  136. }
  137. return handleEmit;
  138. }()
  139. /**
  140. * @param {Object} [compiler] default compiler object passed from webpack
  141. *
  142. * @private
  143. */
  144. }, {
  145. key: 'apply',
  146. value: function apply(compiler) {
  147. var _this = this;
  148. if ('hooks' in compiler) {
  149. // We're in webpack 4+.
  150. compiler.hooks.emit.tapPromise(this.constructor.name, function (compilation) {
  151. return _this.handleEmit(compilation);
  152. });
  153. } else {
  154. // We're in webpack 2 or 3.
  155. compiler.plugin('emit', function (compilation, callback) {
  156. _this.handleEmit(compilation).then(callback).catch(callback);
  157. });
  158. }
  159. }
  160. }]);
  161. return GenerateSW;
  162. }();
  163. module.exports = GenerateSW;