inject-manifest.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 assert = require('assert');
  11. const path = require('path');
  12. const _require = require('workbox-build'),
  13. getManifest = _require.getManifest;
  14. const convertStringToAsset = require('./lib/convert-string-to-asset');
  15. const getDefaultConfig = require('./lib/get-default-config');
  16. const formatManifestFilename = require('./lib/format-manifest-filename');
  17. const getAssetHash = require('./lib/get-asset-hash');
  18. const getManifestEntriesFromCompilation = require('./lib/get-manifest-entries-from-compilation');
  19. const getWorkboxSWImports = require('./lib/get-workbox-sw-imports');
  20. const readFileWrapper = require('./lib/read-file-wrapper');
  21. const relativeToOutputPath = require('./lib/relative-to-output-path');
  22. const sanitizeConfig = require('./lib/sanitize-config');
  23. const stringifyManifest = require('./lib/stringify-manifest');
  24. const warnAboutConfig = require('./lib/warn-about-config');
  25. /**
  26. * This class supports taking an existing service worker file which already
  27. * uses Workbox, and injecting a reference to a [precache manifest]() into it,
  28. * allowing it to efficiently precache the assets created by a webpack build.
  29. *
  30. * Use an instance of `InjectManifest` in the
  31. * [`plugins` array](https://webpack.js.org/concepts/plugins/#usage) of a
  32. * webpack config.
  33. *
  34. * @module workbox-webpack-plugin
  35. */
  36. class InjectManifest {
  37. /**
  38. * Creates an instance of InjectManifest.
  39. *
  40. * @param {Object} [config] See the
  41. * [configuration guide](https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin#configuration)
  42. * for all supported options and defaults.
  43. */
  44. constructor(config = {}) {
  45. assert(typeof config.swSrc === 'string', `swSrc must be set to the path ` + `to an existing service worker file.`);
  46. this.config = Object.assign(getDefaultConfig(), {
  47. // Default to using the same filename as the swSrc file, since that's
  48. // provided here. (In GenerateSW, that's not available.)
  49. swDest: path.basename(config.swSrc)
  50. }, config);
  51. }
  52. /**
  53. * @param {Object} compilation The webpack compilation.
  54. * @param {Function} readFile The function to use when reading files,
  55. * derived from compiler.inputFileSystem.
  56. * @private
  57. */
  58. handleEmit(compilation, readFile) {
  59. var _this = this;
  60. return (0, _asyncToGenerator2.default)(function* () {
  61. const configWarning = warnAboutConfig(_this.config);
  62. if (configWarning) {
  63. compilation.warnings.push(configWarning);
  64. }
  65. const workboxSWImports = yield getWorkboxSWImports(compilation, _this.config); // this.config.modulePathPrefix may or may not have been set by
  66. // getWorkboxSWImports(), depending on the other config options. If it was
  67. // set, we need to pull it out and make use of it later, as it can't be
  68. // used by the underlying workbox-build getManifest() method.
  69. const modulePathPrefix = _this.config.modulePathPrefix;
  70. delete _this.config.modulePathPrefix;
  71. let entries = getManifestEntriesFromCompilation(compilation, _this.config);
  72. const importScriptsArray = [].concat(_this.config.importScripts);
  73. const sanitizedConfig = sanitizeConfig.forGetManifest(_this.config); // If there are any "extra" config options remaining after we remove the
  74. // ones that are used natively by the plugin, then assume that they should
  75. // be passed on to workbox-build.getManifest() to generate extra entries.
  76. if (Object.keys(sanitizedConfig).length > 0) {
  77. // If globPatterns isn't explicitly set, then default to [], instead of
  78. // the workbox-build.getManifest() default.
  79. sanitizedConfig.globPatterns = sanitizedConfig.globPatterns || [];
  80. const _ref = yield getManifest(sanitizedConfig),
  81. manifestEntries = _ref.manifestEntries,
  82. warnings = _ref.warnings;
  83. compilation.warnings = compilation.warnings.concat(warnings || []);
  84. entries = entries.concat(manifestEntries);
  85. }
  86. const manifestString = stringifyManifest(entries);
  87. const manifestAsset = convertStringToAsset(manifestString);
  88. const manifestHash = getAssetHash(manifestAsset);
  89. const manifestFilename = formatManifestFilename(_this.config.precacheManifestFilename, manifestHash);
  90. const pathToManifestFile = relativeToOutputPath(compilation, path.join(_this.config.importsDirectory, manifestFilename));
  91. compilation.assets[pathToManifestFile] = manifestAsset;
  92. importScriptsArray.push((compilation.options.output.publicPath || '') + pathToManifestFile.split(path.sep).join('/')); // workboxSWImports might be null if importWorkboxFrom is 'disabled'.
  93. if (workboxSWImports) {
  94. importScriptsArray.push(...workboxSWImports);
  95. }
  96. let originalSWString;
  97. /**
  98. * Check if the mentioned file name is in the webpack assets itself
  99. * or fallback to filesystem.
  100. */
  101. if (compilation.assets[_this.config.swSrc]) {
  102. originalSWString = compilation.assets[_this.config.swSrc].source();
  103. } else {
  104. originalSWString = yield readFileWrapper(readFile, _this.config.swSrc);
  105. } // compilation.fileDependencies needs absolute paths.
  106. const absoluteSwSrc = path.resolve(_this.config.swSrc);
  107. if (Array.isArray(compilation.fileDependencies)) {
  108. // webpack v3
  109. if (compilation.fileDependencies.indexOf(absoluteSwSrc) === -1) {
  110. compilation.fileDependencies.push(absoluteSwSrc);
  111. }
  112. } else if ('add' in compilation.fileDependencies) {
  113. // webpack v4; no need to check for membership first, since it's a Set.
  114. compilation.fileDependencies.add(absoluteSwSrc);
  115. }
  116. const importScriptsString = importScriptsArray.map(JSON.stringify).join(', ');
  117. const setConfigString = modulePathPrefix ? `workbox.setConfig({modulePathPrefix: ` + `${JSON.stringify(modulePathPrefix)}});` : '';
  118. const postInjectionSWString = `importScripts(${importScriptsString});
  119. ${setConfigString}
  120. ${originalSWString}
  121. `;
  122. const relSwDest = relativeToOutputPath(compilation, _this.config.swDest);
  123. compilation.assets[relSwDest] = convertStringToAsset(postInjectionSWString);
  124. })();
  125. }
  126. /**
  127. * @param {Object} [compiler] default compiler object passed from webpack
  128. *
  129. * @private
  130. */
  131. apply(compiler) {
  132. const readFile = compiler.inputFileSystem.readFile.bind(compiler.inputFileSystem);
  133. if ('hooks' in compiler) {
  134. // We're in webpack 4+.
  135. compiler.hooks.emit.tapPromise(this.constructor.name, compilation => this.handleEmit(compilation, readFile));
  136. } else {
  137. // We're in webpack 2 or 3.
  138. compiler.plugin('emit', (compilation, callback) => {
  139. this.handleEmit(compilation, readFile).then(callback).catch(callback);
  140. });
  141. }
  142. }
  143. }
  144. module.exports = InjectManifest;