UNPKG

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