ChunkTemplate.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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("../declarations/WebpackOptions").Output} 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. * @template T
  21. * @typedef {import("tapable").IfSet<T>} IfSet
  22. */
  23. const getJavascriptModulesPlugin = memoize(() =>
  24. require("./javascript/JavascriptModulesPlugin")
  25. );
  26. // TODO webpack 6 remove this class
  27. class ChunkTemplate {
  28. /**
  29. * @param {OutputOptions} outputOptions output options
  30. * @param {Compilation} compilation the compilation
  31. */
  32. constructor(outputOptions, compilation) {
  33. this._outputOptions = outputOptions || {};
  34. this.hooks = Object.freeze({
  35. renderManifest: {
  36. tap: util.deprecate(
  37. /**
  38. * @template AdditionalOptions
  39. * @param {string | Tap & IfSet<AdditionalOptions>} options options
  40. * @param {function(RenderManifestEntry[], RenderManifestOptions): RenderManifestEntry[]} fn function
  41. */
  42. (options, fn) => {
  43. compilation.hooks.renderManifest.tap(
  44. options,
  45. (entries, options) => {
  46. if (options.chunk.hasRuntime()) return entries;
  47. return fn(entries, options);
  48. }
  49. );
  50. },
  51. "ChunkTemplate.hooks.renderManifest is deprecated (use Compilation.hooks.renderManifest instead)",
  52. "DEP_WEBPACK_CHUNK_TEMPLATE_RENDER_MANIFEST"
  53. )
  54. },
  55. modules: {
  56. tap: util.deprecate(
  57. /**
  58. * @template AdditionalOptions
  59. * @param {string | Tap & IfSet<AdditionalOptions>} options options
  60. * @param {function(Source, ModuleTemplate, RenderContext): Source} fn function
  61. */
  62. (options, fn) => {
  63. getJavascriptModulesPlugin()
  64. .getCompilationHooks(compilation)
  65. .renderChunk.tap(options, (source, renderContext) =>
  66. fn(
  67. source,
  68. compilation.moduleTemplates.javascript,
  69. renderContext
  70. )
  71. );
  72. },
  73. "ChunkTemplate.hooks.modules is deprecated (use JavascriptModulesPlugin.getCompilationHooks().renderChunk instead)",
  74. "DEP_WEBPACK_CHUNK_TEMPLATE_MODULES"
  75. )
  76. },
  77. render: {
  78. tap: util.deprecate(
  79. /**
  80. * @template AdditionalOptions
  81. * @param {string | Tap & IfSet<AdditionalOptions>} options options
  82. * @param {function(Source, ModuleTemplate, RenderContext): Source} fn function
  83. */
  84. (options, fn) => {
  85. getJavascriptModulesPlugin()
  86. .getCompilationHooks(compilation)
  87. .renderChunk.tap(options, (source, renderContext) =>
  88. fn(
  89. source,
  90. compilation.moduleTemplates.javascript,
  91. renderContext
  92. )
  93. );
  94. },
  95. "ChunkTemplate.hooks.render is deprecated (use JavascriptModulesPlugin.getCompilationHooks().renderChunk instead)",
  96. "DEP_WEBPACK_CHUNK_TEMPLATE_RENDER"
  97. )
  98. },
  99. renderWithEntry: {
  100. tap: util.deprecate(
  101. /**
  102. * @template AdditionalOptions
  103. * @param {string | Tap & IfSet<AdditionalOptions>} options options
  104. * @param {function(Source, Chunk): Source} fn function
  105. */
  106. (options, fn) => {
  107. getJavascriptModulesPlugin()
  108. .getCompilationHooks(compilation)
  109. .render.tap(options, (source, renderContext) => {
  110. if (
  111. renderContext.chunkGraph.getNumberOfEntryModules(
  112. renderContext.chunk
  113. ) === 0 ||
  114. renderContext.chunk.hasRuntime()
  115. ) {
  116. return source;
  117. }
  118. return fn(source, renderContext.chunk);
  119. });
  120. },
  121. "ChunkTemplate.hooks.renderWithEntry is deprecated (use JavascriptModulesPlugin.getCompilationHooks().render instead)",
  122. "DEP_WEBPACK_CHUNK_TEMPLATE_RENDER_WITH_ENTRY"
  123. )
  124. },
  125. hash: {
  126. tap: util.deprecate(
  127. /**
  128. * @template AdditionalOptions
  129. * @param {string | Tap & IfSet<AdditionalOptions>} options options
  130. * @param {function(Hash): void} fn function
  131. */
  132. (options, fn) => {
  133. compilation.hooks.fullHash.tap(options, fn);
  134. },
  135. "ChunkTemplate.hooks.hash is deprecated (use Compilation.hooks.fullHash instead)",
  136. "DEP_WEBPACK_CHUNK_TEMPLATE_HASH"
  137. )
  138. },
  139. hashForChunk: {
  140. tap: util.deprecate(
  141. /**
  142. * @template AdditionalOptions
  143. * @param {string | Tap & IfSet<AdditionalOptions>} options options
  144. * @param {function(Hash, Chunk, ChunkHashContext): void} fn function
  145. */
  146. (options, fn) => {
  147. getJavascriptModulesPlugin()
  148. .getCompilationHooks(compilation)
  149. .chunkHash.tap(options, (chunk, hash, context) => {
  150. if (chunk.hasRuntime()) return;
  151. fn(hash, chunk, context);
  152. });
  153. },
  154. "ChunkTemplate.hooks.hashForChunk is deprecated (use JavascriptModulesPlugin.getCompilationHooks().chunkHash instead)",
  155. "DEP_WEBPACK_CHUNK_TEMPLATE_HASH_FOR_CHUNK"
  156. )
  157. }
  158. });
  159. }
  160. }
  161. Object.defineProperty(ChunkTemplate.prototype, "outputOptions", {
  162. get: util.deprecate(
  163. /**
  164. * @this {ChunkTemplate}
  165. * @returns {OutputOptions} output options
  166. */
  167. function () {
  168. return this._outputOptions;
  169. },
  170. "ChunkTemplate.outputOptions is deprecated (use Compilation.outputOptions instead)",
  171. "DEP_WEBPACK_CHUNK_TEMPLATE_OUTPUT_OPTIONS"
  172. )
  173. });
  174. module.exports = ChunkTemplate;