2013-06-19 19:49:57 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
var Template = require("./Template");
|
|
|
|
var BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
|
|
|
|
var ModuleHotAcceptDependency = require("./dependencies/ModuleHotAcceptDependency");
|
|
|
|
var ModuleHotDeclineDependency = require("./dependencies/ModuleHotDeclineDependency");
|
2013-07-01 19:59:02 +08:00
|
|
|
var RawSource = require("webpack-core/lib/RawSource");
|
2014-08-25 15:50:26 +08:00
|
|
|
var ConstDependency = require("./dependencies/ConstDependency");
|
|
|
|
var NullFactory = require("./NullFactory");
|
2013-06-19 19:49:57 +08:00
|
|
|
|
2013-12-18 06:21:49 +08:00
|
|
|
function HotModuleReplacementPlugin() {
|
2013-06-19 19:49:57 +08:00
|
|
|
}
|
|
|
|
module.exports = HotModuleReplacementPlugin;
|
|
|
|
|
|
|
|
HotModuleReplacementPlugin.prototype.apply = function(compiler) {
|
2013-12-18 06:21:49 +08:00
|
|
|
var hotUpdateChunkFilename = compiler.options.output.hotUpdateChunkFilename;
|
|
|
|
var hotUpdateMainFilename = compiler.options.output.hotUpdateMainFilename;
|
2013-06-19 19:49:57 +08:00
|
|
|
compiler.plugin("compilation", function(compilation, params) {
|
2014-06-03 06:14:46 +08:00
|
|
|
var hotUpdateChunkTemplate = compilation.hotUpdateChunkTemplate;
|
2014-06-03 03:23:53 +08:00
|
|
|
if(!hotUpdateChunkTemplate) return;
|
2013-06-20 18:04:31 +08:00
|
|
|
|
2013-06-19 19:49:57 +08:00
|
|
|
var normalModuleFactory = params.normalModuleFactory;
|
|
|
|
|
2014-08-25 15:50:26 +08:00
|
|
|
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
|
|
|
|
compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());
|
|
|
|
|
2013-06-19 19:49:57 +08:00
|
|
|
compilation.dependencyFactories.set(ModuleHotAcceptDependency, normalModuleFactory);
|
|
|
|
compilation.dependencyTemplates.set(ModuleHotAcceptDependency, new ModuleHotAcceptDependency.Template());
|
|
|
|
|
|
|
|
compilation.dependencyFactories.set(ModuleHotDeclineDependency, normalModuleFactory);
|
|
|
|
compilation.dependencyTemplates.set(ModuleHotDeclineDependency, new ModuleHotDeclineDependency.Template());
|
|
|
|
|
|
|
|
compilation.plugin("record", function(compilation, records) {
|
|
|
|
if(records.hash === this.hash) return;
|
|
|
|
records.hash = compilation.hash;
|
|
|
|
records.moduleHashs = {};
|
|
|
|
this.modules.forEach(function(module) {
|
|
|
|
var identifier = module.identifier();
|
2013-07-11 05:20:07 +08:00
|
|
|
var hash = require("crypto").createHash("md5");
|
2013-06-19 19:49:57 +08:00
|
|
|
module.updateHash(hash);
|
|
|
|
records.moduleHashs[identifier] = hash.digest("hex");
|
|
|
|
});
|
|
|
|
records.chunkHashs = {};
|
|
|
|
this.chunks.forEach(function(chunk) {
|
|
|
|
records.chunkHashs[chunk.id] = chunk.hash;
|
|
|
|
});
|
|
|
|
records.chunkModuleIds = {};
|
|
|
|
this.chunks.forEach(function(chunk) {
|
|
|
|
records.chunkModuleIds[chunk.id] = chunk.modules.map(function(m) { return m.id; });
|
|
|
|
});
|
|
|
|
});
|
2013-07-01 19:59:02 +08:00
|
|
|
compilation.plugin("after-hash", function() {
|
|
|
|
var records = this.records;
|
|
|
|
if(!records) return;
|
2013-07-08 14:12:48 +08:00
|
|
|
var preHash = records.preHash || "x";
|
|
|
|
var prepreHash = records.prepreHash || "x";
|
2013-07-05 20:17:19 +08:00
|
|
|
if(preHash === this.hash) {
|
2013-07-08 14:12:48 +08:00
|
|
|
this.modifyHash(prepreHash);
|
2013-07-01 19:59:02 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-07-05 20:56:16 +08:00
|
|
|
records.prepreHash = records.hash || "x";
|
2013-07-01 19:59:02 +08:00
|
|
|
records.preHash = this.hash;
|
2013-07-05 20:56:16 +08:00
|
|
|
this.modifyHash(records.prepreHash);
|
2013-07-01 19:59:02 +08:00
|
|
|
});
|
|
|
|
compilation.plugin("additional-chunk-assets", function() {
|
2013-06-19 19:49:57 +08:00
|
|
|
var records = this.records;
|
|
|
|
if(records.hash === this.hash) return;
|
|
|
|
if(!records.moduleHashs || !records.chunkHashs || !records.chunkModuleIds) return;
|
|
|
|
this.modules.forEach(function(module) {
|
|
|
|
var identifier = module.identifier();
|
2013-07-11 05:20:07 +08:00
|
|
|
var hash = require("crypto").createHash("md5");
|
2013-06-19 19:49:57 +08:00
|
|
|
module.updateHash(hash);
|
|
|
|
hash = hash.digest("hex");
|
|
|
|
module.hotUpdate = records.moduleHashs[identifier] !== hash;
|
|
|
|
});
|
2013-07-01 19:59:02 +08:00
|
|
|
var hotUpdateMainContent = {
|
|
|
|
h: this.hash,
|
|
|
|
c: []
|
|
|
|
};
|
2013-06-19 19:49:57 +08:00
|
|
|
Object.keys(records.chunkHashs).forEach(function(chunkId) {
|
|
|
|
chunkId = +chunkId;
|
|
|
|
var currentChunk = this.chunks.filter(function(chunk) {
|
|
|
|
return chunk.id === chunkId;
|
|
|
|
})[0];
|
|
|
|
if(currentChunk) {
|
2013-07-25 02:50:41 +08:00
|
|
|
var newModules = currentChunk.modules.filter(function(module) {
|
|
|
|
return module.hotUpdate;
|
2013-06-19 19:49:57 +08:00
|
|
|
});
|
2013-07-25 02:50:41 +08:00
|
|
|
if(newModules.length > 0) {
|
|
|
|
var source = hotUpdateChunkTemplate.render(chunkId, newModules, this.hash, this.moduleTemplate, this.dependencyTemplates);
|
2014-08-22 19:51:24 +08:00
|
|
|
var filename = this.getPath(hotUpdateChunkFilename, {
|
|
|
|
hash: records.hash,
|
|
|
|
chunk: currentChunk
|
|
|
|
});
|
2013-12-13 15:55:13 +08:00
|
|
|
this.additionalChunkAssets.push(filename);
|
2013-07-25 02:50:41 +08:00
|
|
|
this.assets[filename] = source;
|
|
|
|
hotUpdateMainContent.c.push(chunkId);
|
2013-07-01 19:59:02 +08:00
|
|
|
currentChunk.files.push(filename);
|
|
|
|
this.applyPlugins("chunk-asset", currentChunk, filename);
|
|
|
|
}
|
2013-06-19 19:49:57 +08:00
|
|
|
}
|
|
|
|
}, this);
|
2013-07-01 19:59:02 +08:00
|
|
|
var source = new RawSource(JSON.stringify(hotUpdateMainContent));
|
2014-08-22 19:51:24 +08:00
|
|
|
var filename = this.getPath(hotUpdateMainFilename, {
|
|
|
|
hash: records.hash
|
|
|
|
});
|
2013-07-01 19:59:02 +08:00
|
|
|
this.assets[filename] = source;
|
2013-06-19 19:49:57 +08:00
|
|
|
});
|
|
|
|
|
2014-06-03 03:23:53 +08:00
|
|
|
compilation.mainTemplate.plugin("hash", function(hash) {
|
2013-07-01 19:59:02 +08:00
|
|
|
hash.update("HotMainTemplateDecorator");
|
2014-06-03 03:23:53 +08:00
|
|
|
});
|
2013-06-19 22:09:46 +08:00
|
|
|
|
2014-06-03 03:23:53 +08:00
|
|
|
compilation.mainTemplate.plugin("module-require", function(_, chunk, hash, varModuleId) {
|
2013-06-19 19:49:57 +08:00
|
|
|
return "hotCreateRequire(" + varModuleId + ")";
|
2014-06-03 03:23:53 +08:00
|
|
|
});
|
2013-06-19 19:49:57 +08:00
|
|
|
|
2015-04-24 05:55:50 +08:00
|
|
|
compilation.mainTemplate.plugin("require-extensions", function(source) {
|
2014-08-25 15:50:26 +08:00
|
|
|
var buf = [source];
|
|
|
|
buf.push("");
|
|
|
|
buf.push("// __webpack_hash__");
|
|
|
|
buf.push(this.requireFn + ".h = function() { return hotCurrentHash; };");
|
|
|
|
return this.asString(buf);
|
|
|
|
});
|
|
|
|
|
2014-06-03 03:23:53 +08:00
|
|
|
compilation.mainTemplate.plugin("bootstrap", function(source, chunk, hash) {
|
|
|
|
source = this.applyPluginsWaterfall("hot-bootstrap", source, chunk, hash);
|
|
|
|
return this.asString([
|
|
|
|
source,
|
|
|
|
"",
|
|
|
|
hotInitCode
|
|
|
|
.replace(/\$require\$/g, this.requireFn)
|
|
|
|
.replace(/\$hash\$/g, JSON.stringify(hash))
|
2015-04-24 05:55:50 +08:00
|
|
|
.replace(/\/\*foreachInstalledChunks\*\//g, chunk.chunks.length > 0 ? "for(var chunkId in installedChunks)" : "var chunkId = " + chunk.id + ";")
|
2014-06-03 03:23:53 +08:00
|
|
|
]);
|
|
|
|
});
|
2013-06-19 19:49:57 +08:00
|
|
|
|
2014-06-03 03:23:53 +08:00
|
|
|
compilation.mainTemplate.plugin("global-hash", function() {
|
|
|
|
return true;
|
|
|
|
});
|
2014-04-20 03:35:01 +08:00
|
|
|
|
2014-11-03 15:02:02 +08:00
|
|
|
compilation.mainTemplate.plugin("current-hash", function(_, length) {
|
|
|
|
if(isFinite(length))
|
|
|
|
return "hotCurrentHash.substr(0, " + length + ")";
|
|
|
|
else
|
|
|
|
return "hotCurrentHash";
|
2014-06-03 03:23:53 +08:00
|
|
|
});
|
2013-06-19 19:49:57 +08:00
|
|
|
|
2014-06-03 03:23:53 +08:00
|
|
|
compilation.mainTemplate.plugin("module-obj", function(source, chunk, hash, varModuleId) {
|
|
|
|
return this.asString([
|
|
|
|
source + ",",
|
2014-08-13 20:53:23 +08:00
|
|
|
"hot: hotCreateModule(" + varModuleId + "),",
|
2014-09-18 05:23:45 +08:00
|
|
|
"parents: hotCurrentParents,",
|
2014-06-03 03:23:53 +08:00
|
|
|
"children: []"
|
|
|
|
]);
|
|
|
|
});
|
2013-06-19 19:49:57 +08:00
|
|
|
|
|
|
|
});
|
2014-08-25 15:50:26 +08:00
|
|
|
compiler.parser.plugin("expression __webpack_hash__", function(expr) {
|
|
|
|
var dep = new ConstDependency("__webpack_require__.h()", expr.range);
|
|
|
|
dep.loc = expr.loc;
|
|
|
|
this.state.current.addDependency(dep);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
compiler.parser.plugin("evaluate typeof __webpack_hash__", function(expr) {
|
|
|
|
return new BasicEvaluatedExpression().setString("string").setRange(expr.range);
|
|
|
|
});
|
2013-06-19 19:49:57 +08:00
|
|
|
compiler.parser.plugin("evaluate Identifier module.hot", function(expr) {
|
2013-06-20 18:04:31 +08:00
|
|
|
return new BasicEvaluatedExpression()
|
2014-06-03 06:14:46 +08:00
|
|
|
.setBoolean(!!this.state.compilation.hotUpdateChunkTemplate)
|
2013-06-20 18:04:31 +08:00
|
|
|
.setRange(expr.range);
|
2013-06-19 19:49:57 +08:00
|
|
|
});
|
|
|
|
compiler.parser.plugin("call module.hot.accept", function(expr) {
|
2014-06-03 06:14:46 +08:00
|
|
|
if(!this.state.compilation.hotUpdateChunkTemplate) return false;
|
2013-06-19 19:49:57 +08:00
|
|
|
if(expr.arguments.length > 1) {
|
2014-09-14 08:33:35 +08:00
|
|
|
var arg = this.evaluateExpression(expr.arguments[0]);
|
2014-09-16 00:04:04 +08:00
|
|
|
var params = [];
|
|
|
|
if(arg.isString()) {
|
|
|
|
params = [arg];
|
|
|
|
}
|
|
|
|
if(arg.isArray()){
|
2015-04-24 05:55:50 +08:00
|
|
|
params = arg.items.filter(function(param) {
|
2014-09-18 07:08:18 +08:00
|
|
|
return param.isString();
|
|
|
|
});
|
2014-09-16 00:04:04 +08:00
|
|
|
}
|
|
|
|
params.forEach(function(param){
|
2013-06-19 19:49:57 +08:00
|
|
|
var dep = new ModuleHotAcceptDependency(param.string, param.range);
|
|
|
|
dep.optional = true;
|
|
|
|
this.state.module.addDependency(dep);
|
2014-09-16 00:04:04 +08:00
|
|
|
}.bind(this));
|
2013-06-19 19:49:57 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
compiler.parser.plugin("call module.hot.decline", function(expr) {
|
2014-06-03 06:14:46 +08:00
|
|
|
if(!this.state.compilation.hotUpdateChunkTemplate) return false;
|
2013-06-19 19:49:57 +08:00
|
|
|
if(expr.arguments.length > 1) {
|
|
|
|
var param = this.evaluateExpression(expr.arguments[0]);
|
|
|
|
if(param.isString()) {
|
|
|
|
var dep = new ModuleHotDeclineDependency(param.string, param.range);
|
|
|
|
dep.optional = true;
|
|
|
|
this.state.module.addDependency(dep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
compiler.parser.plugin("expression module.hot", function() {
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-04-29 05:38:41 +08:00
|
|
|
var hotInitCode = Template.getFunctionContent(require("./HotModuleReplacement.runtime.js"));
|