ModuleTemplate.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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("webpack-sources").Source} Source */
  9. /** @typedef {import("./Chunk")} Chunk */
  10. /** @typedef {import("./ChunkGraph")} ChunkGraph */
  11. /** @typedef {import("./Compilation")} Compilation */
  12. /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
  13. /** @typedef {import("./Module")} Module */
  14. /** @typedef {import("./ModuleGraph")} ModuleGraph */
  15. /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
  16. /** @typedef {import("./util/Hash")} Hash */
  17. const getJavascriptModulesPlugin = memoize(() =>
  18. require("./javascript/JavascriptModulesPlugin")
  19. );
  20. // TODO webpack 6: remove this class
  21. class ModuleTemplate {
  22. /**
  23. * @param {RuntimeTemplate} runtimeTemplate the runtime template
  24. * @param {Compilation} compilation the compilation
  25. */
  26. constructor(runtimeTemplate, compilation) {
  27. this._runtimeTemplate = runtimeTemplate;
  28. this.type = "javascript";
  29. this.hooks = Object.freeze({
  30. content: {
  31. tap: util.deprecate(
  32. (options, fn) => {
  33. getJavascriptModulesPlugin()
  34. .getCompilationHooks(compilation)
  35. .renderModuleContent.tap(
  36. options,
  37. (source, module, renderContext) =>
  38. fn(
  39. source,
  40. module,
  41. renderContext,
  42. renderContext.dependencyTemplates
  43. )
  44. );
  45. },
  46. "ModuleTemplate.hooks.content is deprecated (use JavascriptModulesPlugin.getCompilationHooks().renderModuleContent instead)",
  47. "DEP_MODULE_TEMPLATE_CONTENT"
  48. )
  49. },
  50. module: {
  51. tap: util.deprecate(
  52. (options, fn) => {
  53. getJavascriptModulesPlugin()
  54. .getCompilationHooks(compilation)
  55. .renderModuleContent.tap(
  56. options,
  57. (source, module, renderContext) =>
  58. fn(
  59. source,
  60. module,
  61. renderContext,
  62. renderContext.dependencyTemplates
  63. )
  64. );
  65. },
  66. "ModuleTemplate.hooks.module is deprecated (use JavascriptModulesPlugin.getCompilationHooks().renderModuleContent instead)",
  67. "DEP_MODULE_TEMPLATE_MODULE"
  68. )
  69. },
  70. render: {
  71. tap: util.deprecate(
  72. (options, fn) => {
  73. getJavascriptModulesPlugin()
  74. .getCompilationHooks(compilation)
  75. .renderModuleContainer.tap(
  76. options,
  77. (source, module, renderContext) =>
  78. fn(
  79. source,
  80. module,
  81. renderContext,
  82. renderContext.dependencyTemplates
  83. )
  84. );
  85. },
  86. "ModuleTemplate.hooks.render is deprecated (use JavascriptModulesPlugin.getCompilationHooks().renderModuleContainer instead)",
  87. "DEP_MODULE_TEMPLATE_RENDER"
  88. )
  89. },
  90. package: {
  91. tap: util.deprecate(
  92. (options, fn) => {
  93. getJavascriptModulesPlugin()
  94. .getCompilationHooks(compilation)
  95. .renderModulePackage.tap(
  96. options,
  97. (source, module, renderContext) =>
  98. fn(
  99. source,
  100. module,
  101. renderContext,
  102. renderContext.dependencyTemplates
  103. )
  104. );
  105. },
  106. "ModuleTemplate.hooks.package is deprecated (use JavascriptModulesPlugin.getCompilationHooks().renderModulePackage instead)",
  107. "DEP_MODULE_TEMPLATE_PACKAGE"
  108. )
  109. },
  110. hash: {
  111. tap: util.deprecate(
  112. (options, fn) => {
  113. compilation.hooks.fullHash.tap(options, fn);
  114. },
  115. "ModuleTemplate.hooks.hash is deprecated (use Compilation.hooks.fullHash instead)",
  116. "DEP_MODULE_TEMPLATE_HASH"
  117. )
  118. }
  119. });
  120. }
  121. }
  122. Object.defineProperty(ModuleTemplate.prototype, "runtimeTemplate", {
  123. get: util.deprecate(
  124. /**
  125. * @this {ModuleTemplate}
  126. * @returns {TODO} output options
  127. */
  128. function () {
  129. return this._runtimeTemplate;
  130. },
  131. "ModuleTemplate.runtimeTemplate is deprecated (use Compilation.runtimeTemplate instead)",
  132. "DEP_WEBPACK_CHUNK_TEMPLATE_OUTPUT_OPTIONS"
  133. )
  134. });
  135. module.exports = ModuleTemplate;