Skip to content

Commit 3247725

Browse files
committed
refactored MainTemplate to plugin interface
1 parent bf7922c commit 3247725

25 files changed

Lines changed: 833 additions & 869 deletions

lib/AmdMainTemplateDecorator.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

lib/AmdMainTemplatePlugin.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var ConcatSource = require("webpack-core/lib/ConcatSource");
6+
var Template = require("./Template");
7+
8+
function AmdMainTemplatePlugin(name) {
9+
this.name = name;
10+
}
11+
module.exports = AmdMainTemplatePlugin;
12+
AmdMainTemplatePlugin.prototype.apply = function(mainTemplate) {
13+
mainTemplate.plugin("render", function(source, chunk, hash, moduleTemplate, dependencyTemplates) {
14+
var externals = chunk.modules.filter(function(m) {
15+
return m.external;
16+
});
17+
var externalsDepsArray = JSON.stringify(externals.map(function(m) {
18+
return typeof m.request === "object" ? m.request.amd : m.request;
19+
}));
20+
var externalsArguments = externals.map(function(m) {
21+
return "__WEBPACK_EXTERNAL_MODULE_" + m.id + "__";
22+
}).join(", ");
23+
if(this.name) {
24+
var name = this.name
25+
.replace(Template.REGEXP_HASH, hash)
26+
.replace(Template.REGEXP_CHUNKHASH, chunk.renderedHash)
27+
.replace(Template.REGEXP_ID, chunk.id)
28+
.replace(Template.REGEXP_NAME, chunk.name || "");
29+
return new ConcatSource("define(" + JSON.stringify(name) + ", " + externalsDepsArray + ", function(" + externalsArguments + ") { return ", source, "});");
30+
} else if(externalsArguments) {
31+
return new ConcatSource("define(" + externalsDepsArray + ", function(" + externalsArguments + ") { return ", source, "});");
32+
} else {
33+
return new ConcatSource("define(function() { return ", source, "});");
34+
}
35+
}.bind(this));
36+
mainTemplate.plugin("global-hash", function(chunk) {
37+
if(Template.REGEXP_HASH.test(this.name || ""))
38+
return true;
39+
}.bind(this));
40+
mainTemplate.plugin("hash", function(hash) {
41+
hash.update("exports amd");
42+
hash.update(this.name + "");
43+
}.bind(this));
44+
};

lib/Compilation.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,24 @@ var ArrayMap = require("./ArrayMap");
1515
var Chunk = require("./Chunk");
1616
var Stats = require("./Stats");
1717
var Template = require("./Template");
18+
var MainTemplate = require("./MainTemplate");
1819

1920
function Compilation(compiler) {
2021
Tapable.call(this);
2122
this.compiler = compiler;
22-
this.mainTemplate = compiler.mainTemplate;
23-
this.chunkTemplate = compiler.chunkTemplate;
24-
this.hotUpdateChunkTemplate = compiler.hotUpdateChunkTemplate;
25-
this.moduleTemplate = compiler.moduleTemplate;
2623
this.resolvers = compiler.resolvers;
2724
this.inputFileSystem = compiler.inputFileSystem;
25+
2826
var options = this.options = compiler.options;
2927
this.outputOptions = options && options.output;
3028
this.bail = options && options.bail;
3129
this.profile = options && options.profile;
30+
31+
this.mainTemplate = new MainTemplate(this.outputOptions);
32+
this.chunkTemplate = compiler.chunkTemplate;
33+
this.hotUpdateChunkTemplate = compiler.hotUpdateChunkTemplate;
34+
this.moduleTemplate = compiler.moduleTemplate;
35+
3236
this.entries = [];
3337
this.preparedChunks = [];
3438
this.chunks = [];

lib/Compiler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ Watching.prototype.close = function(callback) {
122122

123123
function Compiler() {
124124
Tapable.call(this);
125-
this.mainTemplate = this.chunkTemplate = this.moduleTemplate = null;
125+
this.chunkTemplate = this.moduleTemplate = null;
126126

127127
this.outputPath = "";
128128
this.outputFileSystem = null;
@@ -305,11 +305,10 @@ Compiler.prototype.readRecords = function readRecords(callback) {
305305
Compiler.prototype.createChildCompiler = function(compilation, compilerName, outputOptions) {
306306
var childCompiler = new Compiler();
307307
for(var name in this._plugins) {
308-
if(["make", "compile", "emit", "after-emit", "invalid", "done"].indexOf(name) < 0)
308+
if(["make", "compile", "emit", "after-emit", "invalid", "done", "this-compilation"].indexOf(name) < 0)
309309
childCompiler._plugins[name] = this._plugins[name].slice();
310310
}
311311
childCompiler.name = compilerName;
312-
childCompiler.mainTemplate = this.mainTemplate;
313312
childCompiler.chunkTemplate = this.chunkTemplate;
314313
childCompiler.moduleTemplate = this.moduleTemplate;
315314
childCompiler.outputPath = this.outputPath;
@@ -343,6 +342,7 @@ Compiler.prototype.newCompilation = function(params) {
343342
compilation.contextTimestamps = this.contextTimestamps;
344343
compilation.name = this.name;
345344
compilation.records = this.records;
345+
this.applyPlugins("this-compilation", compilation, params);
346346
this.applyPlugins("compilation", compilation, params);
347347
return compilation;
348348
};

lib/HotModuleReplacementPlugin.js

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
1717
var hotUpdateMainFilename = compiler.options.output.hotUpdateMainFilename;
1818
compiler.plugin("compilation", function(compilation, params) {
1919
var hotUpdateChunkTemplate = compilation.compiler.hotUpdateChunkTemplate;
20-
if(!hotUpdateChunkTemplate && !compilation.mainTemplate.renderHotModuleReplacementInit) return;
20+
if(!hotUpdateChunkTemplate) return;
2121

2222
var normalModuleFactory = params.normalModuleFactory;
2323
var contextModuleFactory = params.contextModuleFactory;
@@ -107,41 +107,43 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
107107
var mainTemplate = compilation.mainTemplate;
108108
compilation.mainTemplate = Object.create(mainTemplate);
109109

110-
compilation.mainTemplate.updateHash = function(hash) {
110+
compilation.mainTemplate.plugin("hash", function(hash) {
111111
hash.update("HotMainTemplateDecorator");
112-
mainTemplate.updateHash(hash);
113-
};
112+
});
114113

115-
compilation.mainTemplate.renderRequireFunctionForModule = function(hash, chunk, varModuleId) {
114+
compilation.mainTemplate.plugin("module-require", function(_, chunk, hash, varModuleId) {
116115
return "hotCreateRequire(" + varModuleId + ")";
117-
};
116+
});
118117

119-
compilation.mainTemplate.renderInit = function(hash, chunk) {
120-
var buf = mainTemplate.renderInit(hash, chunk);
121-
buf = buf.concat(this.renderHotModuleReplacementInit(hash, chunk));
122-
buf.push("\n\n");
123-
buf.push(hotInitCode
124-
.replace(/\$require\$/g, this.requireFn)
125-
.replace(/\$hash\$/g, JSON.stringify(hash))
126-
.replace(/\/\*foreachInstalledChunks\*\//g, chunk.chunks.length > 0 ? "for(var chunkId in installedChunks)" : "var chunkId = 0;"));
127-
return buf;
128-
};
118+
compilation.mainTemplate.plugin("bootstrap", function(source, chunk, hash) {
119+
source = this.applyPluginsWaterfall("hot-bootstrap", source, chunk, hash);
120+
return this.asString([
121+
source,
122+
"",
123+
hotInitCode
124+
.replace(/\$require\$/g, this.requireFn)
125+
.replace(/\$hash\$/g, JSON.stringify(hash))
126+
.replace(/\/\*foreachInstalledChunks\*\//g, chunk.chunks.length > 0 ? "for(var chunkId in installedChunks)" : "var chunkId = 0;")
127+
]);
128+
});
129129

130-
compilation.mainTemplate.useChunkHash = false;
130+
compilation.mainTemplate.plugin("global-hash", function() {
131+
return true;
132+
});
131133

132-
compilation.mainTemplate.renderCurrentHashCode = function(hash) {
134+
compilation.mainTemplate.plugin("current-hash", function() {
133135
return "hotCurrentHash";
134-
};
136+
});
135137

136-
compilation.mainTemplate.renderModule = function(hash, chunk, varModuleId) {
137-
var buf = mainTemplate.renderModule(hash, chunk, varModuleId);
138-
buf.push(buf.pop() + ",");
139-
buf.push("hot: hotCreateModule(" + varModuleId + "),");
140-
buf.push("parents: [hotCurrentParent],");
141-
buf.push("data: hotCurrentModuleData[" + varModuleId + "],");
142-
buf.push("children: []");
143-
return buf;
144-
};
138+
compilation.mainTemplate.plugin("module-obj", function(source, chunk, hash, varModuleId) {
139+
return this.asString([
140+
source + ",",
141+
"hot: hotCreateModule(" + varModuleId + "),",
142+
"parents: [hotCurrentParent],",
143+
"data: hotCurrentModuleData[" + varModuleId + "],",
144+
"children: []"
145+
]);
146+
});
145147

146148
});
147149
compiler.parser.plugin("evaluate Identifier module.hot", function(expr) {

lib/JsonpExportMainTemplateDecorator.js

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var ConcatSource = require("webpack-core/lib/ConcatSource");
6+
var Template = require("./Template");
7+
8+
function JsonpExportMainTemplatePlugin(name) {
9+
this.name = name;
10+
}
11+
module.exports = JsonpExportMainTemplatePlugin;
12+
JsonpExportMainTemplatePlugin.prototype.apply = function(mainTemplate) {
13+
mainTemplate.plugin("render", function(source, chunk, hash) {
14+
var name = (this.name || "")
15+
.replace(Template.REGEXP_HASH, hash)
16+
.replace(Template.REGEXP_CHUNKHASH, chunk.renderedHash)
17+
.replace(Template.REGEXP_ID, chunk.id)
18+
.replace(Template.REGEXP_NAME, chunk.name || "");
19+
return new ConcatSource(name + "(", source, ");");
20+
}.bind(this));
21+
mainTemplate.plugin("global-hash", function(chunk) {
22+
if(Template.REGEXP_HASH.test(this.name || ""))
23+
return true;
24+
}.bind(this));
25+
mainTemplate.plugin("hash", function(hash) {
26+
hash.update("jsonp export");
27+
hash.update(this.name + "");
28+
}.bind(this));
29+
};

0 commit comments

Comments
 (0)