ChunkTemplate.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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("../declarations/WebpackOptions").Output} OutputOptions */
  9. /** @typedef {import("./Compilation")} Compilation */
  10. const getJavascriptModulesPlugin = memoize(() =>
  11. require("./javascript/JavascriptModulesPlugin")
  12. );
  13. // TODO webpack 6 remove this class
  14. class ChunkTemplate {
  15. /**
  16. * @param {OutputOptions} outputOptions output options
  17. * @param {Compilation} compilation the compilation
  18. */
  19. constructor(outputOptions, compilation) {
  20. this._outputOptions = outputOptions || {};
  21. this.hooks = Object.freeze({
  22. renderManifest: {
  23. tap: util.deprecate(
  24. (options, fn) => {
  25. compilation.hooks.renderManifest.tap(
  26. options,
  27. (entries, options) => {
  28. if (options.chunk.hasRuntime()) return entries;
  29. return fn(entries, options);
  30. }
  31. );
  32. },
  33. "ChunkTemplate.hooks.renderManifest is deprecated (use Compilation.hooks.renderManifest instead)",
  34. "DEP_WEBPACK_CHUNK_TEMPLATE_RENDER_MANIFEST"
  35. )
  36. },
  37. modules: {
  38. tap: util.deprecate(
  39. (options, fn) => {
  40. getJavascriptModulesPlugin()
  41. .getCompilationHooks(compilation)
  42. .renderChunk.tap(options, (source, renderContext) =>
  43. fn(
  44. source,
  45. compilation.moduleTemplates.javascript,
  46. renderContext
  47. )
  48. );
  49. },
  50. "ChunkTemplate.hooks.modules is deprecated (use JavascriptModulesPlugin.getCompilationHooks().renderChunk instead)",
  51. "DEP_WEBPACK_CHUNK_TEMPLATE_MODULES"
  52. )
  53. },
  54. render: {
  55. tap: util.deprecate(
  56. (options, fn) => {
  57. getJavascriptModulesPlugin()
  58. .getCompilationHooks(compilation)
  59. .renderChunk.tap(options, (source, renderContext) =>
  60. fn(
  61. source,
  62. compilation.moduleTemplates.javascript,
  63. renderContext
  64. )
  65. );
  66. },
  67. "ChunkTemplate.hooks.render is deprecated (use JavascriptModulesPlugin.getCompilationHooks().renderChunk instead)",
  68. "DEP_WEBPACK_CHUNK_TEMPLATE_RENDER"
  69. )
  70. },
  71. renderWithEntry: {
  72. tap: util.deprecate(
  73. (options, fn) => {
  74. getJavascriptModulesPlugin()
  75. .getCompilationHooks(compilation)
  76. .render.tap(options, (source, renderContext) => {
  77. if (
  78. renderContext.chunkGraph.getNumberOfEntryModules(
  79. renderContext.chunk
  80. ) === 0 ||
  81. renderContext.chunk.hasRuntime()
  82. ) {
  83. return source;
  84. }
  85. return fn(source, renderContext.chunk);
  86. });
  87. },
  88. "ChunkTemplate.hooks.renderWithEntry is deprecated (use JavascriptModulesPlugin.getCompilationHooks().render instead)",
  89. "DEP_WEBPACK_CHUNK_TEMPLATE_RENDER_WITH_ENTRY"
  90. )
  91. },
  92. hash: {
  93. tap: util.deprecate(
  94. (options, fn) => {
  95. compilation.hooks.fullHash.tap(options, fn);
  96. },
  97. "ChunkTemplate.hooks.hash is deprecated (use Compilation.hooks.fullHash instead)",
  98. "DEP_WEBPACK_CHUNK_TEMPLATE_HASH"
  99. )
  100. },
  101. hashForChunk: {
  102. tap: util.deprecate(
  103. (options, fn) => {
  104. getJavascriptModulesPlugin()
  105. .getCompilationHooks(compilation)
  106. .chunkHash.tap(options, (chunk, hash, context) => {
  107. if (chunk.hasRuntime()) return;
  108. fn(hash, chunk, context);
  109. });
  110. },
  111. "ChunkTemplate.hooks.hashForChunk is deprecated (use JavascriptModulesPlugin.getCompilationHooks().chunkHash instead)",
  112. "DEP_WEBPACK_CHUNK_TEMPLATE_HASH_FOR_CHUNK"
  113. )
  114. }
  115. });
  116. }
  117. }
  118. Object.defineProperty(ChunkTemplate.prototype, "outputOptions", {
  119. get: util.deprecate(
  120. /**
  121. * @this {ChunkTemplate}
  122. * @returns {OutputOptions} output options
  123. */
  124. function () {
  125. return this._outputOptions;
  126. },
  127. "ChunkTemplate.outputOptions is deprecated (use Compilation.outputOptions instead)",
  128. "DEP_WEBPACK_CHUNK_TEMPLATE_OUTPUT_OPTIONS"
  129. )
  130. });
  131. module.exports = ChunkTemplate;