Skip to content

Commit 8678681

Browse files
authored
Merge pull request webpack#6085 from webpack/refactor/static-template
make all methods on Template static
2 parents bbf1458 + e8da5a9 commit 8678681

12 files changed

+126
-120
lines changed

lib/ChunkTemplate.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
const ConcatSource = require("webpack-sources").ConcatSource;
88
const Template = require("./Template");
9+
const Tapable = require("tapable").Tapable;
910
const SyncWaterfallHook = require("tapable").SyncWaterfallHook;
1011
const SyncHook = require("tapable").SyncHook;
1112

12-
module.exports = class ChunkTemplate extends Template {
13+
module.exports = class ChunkTemplate extends Tapable {
1314
constructor(outputOptions) {
14-
super(outputOptions);
15+
super();
16+
this.outputOptions = outputOptions || {};
1517
this.hooks = {
1618
modules: new SyncWaterfallHook(["source", "chunk", "moduleTemplate", "dependencyTemplates"]),
1719
render: new SyncWaterfallHook(["source", "chunk", "moduleTemplate", "dependencyTemplates"]),
@@ -67,7 +69,7 @@ module.exports = class ChunkTemplate extends Template {
6769
}
6870

6971
renderJavascript(chunk, moduleTemplate, dependencyTemplates) {
70-
const moduleSources = this.renderChunkModules(chunk, m => true, moduleTemplate, dependencyTemplates);
72+
const moduleSources = Template.renderChunkModules(chunk, m => true, moduleTemplate, dependencyTemplates);
7173
const core = this.hooks.modules.call(moduleSources, chunk, moduleTemplate, dependencyTemplates);
7274
let source = this.hooks.render.call(core, chunk, moduleTemplate, dependencyTemplates);
7375
if(chunk.hasEntryModule()) {

lib/ExtendedAPIPlugin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
"use strict";
66

7+
const Template = require("./Template");
78
const ConstDependency = require("./dependencies/ConstDependency");
89
const ParserHelpers = require("./ParserHelpers");
910
const NullFactory = require("./NullFactory");
@@ -34,7 +35,7 @@ class ExtendedAPIPlugin {
3435
buf.push("");
3536
buf.push("// __webpack_chunkname__");
3637
buf.push(`${mainTemplate.requireFn}.cn = ${JSON.stringify(chunk.name)};`);
37-
return mainTemplate.asString(buf);
38+
return Template.asString(buf);
3839
});
3940
mainTemplate.plugin("global-hash", () => true);
4041

lib/HotModuleReplacementPlugin.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,12 @@ module.exports = class HotModuleReplacementPlugin {
158158
buf.push("");
159159
buf.push("// __webpack_hash__");
160160
buf.push(mainTemplate.requireFn + ".h = function() { return hotCurrentHash; };");
161-
return mainTemplate.asString(buf);
161+
return Template.asString(buf);
162162
});
163163

164164
mainTemplate.plugin("bootstrap", (source, chunk, hash) => {
165165
source = mainTemplate.hooks.hotBootstrap.call(source, chunk, hash);
166-
return mainTemplate.asString([
166+
return Template.asString([
167167
source,
168168
"",
169169
hotInitCode
@@ -184,7 +184,7 @@ module.exports = class HotModuleReplacementPlugin {
184184
});
185185

186186
mainTemplate.plugin("module-obj", (source, chunk, hash, varModuleId) => {
187-
return mainTemplate.asString([
187+
return Template.asString([
188188
`${source},`,
189189
`hot: hotCreateModule(${varModuleId}),`,
190190
"parents: (hotCurrentParentsTemp = hotCurrentParents, hotCurrentParents = [], hotCurrentParentsTemp),",

lib/HotUpdateChunkTemplate.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
const Template = require("./Template");
88
const Chunk = require("./Chunk");
9+
const Tapable = require("tapable").Tapable;
910
const SyncWaterfallHook = require("tapable").SyncWaterfallHook;
1011
const SyncHook = require("tapable").SyncHook;
1112

12-
module.exports = class HotUpdateChunkTemplate extends Template {
13+
module.exports = class HotUpdateChunkTemplate extends Tapable {
1314
constructor(outputOptions) {
14-
super(outputOptions);
15+
super();
16+
this.outputOptions = outputOptions || {};
1517
this.hooks = {
1618
modules: new SyncWaterfallHook(["source", "modules", "removedModules", "moduleTemplate", "dependencyTemplates"]),
1719
render: new SyncWaterfallHook(["source", "modules", "removedModules", "hash", "id", "moduleTemplate", "dependencyTemplates"]),
@@ -24,7 +26,7 @@ module.exports = class HotUpdateChunkTemplate extends Template {
2426
hotUpdateChunk.id = id;
2527
hotUpdateChunk.setModules(modules);
2628
hotUpdateChunk.removedModules = removedModules;
27-
const modulesSource = this.renderChunkModules(hotUpdateChunk, () => true, moduleTemplate, dependencyTemplates);
29+
const modulesSource = Template.renderChunkModules(hotUpdateChunk, () => true, moduleTemplate, dependencyTemplates);
2830
const core = this.hooks.modules.call(modulesSource, modules, removedModules, moduleTemplate, dependencyTemplates);
2931
const source = this.hooks.render.call(core, modules, removedModules, hash, id, moduleTemplate, dependencyTemplates);
3032
return source;

lib/MainTemplate.js

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const ConcatSource = require("webpack-sources").ConcatSource;
88
const OriginalSource = require("webpack-sources").OriginalSource;
99
const PrefixSource = require("webpack-sources").PrefixSource;
1010
const Template = require("./Template");
11+
const Tapable = require("tapable").Tapable;
1112
const SyncWaterfallHook = require("tapable").SyncWaterfallHook;
1213
const SyncHook = require("tapable").SyncHook;
1314
const SyncBailHook = require("tapable").SyncBailHook;
@@ -28,9 +29,10 @@ const SyncBailHook = require("tapable").SyncBailHook;
2829
// __webpack_require__.oe = the uncatched error handler for the webpack runtime
2930
// __webpack_require__.nc = the script nonce
3031

31-
module.exports = class MainTemplate extends Template {
32+
module.exports = class MainTemplate extends Tapable {
3233
constructor(outputOptions) {
33-
super(outputOptions);
34+
super();
35+
this.outputOptions = outputOptions || {};
3436
this.hooks = {
3537
modules: new SyncWaterfallHook(["modules", "chunk", "hash", "moduleTemplate", "dependencyTemplates"]),
3638
moduleObj: new SyncWaterfallHook(["source", "chunk", "hash", "moduleIdExpression"]),
@@ -61,7 +63,7 @@ module.exports = class MainTemplate extends Template {
6163
buf.push("// Load entry module and return exports");
6264
buf.push(`return ${this.renderRequireFunctionForModule(hash, chunk, JSON.stringify(chunk.entryModule.id))}(${this.requireFn}.s = ${JSON.stringify(chunk.entryModule.id)});`);
6365
}
64-
return this.asString(buf);
66+
return Template.asString(buf);
6567
});
6668
this.plugin("render", (bootstrapSource, chunk, hash, moduleTemplate, dependencyTemplates) => {
6769
const source = new ConcatSource();
@@ -70,40 +72,40 @@ module.exports = class MainTemplate extends Template {
7072
source.add("/******/ })\n");
7173
source.add("/************************************************************************/\n");
7274
source.add("/******/ (");
73-
const modules = this.renderChunkModules(chunk, () => true, moduleTemplate, dependencyTemplates, "/******/ ");
75+
const modules = Template.renderChunkModules(chunk, () => true, moduleTemplate, dependencyTemplates, "/******/ ");
7476
source.add(this.hooks.modules.call(modules, chunk, hash, moduleTemplate, dependencyTemplates));
7577
source.add(")");
7678
return source;
7779
});
7880
this.plugin("local-vars", (source, chunk, hash) => {
79-
return this.asString([
81+
return Template.asString([
8082
source,
8183
"// The module cache",
8284
"var installedModules = {};"
8385
]);
8486
});
8587
this.plugin("require", (source, chunk, hash) => {
86-
return this.asString([
88+
return Template.asString([
8789
source,
8890
"// Check if module is in cache",
8991
"if(installedModules[moduleId]) {",
90-
this.indent("return installedModules[moduleId].exports;"),
92+
Template.indent("return installedModules[moduleId].exports;"),
9193
"}",
9294
"// Create a new module (and put it into the cache)",
9395
"var module = installedModules[moduleId] = {",
94-
this.indent(this.hooks.moduleObj.call("", chunk, hash, "moduleId")),
96+
Template.indent(this.hooks.moduleObj.call("", chunk, hash, "moduleId")),
9597
"};",
9698
"",
97-
this.asString(outputOptions.strictModuleExceptionHandling ? [
99+
Template.asString(outputOptions.strictModuleExceptionHandling ? [
98100
"// Execute the module function",
99101
"var threw = true;",
100102
"try {",
101-
this.indent([
103+
Template.indent([
102104
`modules[moduleId].call(module.exports, module, module.exports, ${this.renderRequireFunctionForModule(hash, chunk, "moduleId")});`,
103105
"threw = false;"
104106
]),
105107
"} finally {",
106-
this.indent([
108+
Template.indent([
107109
"if(threw) delete installedModules[moduleId];"
108110
]),
109111
"}"
@@ -120,7 +122,7 @@ module.exports = class MainTemplate extends Template {
120122
]);
121123
});
122124
this.plugin("module-obj", (source, chunk, hash, varModuleId) => {
123-
return this.asString([
125+
return Template.asString([
124126
"i: moduleId,",
125127
"l: false,",
126128
"exports: {}"
@@ -132,9 +134,9 @@ module.exports = class MainTemplate extends Template {
132134
buf.push("// This file contains only the entry chunk.");
133135
buf.push("// The chunk loading function for additional chunks");
134136
buf.push(`${this.requireFn}.e = function requireEnsure(chunkId) {`);
135-
buf.push(this.indent("var promises = [];"));
136-
buf.push(this.indent(this.hooks.requireEnsure.call("", chunk, hash, "chunkId")));
137-
buf.push(this.indent("return Promise.all(promises);"));
137+
buf.push(Template.indent("var promises = [];"));
138+
buf.push(Template.indent(this.hooks.requireEnsure.call("", chunk, hash, "chunkId")));
139+
buf.push(Template.indent("return Promise.all(promises);"));
138140
buf.push("};");
139141
}
140142
buf.push("");
@@ -148,11 +150,11 @@ module.exports = class MainTemplate extends Template {
148150
buf.push("");
149151
buf.push("// define getter function for harmony exports");
150152
buf.push(`${this.requireFn}.d = function(exports, name, getter) {`);
151-
buf.push(this.indent([
153+
buf.push(Template.indent([
152154
`if(!${this.requireFn}.o(exports, name)) {`,
153-
this.indent([
155+
Template.indent([
154156
"Object.defineProperty(exports, name, {",
155-
this.indent([
157+
Template.indent([
156158
"configurable: false,",
157159
"enumerable: true,",
158160
"get: getter"
@@ -166,17 +168,17 @@ module.exports = class MainTemplate extends Template {
166168
buf.push("");
167169
buf.push("// define __esModule on exports");
168170
buf.push(`${this.requireFn}.r = function(exports) {`);
169-
buf.push(this.indent([
171+
buf.push(Template.indent([
170172
"Object.defineProperty(exports, '__esModule', { value: true });"
171173
]));
172174
buf.push("};");
173175

174176
buf.push("");
175177
buf.push("// getDefaultExport function for compatibility with non-harmony modules");
176178
buf.push(this.requireFn + ".n = function(module) {");
177-
buf.push(this.indent([
179+
buf.push(Template.indent([
178180
"var getter = module && module.__esModule ?",
179-
this.indent([
181+
Template.indent([
180182
"function getDefault() { return module['default']; } :",
181183
"function getModuleExports() { return module; };"
182184
]),
@@ -195,7 +197,7 @@ module.exports = class MainTemplate extends Template {
195197
buf.push("");
196198
buf.push("// __webpack_public_path__");
197199
buf.push(`${this.requireFn}.p = ${JSON.stringify(publicPath)};`);
198-
return this.asString(buf);
200+
return Template.asString(buf);
199201
});
200202

201203
this.requireFn = "__webpack_require__";
@@ -243,13 +245,13 @@ module.exports = class MainTemplate extends Template {
243245
buf.push("");
244246
buf.push("// The require function");
245247
buf.push(`function ${this.requireFn}(moduleId) {`);
246-
buf.push(this.indent(this.hooks.require.call("", chunk, hash)));
248+
buf.push(Template.indent(this.hooks.require.call("", chunk, hash)));
247249
buf.push("}");
248250
buf.push("");
249-
buf.push(this.asString(this.hooks.requireExtensions.call("", chunk, hash)));
251+
buf.push(Template.asString(this.hooks.requireExtensions.call("", chunk, hash)));
250252
buf.push("");
251-
buf.push(this.asString(this.hooks.startup.call("", chunk, hash)));
252-
let source = this.hooks.render.call(new OriginalSource(this.prefix(buf, " \t") + "\n", "webpack/bootstrap"), chunk, hash, moduleTemplate, dependencyTemplates);
253+
buf.push(Template.asString(this.hooks.startup.call("", chunk, hash)));
254+
let source = this.hooks.render.call(new OriginalSource(Template.prefix(buf, " \t") + "\n", "webpack/bootstrap"), chunk, hash, moduleTemplate, dependencyTemplates);
253255
if(chunk.hasEntryModule()) {
254256
source = this.hooks.renderWithEntry.call(source, chunk, hash);
255257
}

lib/ModuleTemplate.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
*/
55
"use strict";
66

7-
const Template = require("./Template");
7+
const Tapable = require("tapable").Tapable;
88
const SyncWaterfallHook = require("tapable").SyncWaterfallHook;
99
const SyncHook = require("tapable").SyncHook;
1010

11-
module.exports = class ModuleTemplate extends Template {
11+
module.exports = class ModuleTemplate extends Tapable {
1212
constructor(outputOptions, requestShortener) {
13-
super(outputOptions);
13+
super();
14+
this.outputOptions = outputOptions || {};
1415
this.requestShortener = requestShortener;
1516
this.hooks = {
1617
content: new SyncWaterfallHook(["source", "module", "options", "dependencyTemplates"]),

lib/Template.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
"use strict";
66

7-
const Tapable = require("tapable").Tapable;
87
const ConcatSource = require("webpack-sources").ConcatSource;
98

109
const START_LOWERCASE_ALPHABET_CODE = "a".charCodeAt(0);
@@ -31,12 +30,7 @@ const moduleIdIsNumber = module => {
3130
return typeof module.id === "number";
3231
};
3332

34-
module.exports = class Template extends Tapable {
35-
constructor(outputOptions) {
36-
super();
37-
this.outputOptions = outputOptions || {};
38-
}
39-
33+
module.exports = class Template {
4034
static getFunctionContent(fn) {
4135
return fn.toString().replace(FUNCTION_CONTENT_REGEX, "").replace(INDENT_MULTILINE_REGEX, "").replace(LINE_SEPARATOR_REGEX, "\n");
4236
}
@@ -74,9 +68,9 @@ module.exports = class Template extends Tapable {
7468
return Template.numberToIdentifer(n % (2 * DELTA_A_TO_Z)) + Template.numberToIdentifer(Math.floor(n / (2 * DELTA_A_TO_Z)));
7569
}
7670

77-
indent(str) {
71+
static indent(str) {
7872
if(Array.isArray(str)) {
79-
return str.map(this.indent.bind(this)).join("\n");
73+
return str.map(Template.indent).join("\n");
8074
} else {
8175
str = str.trimRight();
8276
if(!str) return "";
@@ -85,7 +79,7 @@ module.exports = class Template extends Tapable {
8579
}
8680
}
8781

88-
prefix(str, prefix) {
82+
static prefix(str, prefix) {
8983
if(Array.isArray(str)) {
9084
str = str.join("\n");
9185
}
@@ -95,14 +89,14 @@ module.exports = class Template extends Tapable {
9589
return ind + str.replace(/\n([^\n])/g, "\n" + prefix + "$1");
9690
}
9791

98-
asString(str) {
92+
static asString(str) {
9993
if(Array.isArray(str)) {
10094
return str.join("\n");
10195
}
10296
return str;
10397
}
10498

105-
getModulesArrayBounds(modules) {
99+
static getModulesArrayBounds(modules) {
106100
if(!modules.every(moduleIdIsNumber))
107101
return false;
108102
var maxId = -Infinity;
@@ -125,7 +119,7 @@ module.exports = class Template extends Tapable {
125119
return arrayOverhead < objectOverhead ? [minId, maxId] : false;
126120
}
127121

128-
renderChunkModules(chunk, filterFn, moduleTemplate, dependencyTemplates, prefix) {
122+
static renderChunkModules(chunk, filterFn, moduleTemplate, dependencyTemplates, prefix) {
129123
if(!prefix) prefix = "";
130124
var source = new ConcatSource();
131125
const modules = chunk.getModules().filter(filterFn);
@@ -150,7 +144,7 @@ module.exports = class Template extends Tapable {
150144
});
151145
});
152146
}
153-
var bounds = this.getModulesArrayBounds(allModules);
147+
var bounds = Template.getModulesArrayBounds(allModules);
154148

155149
if(bounds) {
156150
// Render a spare array

0 commit comments

Comments
 (0)