ChunkTemplate.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const util = require("util");
  7. const memoize = require("./util/memoize");
  8. /** @typedef {import("tapable").Tap} Tap */
  9. /** @typedef {import("./config/defaults").OutputNormalizedWithDefaults} OutputOptions */
  10. /** @typedef {import("./Chunk")} Chunk */
  11. /** @typedef {import("./Compilation")} Compilation */
  12. /** @typedef {import("./Compilation").ChunkHashContext} ChunkHashContext */
  13. /** @typedef {import("./Compilation").Hash} Hash */
  14. /** @typedef {import("./Compilation").RenderManifestEntry} RenderManifestEntry */
  15. /** @typedef {import("./Compilation").RenderManifestOptions} RenderManifestOptions */
  16. /** @typedef {import("./Compilation").Source} Source */
  17. /** @typedef {import("./ModuleTemplate")} ModuleTemplate */
  18. /** @typedef {import("./javascript/JavascriptModulesPlugin").RenderContext} RenderContext */
  19. /**
  20. * Defines the if set type used by this module.
  21. * @template T
  22. * @typedef {import("tapable").IfSet<T>} IfSet
  23. */
  24. const getJavascriptModulesPlugin = memoize(() =>
  25. require("./javascript/JavascriptModulesPlugin")
  26. );
  27. // TODO webpack 6 remove this class
  28. class ChunkTemplate {
  29. /**
  30. * Creates an instance of ChunkTemplate.
  31. * @param {OutputOptions} outputOptions output options
  32. * @param {Compilation} compilation the compilation
  33. */
  34. constructor(outputOptions, compilation) {
  35. this._outputOptions = outputOptions || {};
  36. this.hooks = Object.freeze({
  37. renderManifest: {
  38. tap: util.deprecate(
  39. /**
  40. * Handles the callback logic for this hook.
  41. * @template AdditionalOptions
  42. * @param {string | Tap & IfSet<AdditionalOptions>} options options
  43. * @param {(renderManifestEntries: RenderManifestEntry[], renderManifestOptions: RenderManifestOptions) => RenderManifestEntry[]} fn function
  44. */
  45. (options, fn) => {
  46. compilation.hooks.renderManifest.tap(
  47. options,
  48. (entries, options) => {
  49. if (options.chunk.hasRuntime()) return entries;
  50. return fn(entries, options);
  51. }
  52. );
  53. },
  54. "ChunkTemplate.hooks.renderManifest is deprecated (use Compilation.hooks.renderManifest instead)",
  55. "DEP_WEBPACK_CHUNK_TEMPLATE_RENDER_MANIFEST"
  56. )
  57. },
  58. modules: {
  59. tap: util.deprecate(
  60. /**
  61. * Handles the callback logic for this hook.
  62. * @template AdditionalOptions
  63. * @param {string | Tap & IfSet<AdditionalOptions>} options options
  64. * @param {(source: Source, moduleTemplate: ModuleTemplate, renderContext: RenderContext) => Source} fn function
  65. */
  66. (options, fn) => {
  67. getJavascriptModulesPlugin()
  68. .getCompilationHooks(compilation)
  69. .renderChunk.tap(options, (source, renderContext) =>
  70. fn(
  71. source,
  72. compilation.moduleTemplates.javascript,
  73. renderContext
  74. )
  75. );
  76. },
  77. "ChunkTemplate.hooks.modules is deprecated (use JavascriptModulesPlugin.getCompilationHooks().renderChunk instead)",
  78. "DEP_WEBPACK_CHUNK_TEMPLATE_MODULES"
  79. )
  80. },
  81. render: {
  82. tap: util.deprecate(
  83. /**
  84. * Handles the callback logic for this hook.
  85. * @template AdditionalOptions
  86. * @param {string | Tap & IfSet<AdditionalOptions>} options options
  87. * @param {(source: Source, moduleTemplate: ModuleTemplate, renderContext: RenderContext) => Source} fn function
  88. */
  89. (options, fn) => {
  90. getJavascriptModulesPlugin()
  91. .getCompilationHooks(compilation)
  92. .renderChunk.tap(options, (source, renderContext) =>
  93. fn(
  94. source,
  95. compilation.moduleTemplates.javascript,
  96. renderContext
  97. )
  98. );
  99. },
  100. "ChunkTemplate.hooks.render is deprecated (use JavascriptModulesPlugin.getCompilationHooks().renderChunk instead)",
  101. "DEP_WEBPACK_CHUNK_TEMPLATE_RENDER"
  102. )
  103. },
  104. renderWithEntry: {
  105. tap: util.deprecate(
  106. /**
  107. * Handles the callback logic for this hook.
  108. * @template AdditionalOptions
  109. * @param {string | Tap & IfSet<AdditionalOptions>} options options
  110. * @param {(source: Source, chunk: Chunk) => Source} fn function
  111. */
  112. (options, fn) => {
  113. getJavascriptModulesPlugin()
  114. .getCompilationHooks(compilation)
  115. .render.tap(options, (source, renderContext) => {
  116. if (
  117. renderContext.chunkGraph.getNumberOfEntryModules(
  118. renderContext.chunk
  119. ) === 0 ||
  120. renderContext.chunk.hasRuntime()
  121. ) {
  122. return source;
  123. }
  124. return fn(source, renderContext.chunk);
  125. });
  126. },
  127. "ChunkTemplate.hooks.renderWithEntry is deprecated (use JavascriptModulesPlugin.getCompilationHooks().render instead)",
  128. "DEP_WEBPACK_CHUNK_TEMPLATE_RENDER_WITH_ENTRY"
  129. )
  130. },
  131. hash: {
  132. tap: util.deprecate(
  133. /**
  134. * Handles the callback logic for this hook.
  135. * @template AdditionalOptions
  136. * @param {string | Tap & IfSet<AdditionalOptions>} options options
  137. * @param {(hash: Hash) => void} fn function
  138. */
  139. (options, fn) => {
  140. compilation.hooks.fullHash.tap(options, fn);
  141. },
  142. "ChunkTemplate.hooks.hash is deprecated (use Compilation.hooks.fullHash instead)",
  143. "DEP_WEBPACK_CHUNK_TEMPLATE_HASH"
  144. )
  145. },
  146. hashForChunk: {
  147. tap: util.deprecate(
  148. /**
  149. * Handles the callback logic for this hook.
  150. * @template AdditionalOptions
  151. * @param {string | Tap & IfSet<AdditionalOptions>} options options
  152. * @param {(hash: Hash, chunk: Chunk, chunkHashContext: ChunkHashContext) => void} fn function
  153. */
  154. (options, fn) => {
  155. getJavascriptModulesPlugin()
  156. .getCompilationHooks(compilation)
  157. .chunkHash.tap(options, (chunk, hash, context) => {
  158. if (chunk.hasRuntime()) return;
  159. fn(hash, chunk, context);
  160. });
  161. },
  162. "ChunkTemplate.hooks.hashForChunk is deprecated (use JavascriptModulesPlugin.getCompilationHooks().chunkHash instead)",
  163. "DEP_WEBPACK_CHUNK_TEMPLATE_HASH_FOR_CHUNK"
  164. )
  165. }
  166. });
  167. }
  168. }
  169. Object.defineProperty(ChunkTemplate.prototype, "outputOptions", {
  170. get: util.deprecate(
  171. /**
  172. * Returns output options.
  173. * @this {ChunkTemplate}
  174. * @returns {OutputOptions} output options
  175. */
  176. function outputOptions() {
  177. return this._outputOptions;
  178. },
  179. "ChunkTemplate.outputOptions is deprecated (use Compilation.outputOptions instead)",
  180. "DEP_WEBPACK_CHUNK_TEMPLATE_OUTPUT_OPTIONS"
  181. )
  182. });
  183. module.exports = ChunkTemplate;