Skip to content

Commit 8b23010

Browse files
committed
added hot module replacement, fixes webpack#26
1 parent f944553 commit 8b23010

24 files changed

Lines changed: 732 additions & 2 deletions

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Take a look at the [`examples`](https://github.com/webpack/webpack/tree/master/e
3535
* usable as [grunt plugin](https://github.com/webpack/grunt-webpack)
3636
* browser replacements
3737
* comes with browser replacements for some node.js modules
38+
* [Hot Module Replacement](https://github.com/webpack/docs/wiki/hot-code-replacement)
39+
* install updates without full page refresh
3840
* see also
3941
* [webpack-dev-middleware](https://github.com/webpack/webpack-dev-middleware)
4042
* [webpack-dev-server](https://github.com/webpack/webpack-dev-server)

bin/config-optimist.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ module.exports = function(optimist) {
4343

4444
.boolean("watch").alias("watch", "w").describe("watch")
4545

46+
.boolean("hot").alias("hot", "h").describe("hot")
47+
4648
.boolean("debug").alias("debug", "d").describe("debug")
4749

4850
.string("devtool").describe("devtool")

bin/convert-argv.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ module.exports = function(optimist, argv, convertOptions) {
207207
options.watchDelay = value;
208208
});
209209

210+
mapArgToBoolean("hot", "hot");
210211
mapArgToBoolean("debug", "debug");
211212

212213
ifBooleanArg("progress", function() {

hot/dev-server.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
if(module.hot) {
2+
window.onmessage = function(event) {
3+
if(event.data === "webpackHotUpdate" && module.hot.status() === "idle") {
4+
module.hot.check(function(err, updatedModules) {
5+
if(err) {
6+
if(module.hot.status() in {abort:1,fail:1})
7+
window.location.reload();
8+
else
9+
console.warn("Update failed: " + err);
10+
return;
11+
}
12+
13+
if(!updatedModules || updatedModules.length === 0)
14+
return console.log("Update is empty.");
15+
console.log("Updated modules:");
16+
updatedModules.forEach(function(moduleId) {
17+
console.log(" - " + moduleId);
18+
});
19+
});
20+
}
21+
};
22+
}

hot/poll.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
if(module.hot) {
2+
var hotPollInterval = +(__resourceQuery.substr(1)) || (10*60*1000);
3+
setInterval(function() {
4+
if(module.hot.status() === "idle") {
5+
module.hot.check(function(err, updatedModules) {
6+
if(err) {
7+
console.warn("Update failed: " + err);
8+
return;
9+
}
10+
});
11+
}
12+
}, hotPollInterval);
13+
}

lib/Compilation.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function Compilation(compiler) {
2121
this.compiler = compiler;
2222
this.mainTemplate = compiler.mainTemplate;
2323
this.chunkTemplate = compiler.chunkTemplate;
24+
this.hotUpdateChunkTemplate = compiler.hotUpdateChunkTemplate;
2425
this.moduleTemplate = compiler.moduleTemplate;
2526
this.resolvers = compiler.resolvers;
2627
this.inputFileSystem = compiler.inputFileSystem;
@@ -373,6 +374,7 @@ Compilation.prototype.seal = function seal(callback) {
373374

374375
this.sortItems();
375376
this.createChunkAssets();
377+
this.applyPlugins("after-chunk-assets", this.chunks);
376378
this.summarizeDependencies();
377379
this.applyPlugins("record", this, this.records);
378380

0 commit comments

Comments
 (0)