get-manifest.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. "use strict";
  2. /*
  3. Copyright 2018 Google LLC
  4. Use of this source code is governed by an MIT-style
  5. license that can be found in the LICENSE file or at
  6. https://opensource.org/licenses/MIT.
  7. */
  8. const getFileManifestEntries = require('./lib/get-file-manifest-entries');
  9. const getManifestSchema = require('./options/schema/get-manifest');
  10. const validate = require('./lib/validate-options'); // eslint-disable-next-line jsdoc/newline-after-description
  11. /**
  12. * This method returns a list of URLs to precache, referred to as a "precache
  13. * manifest", along with details about the number of entries and their size,
  14. * based on the options you provide.
  15. *
  16. * @param {Object} config The configuration to use.
  17. *
  18. * @param {string} config.globDirectory The local directory you wish to match
  19. * `globPatterns` against. The path is relative to the current directory.
  20. *
  21. * @param {Array<module:workbox-build.ManifestEntry>} [config.additionalManifestEntries]
  22. * A list of entries to be precached, in addition to any entries that are
  23. * generated as part of the build configuration.
  24. *
  25. * @param {RegExp} [config.dontCacheBustURLsMatching] Assets that match this will be
  26. * assumed to be uniquely versioned via their URL, and exempted from the normal
  27. * HTTP cache-busting that's done when populating the precache. While not
  28. * required, it's recommended that if your existing build process already
  29. * inserts a `[hash]` value into each filename, you provide a RegExp that will
  30. * detect that, as it will reduce the bandwidth consumed when precaching.
  31. *
  32. * @param {boolean} [config.globFollow=true] Determines whether or not symlinks
  33. * are followed when generating the precache manifest. For more information, see
  34. * the definition of `follow` in the `glob`
  35. * [documentation](https://github.com/isaacs/node-glob#options).
  36. *
  37. * @param {Array<string>} [config.globIgnores=['node_modules/**']]
  38. * A set of patterns matching files to always exclude when generating the
  39. * precache manifest. For more information, see the definition of `ignore` in the `glob`
  40. * [documentation](https://github.com/isaacs/node-glob#options).
  41. *
  42. * @param {Array<string>} [config.globPatterns=['**.{js,css,html}']]
  43. * Files matching any of these patterns will be included in the precache
  44. * manifest. For more information, see the
  45. * [`glob` primer](https://github.com/isaacs/node-glob#glob-primer).
  46. *
  47. * @param {boolean} [config.globStrict=true] If true, an error reading a directory when
  48. * generating a precache manifest will cause the build to fail. If false, the
  49. * problematic directory will be skipped. For more information, see the
  50. * definition of `strict` in the `glob`
  51. * [documentation](https://github.com/isaacs/node-glob#options).
  52. *
  53. * @param {Array<module:workbox-build.ManifestTransform>} [config.manifestTransforms] One or more
  54. * functions which will be applied sequentially against the generated manifest.
  55. * If `modifyURLPrefix` or `dontCacheBustURLsMatching` are also specified, their
  56. * corresponding transformations will be applied first.
  57. *
  58. * @param {number} [config.maximumFileSizeToCacheInBytes=2097152] This value can be
  59. * used to determine the maximum size of files that will be precached. This
  60. * prevents you from inadvertently precaching very large files that might have
  61. * accidentally matched one of your patterns.
  62. *
  63. * @param {string} [config.mode='production'] If set to 'production', then an
  64. * optimized service worker bundle that excludes debugging info will be
  65. * produced. If not explicitly configured here, the `process.env.NODE_ENV` value
  66. * will be used, and failing that, it will fall back to `'production'`.
  67. *
  68. * @param {object<string, string>} [config.modifyURLPrefix] A mapping of prefixes
  69. * that, if present in an entry in the precache manifest, will be replaced with
  70. * the corresponding value. This can be used to, for example, remove or add a
  71. * path prefix from a manifest entry if your web hosting setup doesn't match
  72. * your local filesystem setup. As an alternative with more flexibility, you can
  73. * use the `manifestTransforms` option and provide a function that modifies the
  74. * entries in the manifest using whatever logic you provide.
  75. *
  76. * @param {Object} [config.templatedURLs] If a URL is rendered based on some
  77. * server-side logic, its contents may depend on multiple files or on some other
  78. * unique string value. The keys in this object are server-rendered URLs. If the
  79. * values are an array of strings, they will be interpreted as `glob` patterns,
  80. * and the contents of any files matching the patterns will be used to uniquely
  81. * version the URL. If used with a single string, it will be interpreted as
  82. * unique versioning information that you've generated for a given URL.
  83. *
  84. * @return {Promise<{count: number, manifestEntries: Array<module:workbox-build.ManifestEntry>, size: number, warnings: Array<string>}>}
  85. * A promise that resolves once the precache manifest (available in the
  86. * `manifestEntries` property) has been determined. The `size` property
  87. * contains the aggregate size of all the precached entries, in bytes, and the
  88. * `count` property contains the total number of precached entries. Any
  89. * non-fatal warning messages will be returned via `warnings`.
  90. *
  91. * @memberof module:workbox-build
  92. */
  93. async function getManifest(config) {
  94. const options = validate(config, getManifestSchema);
  95. const {
  96. manifestEntries,
  97. count,
  98. size,
  99. warnings
  100. } = await getFileManifestEntries(options);
  101. return {
  102. manifestEntries,
  103. count,
  104. size,
  105. warnings
  106. };
  107. }
  108. module.exports = getManifest;