inject-manifest.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 fse = require('fs-extra');
  12. const path = require('path');
  13. const checkForDeprecatedOptions = require('../lib/check-for-deprecated-options');
  14. const defaults = require('./options/defaults');
  15. const errors = require('../lib/errors');
  16. const getFileManifestEntries = require('../lib/get-file-manifest-entries');
  17. const injectManifestSchema = require('./options/inject-manifest-schema');
  18. const validate = require('./options/validate');
  19. /**
  20. * This method creates a list of URLs to precache, referred to as a "precache
  21. * manifest", based on the options you provide.
  22. *
  23. * The manifest is injected into the `swSrc` file, and the regular expression
  24. * `injectionPointRegexp` determines where in the file the manifest should go.
  25. *
  26. * The final service worker file, with the manifest injected, is written to
  27. * disk at `swDest`.
  28. *
  29. * @param {Object} config Please refer to the
  30. * [configuration guide](https://developers.google.com/web/tools/workbox/modules/workbox-build#full_injectmanifest_config).
  31. * @return {Promise<{count: number, size: number, warnings: Array<string>}>}
  32. * A promise that resolves once the service worker file has been written to
  33. * `swDest`. The `size` property contains the aggregate size of all the
  34. * precached entries, in bytes, and the `count` property contains the total
  35. * number of precached entries. Any non-fatal warning messages will be returned
  36. * via `warnings`.
  37. *
  38. * @memberof module:workbox-build
  39. */
  40. function injectManifest(_x) {
  41. return _injectManifest.apply(this, arguments);
  42. }
  43. function _injectManifest() {
  44. _injectManifest = (0, _asyncToGenerator2.default)(function* (config) {
  45. // This check needs to be done before validation, since the deprecated options
  46. // will be renamed.
  47. const deprecationWarnings = checkForDeprecatedOptions(config);
  48. const options = validate(config, injectManifestSchema);
  49. if (path.normalize(config.swSrc) === path.normalize(config.swDest)) {
  50. throw new Error(errors['same-src-and-dest']);
  51. }
  52. const globalRegexp = new RegExp(options.injectionPointRegexp, 'g');
  53. const _ref = yield getFileManifestEntries(options),
  54. count = _ref.count,
  55. size = _ref.size,
  56. manifestEntries = _ref.manifestEntries,
  57. warnings = _ref.warnings;
  58. let swFileContents;
  59. try {
  60. swFileContents = yield fse.readFile(config.swSrc, 'utf8');
  61. } catch (error) {
  62. throw new Error(`${errors['invalid-sw-src']} ${error.message}`);
  63. }
  64. const injectionResults = swFileContents.match(globalRegexp);
  65. assert(injectionResults, errors['injection-point-not-found'] + ( // Customize the error message when this happens:
  66. // - If the default RegExp is used, then include the expected string that
  67. // matches as a hint to the developer.
  68. // - If a custom RegExp is used, then just include the raw RegExp.
  69. options.injectionPointRegexp === defaults.injectionPointRegexp ? 'workbox.precaching.precacheAndRoute([])' : options.injectionPointRegexp));
  70. assert(injectionResults.length === 1, errors['multiple-injection-points'] + ` ${options.injectionPointRegexp}`);
  71. const entriesString = JSON.stringify(manifestEntries, null, 2);
  72. swFileContents = swFileContents.replace(globalRegexp, `$1${entriesString}$2`);
  73. try {
  74. yield fse.mkdirp(path.dirname(options.swDest));
  75. } catch (error) {
  76. throw new Error(errors['unable-to-make-injection-directory'] + ` '${error.message}'`);
  77. }
  78. yield fse.writeFile(config.swDest, swFileContents); // Add in any deprecation warnings.
  79. warnings.push(...deprecationWarnings);
  80. return {
  81. count,
  82. size,
  83. warnings
  84. };
  85. });
  86. return _injectManifest.apply(this, arguments);
  87. }
  88. module.exports = injectManifest;