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