1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const { SyncWaterfallHook } = require("tapable");
|
9 | const util = require("util");
|
10 | const RuntimeGlobals = require("./RuntimeGlobals");
|
11 | const memorize = require("./util/memorize");
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | const getJavascriptModulesPlugin = memorize(() =>
|
31 | require("./javascript/JavascriptModulesPlugin")
|
32 | );
|
33 | const getJsonpTemplatePlugin = memorize(() =>
|
34 | require("./web/JsonpTemplatePlugin")
|
35 | );
|
36 | const getLoadScriptRuntimeModule = memorize(() =>
|
37 | require("./runtime/LoadScriptRuntimeModule")
|
38 | );
|
39 |
|
40 |
|
41 | class MainTemplate {
|
42 | |
43 |
|
44 |
|
45 |
|
46 |
|
47 | constructor(outputOptions, compilation) {
|
48 |
|
49 | this._outputOptions = outputOptions || {};
|
50 | this.hooks = Object.freeze({
|
51 | renderManifest: {
|
52 | tap: util.deprecate(
|
53 | (options, fn) => {
|
54 | compilation.hooks.renderManifest.tap(
|
55 | options,
|
56 | (entries, options) => {
|
57 | if (!options.chunk.hasRuntime()) return entries;
|
58 | return fn(entries, options);
|
59 | }
|
60 | );
|
61 | },
|
62 | "MainTemplate.hooks.renderManifest is deprecated (use Compilation.hooks.renderManifest instead)",
|
63 | "DEP_WEBPACK_MAIN_TEMPLATE_RENDER_MANIFEST"
|
64 | )
|
65 | },
|
66 | modules: {
|
67 | tap: () => {
|
68 | throw new Error(
|
69 | "MainTemplate.hooks.modules has been removed (there is no replacement, please create an issue to request that)"
|
70 | );
|
71 | }
|
72 | },
|
73 | moduleObj: {
|
74 | tap: () => {
|
75 | throw new Error(
|
76 | "MainTemplate.hooks.moduleObj has been removed (there is no replacement, please create an issue to request that)"
|
77 | );
|
78 | }
|
79 | },
|
80 | require: {
|
81 | tap: util.deprecate(
|
82 | (options, fn) => {
|
83 | getJavascriptModulesPlugin()
|
84 | .getCompilationHooks(compilation)
|
85 | .renderRequire.tap(options, fn);
|
86 | },
|
87 | "MainTemplate.hooks.require is deprecated (use JavascriptModulesPlugin.getCompilationHooks().renderRequire instead)",
|
88 | "DEP_WEBPACK_MAIN_TEMPLATE_REQUIRE"
|
89 | )
|
90 | },
|
91 | beforeStartup: {
|
92 | tap: () => {
|
93 | throw new Error(
|
94 | "MainTemplate.hooks.beforeStartup has been removed (use RuntimeGlobals.startup instead)"
|
95 | );
|
96 | }
|
97 | },
|
98 | startup: {
|
99 | tap: () => {
|
100 | throw new Error(
|
101 | "MainTemplate.hooks.startup has been removed (use RuntimeGlobals.startup instead)"
|
102 | );
|
103 | }
|
104 | },
|
105 | afterStartup: {
|
106 | tap: () => {
|
107 | throw new Error(
|
108 | "MainTemplate.hooks.afterStartup has been removed (use RuntimeGlobals.startup instead)"
|
109 | );
|
110 | }
|
111 | },
|
112 | render: {
|
113 | tap: util.deprecate(
|
114 | (options, fn) => {
|
115 | getJavascriptModulesPlugin()
|
116 | .getCompilationHooks(compilation)
|
117 | .render.tap(options, (source, renderContext) => {
|
118 | if (
|
119 | renderContext.chunkGraph.getNumberOfEntryModules(
|
120 | renderContext.chunk
|
121 | ) === 0 ||
|
122 | !renderContext.chunk.hasRuntime()
|
123 | ) {
|
124 | return source;
|
125 | }
|
126 | return fn(
|
127 | source,
|
128 | renderContext.chunk,
|
129 | compilation.hash,
|
130 | compilation.moduleTemplates.javascript,
|
131 | compilation.dependencyTemplates
|
132 | );
|
133 | });
|
134 | },
|
135 | "MainTemplate.hooks.render is deprecated (use JavascriptModulesPlugin.getCompilationHooks().render instead)",
|
136 | "DEP_WEBPACK_MAIN_TEMPLATE_RENDER"
|
137 | )
|
138 | },
|
139 | renderWithEntry: {
|
140 | tap: util.deprecate(
|
141 | (options, fn) => {
|
142 | getJavascriptModulesPlugin()
|
143 | .getCompilationHooks(compilation)
|
144 | .render.tap(options, (source, renderContext) => {
|
145 | if (
|
146 | renderContext.chunkGraph.getNumberOfEntryModules(
|
147 | renderContext.chunk
|
148 | ) === 0 ||
|
149 | !renderContext.chunk.hasRuntime()
|
150 | ) {
|
151 | return source;
|
152 | }
|
153 | return fn(source, renderContext.chunk, compilation.hash);
|
154 | });
|
155 | },
|
156 | "MainTemplate.hooks.renderWithEntry is deprecated (use JavascriptModulesPlugin.getCompilationHooks().render instead)",
|
157 | "DEP_WEBPACK_MAIN_TEMPLATE_RENDER_WITH_ENTRY"
|
158 | )
|
159 | },
|
160 | assetPath: {
|
161 | tap: util.deprecate(
|
162 | (options, fn) => {
|
163 | compilation.hooks.assetPath.tap(options, fn);
|
164 | },
|
165 | "MainTemplate.hooks.assetPath is deprecated (use Compilation.hooks.assetPath instead)",
|
166 | "DEP_WEBPACK_MAIN_TEMPLATE_ASSET_PATH"
|
167 | ),
|
168 | call: util.deprecate(
|
169 | (filename, options) => {
|
170 | return compilation.getAssetPath(filename, options);
|
171 | },
|
172 | "MainTemplate.hooks.assetPath is deprecated (use Compilation.hooks.assetPath instead)",
|
173 | "DEP_WEBPACK_MAIN_TEMPLATE_ASSET_PATH"
|
174 | )
|
175 | },
|
176 | hash: {
|
177 | tap: util.deprecate(
|
178 | (options, fn) => {
|
179 | compilation.hooks.fullHash.tap(options, fn);
|
180 | },
|
181 | "MainTemplate.hooks.hash is deprecated (use Compilation.hooks.fullHash instead)",
|
182 | "DEP_WEBPACK_MAIN_TEMPLATE_HASH"
|
183 | )
|
184 | },
|
185 | hashForChunk: {
|
186 | tap: util.deprecate(
|
187 | (options, fn) => {
|
188 | getJavascriptModulesPlugin()
|
189 | .getCompilationHooks(compilation)
|
190 | .chunkHash.tap(options, (chunk, hash) => {
|
191 | if (!chunk.hasRuntime()) return;
|
192 | return fn(hash, chunk);
|
193 | });
|
194 | },
|
195 | "MainTemplate.hooks.hashForChunk is deprecated (use JavascriptModulesPlugin.getCompilationHooks().chunkHash instead)",
|
196 | "DEP_WEBPACK_MAIN_TEMPLATE_HASH_FOR_CHUNK"
|
197 | )
|
198 | },
|
199 | globalHashPaths: {
|
200 | tap: util.deprecate(
|
201 | () => {},
|
202 | "MainTemplate.hooks.globalHashPaths has been removed (it's no longer needed)",
|
203 | "DEP_WEBPACK_MAIN_TEMPLATE_HASH_FOR_CHUNK"
|
204 | )
|
205 | },
|
206 | globalHash: {
|
207 | tap: util.deprecate(
|
208 | () => {},
|
209 | "MainTemplate.hooks.globalHash has been removed (it's no longer needed)",
|
210 | "DEP_WEBPACK_MAIN_TEMPLATE_HASH_FOR_CHUNK"
|
211 | )
|
212 | },
|
213 | hotBootstrap: {
|
214 | tap: () => {
|
215 | throw new Error(
|
216 | "MainTemplate.hooks.hotBootstrap has been removed (use your own RuntimeModule instead)"
|
217 | );
|
218 | }
|
219 | },
|
220 |
|
221 |
|
222 |
|
223 | bootstrap: new SyncWaterfallHook([
|
224 | "source",
|
225 | "chunk",
|
226 | "hash",
|
227 | "moduleTemplate",
|
228 | "dependencyTemplates"
|
229 | ]),
|
230 |
|
231 | localVars: new SyncWaterfallHook(["source", "chunk", "hash"]),
|
232 |
|
233 | requireExtensions: new SyncWaterfallHook(["source", "chunk", "hash"]),
|
234 |
|
235 | requireEnsure: new SyncWaterfallHook([
|
236 | "source",
|
237 | "chunk",
|
238 | "hash",
|
239 | "chunkIdExpression"
|
240 | ]),
|
241 | get jsonpScript() {
|
242 | const hooks = getLoadScriptRuntimeModule().getCompilationHooks(
|
243 | compilation
|
244 | );
|
245 | return hooks.createScript;
|
246 | },
|
247 | get linkPrefetch() {
|
248 | const hooks = getJsonpTemplatePlugin().getCompilationHooks(compilation);
|
249 | return hooks.linkPrefetch;
|
250 | },
|
251 | get linkPreload() {
|
252 | const hooks = getJsonpTemplatePlugin().getCompilationHooks(compilation);
|
253 | return hooks.linkPreload;
|
254 | }
|
255 | });
|
256 |
|
257 | this.renderCurrentHashCode = util.deprecate(
|
258 | |
259 |
|
260 |
|
261 |
|
262 |
|
263 | (hash, length) => {
|
264 | if (length) {
|
265 | return `${RuntimeGlobals.getFullHash} ? ${
|
266 | RuntimeGlobals.getFullHash
|
267 | }().slice(0, ${length}) : ${hash.slice(0, length)}`;
|
268 | }
|
269 | return `${RuntimeGlobals.getFullHash} ? ${RuntimeGlobals.getFullHash}() : ${hash}`;
|
270 | },
|
271 | "MainTemplate.renderCurrentHashCode is deprecated (use RuntimeGlobals.getFullHash runtime function instead)",
|
272 | "DEP_WEBPACK_MAIN_TEMPLATE_RENDER_CURRENT_HASH_CODE"
|
273 | );
|
274 |
|
275 | this.getPublicPath = util.deprecate(
|
276 | |
277 |
|
278 |
|
279 |
|
280 | options => {
|
281 | return compilation.getAssetPath(
|
282 | compilation.outputOptions.publicPath,
|
283 | options
|
284 | );
|
285 | },
|
286 | "MainTemplate.getPublicPath is deprecated (use Compilation.getAssetPath(compilation.outputOptions.publicPath, options) instead)",
|
287 | "DEP_WEBPACK_MAIN_TEMPLATE_GET_PUBLIC_PATH"
|
288 | );
|
289 |
|
290 | this.getAssetPath = util.deprecate(
|
291 | (path, options) => {
|
292 | return compilation.getAssetPath(path, options);
|
293 | },
|
294 | "MainTemplate.getAssetPath is deprecated (use Compilation.getAssetPath instead)",
|
295 | "DEP_WEBPACK_MAIN_TEMPLATE_GET_ASSET_PATH"
|
296 | );
|
297 |
|
298 | this.getAssetPathWithInfo = util.deprecate(
|
299 | (path, options) => {
|
300 | return compilation.getAssetPathWithInfo(path, options);
|
301 | },
|
302 | "MainTemplate.getAssetPathWithInfo is deprecated (use Compilation.getAssetPath instead)",
|
303 | "DEP_WEBPACK_MAIN_TEMPLATE_GET_ASSET_PATH_WITH_INFO"
|
304 | );
|
305 | }
|
306 | }
|
307 |
|
308 | Object.defineProperty(MainTemplate.prototype, "requireFn", {
|
309 | get: util.deprecate(
|
310 | () => "__webpack_require__",
|
311 | 'MainTemplate.requireFn is deprecated (use "__webpack_require__")',
|
312 | "DEP_WEBPACK_MAIN_TEMPLATE_REQUIRE_FN"
|
313 | )
|
314 | });
|
315 |
|
316 | Object.defineProperty(MainTemplate.prototype, "outputOptions", {
|
317 | get: util.deprecate(
|
318 | /**
|
319 | * @this {MainTemplate}
|
320 | * @returns {OutputOptions} output options
|
321 | */
|
322 | function () {
|
323 | return this._outputOptions;
|
324 | },
|
325 | "MainTemplate.outputOptions is deprecated (use Compilation.outputOptions instead)",
|
326 | "DEP_WEBPACK_MAIN_TEMPLATE_OUTPUT_OPTIONS"
|
327 | )
|
328 | });
|
329 |
|
330 | module.exports = MainTemplate;
|