ChunkTemplate.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { Tapable, SyncWaterfallHook, SyncHook } = require("tapable");
  7. /** @typedef {import("./ModuleTemplate")} ModuleTemplate */
  8. /** @typedef {import("./Chunk")} Chunk */
  9. /** @typedef {import("./Module")} Module} */
  10. /** @typedef {import("./util/createHash").Hash} Hash} */
  11. /**
  12. * @typedef {Object} RenderManifestOptions
  13. * @property {Chunk} chunk the chunk used to render
  14. * @property {string} hash
  15. * @property {string} fullHash
  16. * @property {TODO} outputOptions
  17. * @property {{javascript: ModuleTemplate, webassembly: ModuleTemplate}} moduleTemplates
  18. * @property {Map<TODO, TODO>} dependencyTemplates
  19. */
  20. module.exports = class ChunkTemplate extends Tapable {
  21. constructor(outputOptions) {
  22. super();
  23. this.outputOptions = outputOptions || {};
  24. this.hooks = {
  25. /** @type {SyncWaterfallHook<TODO[], RenderManifestOptions>} */
  26. renderManifest: new SyncWaterfallHook(["result", "options"]),
  27. modules: new SyncWaterfallHook([
  28. "source",
  29. "chunk",
  30. "moduleTemplate",
  31. "dependencyTemplates"
  32. ]),
  33. render: new SyncWaterfallHook([
  34. "source",
  35. "chunk",
  36. "moduleTemplate",
  37. "dependencyTemplates"
  38. ]),
  39. renderWithEntry: new SyncWaterfallHook(["source", "chunk"]),
  40. hash: new SyncHook(["hash"]),
  41. hashForChunk: new SyncHook(["hash", "chunk"])
  42. };
  43. }
  44. /**
  45. *
  46. * @param {RenderManifestOptions} options render manifest options
  47. * @returns {TODO[]} returns render manifest
  48. */
  49. getRenderManifest(options) {
  50. const result = [];
  51. this.hooks.renderManifest.call(result, options);
  52. return result;
  53. }
  54. /**
  55. * Updates hash with information from this template
  56. * @param {Hash} hash the hash to update
  57. * @returns {void}
  58. */
  59. updateHash(hash) {
  60. hash.update("ChunkTemplate");
  61. hash.update("2");
  62. this.hooks.hash.call(hash);
  63. }
  64. /**
  65. * Updates hash with chunk-specific information from this template
  66. * @param {Hash} hash the hash to update
  67. * @param {Chunk} chunk the chunk
  68. * @returns {void}
  69. */
  70. updateHashForChunk(hash, chunk) {
  71. this.updateHash(hash);
  72. this.hooks.hashForChunk.call(hash, chunk);
  73. }
  74. };