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