You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.0 KiB

4 years ago
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { Tapable, SyncWaterfallHook, SyncHook } = require("tapable");
  7. /** @typedef {import("webpack-sources").Source} Source */
  8. /** @typedef {import("./Module")} Module */
  9. module.exports = class ModuleTemplate extends Tapable {
  10. constructor(runtimeTemplate, type) {
  11. super();
  12. this.runtimeTemplate = runtimeTemplate;
  13. this.type = type;
  14. this.hooks = {
  15. content: new SyncWaterfallHook([
  16. "source",
  17. "module",
  18. "options",
  19. "dependencyTemplates"
  20. ]),
  21. module: new SyncWaterfallHook([
  22. "source",
  23. "module",
  24. "options",
  25. "dependencyTemplates"
  26. ]),
  27. render: new SyncWaterfallHook([
  28. "source",
  29. "module",
  30. "options",
  31. "dependencyTemplates"
  32. ]),
  33. package: new SyncWaterfallHook([
  34. "source",
  35. "module",
  36. "options",
  37. "dependencyTemplates"
  38. ]),
  39. hash: new SyncHook(["hash"])
  40. };
  41. }
  42. /**
  43. * @param {Module} module the module
  44. * @param {TODO} dependencyTemplates templates for dependencies
  45. * @param {TODO} options render options
  46. * @returns {Source} the source
  47. */
  48. render(module, dependencyTemplates, options) {
  49. try {
  50. const moduleSource = module.source(
  51. dependencyTemplates,
  52. this.runtimeTemplate,
  53. this.type
  54. );
  55. const moduleSourcePostContent = this.hooks.content.call(
  56. moduleSource,
  57. module,
  58. options,
  59. dependencyTemplates
  60. );
  61. const moduleSourcePostModule = this.hooks.module.call(
  62. moduleSourcePostContent,
  63. module,
  64. options,
  65. dependencyTemplates
  66. );
  67. const moduleSourcePostRender = this.hooks.render.call(
  68. moduleSourcePostModule,
  69. module,
  70. options,
  71. dependencyTemplates
  72. );
  73. return this.hooks.package.call(
  74. moduleSourcePostRender,
  75. module,
  76. options,
  77. dependencyTemplates
  78. );
  79. } catch (e) {
  80. e.message = `${module.identifier()}\n${e.message}`;
  81. throw e;
  82. }
  83. }
  84. updateHash(hash) {
  85. hash.update("1");
  86. this.hooks.hash.call(hash);
  87. }
  88. };