Skip to content

Commit 49506c8

Browse files
committed
HOT: added update json info file, fixed infinite hash updates, webpack#26
1 parent bf5b14a commit 49506c8

6 files changed

Lines changed: 112 additions & 40 deletions

File tree

‎lib/Compilation.js‎

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function Compilation(compiler) {
3939
this.records = null;
4040
this.nextFreeModuleId = 1;
4141
this.nextFreeChunkId = 1;
42+
this.additionalChunkAssets = [];
4243
this.assets = {};
4344
this.errors = [];
4445
this.warnings = [];
@@ -373,8 +374,11 @@ Compilation.prototype.seal = function seal(callback) {
373374
this.applyPlugins("record-chunks", this.chunks, this.records);
374375

375376
this.sortItems();
377+
this.applyPlugins("before-hash");
378+
this.createHash();
379+
this.applyPlugins("after-hash");
376380
this.createChunkAssets();
377-
this.applyPlugins("after-chunk-assets", this.chunks);
381+
this.applyPlugins("additional-chunk-assets", this.chunks);
378382
this.summarizeDependencies();
379383
this.applyPlugins("record", this, this.records);
380384

@@ -499,11 +503,8 @@ Compilation.prototype.summarizeDependencies = function summarizeDependencies() {
499503
this.contextDependencies = filterDups(this.contextDependencies);
500504
};
501505

502-
Compilation.prototype.createChunkAssets = function createChunkAssets() {
503-
var outputOptions = this.outputOptions || {};
504-
var filename = outputOptions.filename || "bundle.js";
505-
var chunkFilename = outputOptions.chunkFilename || "[id]." + filename.replace(Template.REGEXP_NAME, "");
506-
var namedChunkFilename = outputOptions.namedChunkFilename || null;
506+
Compilation.prototype.createHash = function createHash() {
507+
var outputOptions = this.outputOptions;
507508
var hashFunction = outputOptions.hashFunction;
508509
var hashDigest = outputOptions.hashDigest;
509510
var hashDigestLength = outputOptions.hashDigestLength;
@@ -521,8 +522,27 @@ Compilation.prototype.createChunkAssets = function createChunkAssets() {
521522
hash.update(chunk.hash);
522523
chunk.renderedHash = chunk.hash.substr(0, hashDigestLength);
523524
}
524-
hash = hash.digest(hashDigest);
525-
this.hash = hash.substr(0, hashDigestLength);
525+
this.fullHash = hash.digest(hashDigest);
526+
this.hash = this.fullHash.substr(0, hashDigestLength);
527+
};
528+
529+
Compilation.prototype.modifyHash = function modifyHash(update) {
530+
var outputOptions = this.outputOptions;
531+
var hashFunction = outputOptions.hashFunction;
532+
var hashDigest = outputOptions.hashDigest;
533+
var hashDigestLength = outputOptions.hashDigestLength;
534+
var hash = new (require("crypto").Hash)(hashFunction);
535+
hash.update(this.fullHash);
536+
hash.update(update);
537+
this.fullHash = hash.digest(hashDigest);
538+
this.hash = this.fullHash.substr(0, hashDigestLength);
539+
};
540+
541+
Compilation.prototype.createChunkAssets = function createChunkAssets() {
542+
var outputOptions = this.outputOptions;
543+
var filename = outputOptions.filename || "bundle.js";
544+
var chunkFilename = outputOptions.chunkFilename || "[id]." + filename.replace(Template.REGEXP_NAME, "");
545+
var namedChunkFilename = outputOptions.namedChunkFilename || null;
526546
for(i = 0; i < this.modules.length; i++) {
527547
var module = this.modules[i];
528548
if(module.assets) {
@@ -540,13 +560,13 @@ Compilation.prototype.createChunkAssets = function createChunkAssets() {
540560
var source;
541561
var file;
542562
if(chunk.entry) {
543-
if(this.cache && this.cache["c" + chunk.id + chunk.name] && this.cache["c" + chunk.id + chunk.name].hash == hash) {
563+
if(this.cache && this.cache["c" + chunk.id + chunk.name] && this.cache["c" + chunk.id + chunk.name].hash == this.fullHash) {
544564
source = this.cache["c" + chunk.id + chunk.name].source;
545565
} else {
546566
source = this.mainTemplate.render(this.hash, chunk, this.moduleTemplate, this.dependencyTemplates);
547567
if(this.cache) {
548568
this.cache["c" + chunk.id + chunk.name] = {
549-
hash: hash,
569+
hash: this.fullHash,
550570
source: source
551571
}
552572
}

‎lib/HotModuleReplacementPlugin.js‎

Lines changed: 75 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var Template = require("./Template");
66
var BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
77
var ModuleHotAcceptDependency = require("./dependencies/ModuleHotAcceptDependency");
88
var ModuleHotDeclineDependency = require("./dependencies/ModuleHotDeclineDependency");
9+
var RawSource = require("webpack-core/lib/RawSource");
910

1011
function HotModuleReplacementPlugin(outputOptions) {
1112
this.outputOptions = outputOptions;
@@ -14,6 +15,7 @@ module.exports = HotModuleReplacementPlugin;
1415

1516
HotModuleReplacementPlugin.prototype.apply = function(compiler) {
1617
var hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename || "[id].[hash].hot-update.js";
18+
var hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename || "[hash].hot-update.json";
1719
var hotUpdateFunction = this.outputOptions.hotUpdateFunction || ("webpackHotUpdate" + (this.outputOptions.library || ""));
1820
compiler.plugin("compilation", function(compilation, params) {
1921
var hotUpdateChunkTemplate = compilation.compiler.hotUpdateChunkTemplate;
@@ -47,7 +49,24 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
4749
records.chunkModuleIds[chunk.id] = chunk.modules.map(function(m) { return m.id; });
4850
});
4951
});
50-
compilation.plugin("after-chunk-assets", function() {
52+
compilation.plugin("after-hash", function() {
53+
var records = this.records;
54+
if(!records) return;
55+
if(!records.hash) return;
56+
if(!records.preHash) {
57+
records.preHash = this.hash;
58+
return;
59+
}
60+
if(records.preHash === this.hash) {
61+
if(records.prepreHash)
62+
this.modifyHash(records.prepreHash);
63+
return;
64+
}
65+
records.preHash = this.hash;
66+
records.prepreHash = records.preHash;
67+
this.modifyHash(records.preHash);
68+
});
69+
compilation.plugin("additional-chunk-assets", function() {
5170
var records = this.records;
5271
if(records.hash === this.hash) return;
5372
if(!records.moduleHashs || !records.chunkHashs || !records.chunkModuleIds) return;
@@ -59,6 +78,10 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
5978
hash = hash.digest("hex");
6079
module.hotUpdate = records.moduleHashs[identifier] !== hash;
6180
});
81+
var hotUpdateMainContent = {
82+
h: this.hash,
83+
c: []
84+
};
6285
Object.keys(records.chunkHashs).forEach(function(chunkId) {
6386
chunkId = +chunkId;
6487
var newModules = [];
@@ -77,28 +100,34 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
77100
});
78101
}
79102
newModules = removedModules.concat(newModules);
80-
var source = hotUpdateChunkTemplate.render(chunkId, newModules, this.hash, this.moduleTemplate, this.dependencyTemplates);
81-
var filename = hotUpdateChunkFilename
82-
.replace(Template.REGEXP_HASH, records.hash)
83-
.replace(Template.REGEXP_CHUNKHASH, records.chunkHashs[chunkId])
84-
.replace(Template.REGEXP_ID, chunkId);
85-
this.assets[filename] = source;
86-
if(currentChunk) {
87-
currentChunk.files.push(filename);
88-
this.applyPlugins("chunk-asset", currentChunk, filename);
89-
} else if(this.chunks[0]) {
90-
// TODO: good place for this? (minimizing is only on chunk assets)
91-
this.chunks[0].files.push(filename);
92-
this.applyPlugins("chunk-asset", this.chunks[0], filename);
103+
if(newModules.length > 0) {
104+
var source = hotUpdateChunkTemplate.render(chunkId, newModules, this.hash, this.moduleTemplate, this.dependencyTemplates);
105+
var filename = hotUpdateChunkFilename
106+
.replace(Template.REGEXP_HASH, records.hash)
107+
.replace(Template.REGEXP_ID, chunkId);
108+
this.assets[filename] = source;
109+
hotUpdateMainContent.c.push(chunkId);
110+
if(currentChunk) {
111+
currentChunk.files.push(filename);
112+
this.applyPlugins("chunk-asset", currentChunk, filename);
113+
} else {
114+
this.additionalChunkAssets.push(filename);
115+
this.applyPlugins("chunk-asset", null, filename);
116+
}
93117
}
94118
}, this);
119+
var source = new RawSource(JSON.stringify(hotUpdateMainContent));
120+
var filename = hotUpdateMainFilename.replace(Template.REGEXP_HASH, records.hash);
121+
this.assets[filename] = source;
122+
this.additionalChunkAssets.push(filename);
123+
this.applyPlugins("chunk-asset", null, filename);
95124
});
96125

97126
var mainTemplate = compilation.mainTemplate;
98127
compilation.mainTemplate = Object.create(mainTemplate);
99128

100129
compilation.mainTemplate.updateHash = function(hash) {
101-
hash.update(compilation.records.hash + "");
130+
hash.update("HotMainTemplateDecorator");
102131
mainTemplate.updateHash(hash);
103132
};
104133

@@ -111,9 +140,12 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
111140
var currentHotUpdateChunkFilename = JSON.stringify(hotUpdateChunkFilename)
112141
.replace(Template.REGEXP_HASH, "\" + " + this.renderCurrentHashCode(hash) + " + \"")
113142
.replace(Template.REGEXP_ID, "\" + chunkId + \"");
143+
var currentHotUpdateMainFilename = JSON.stringify(hotUpdateMainFilename)
144+
.replace(Template.REGEXP_HASH, "\" + " + this.renderCurrentHashCode(hash) + " + \"");
114145
buf.push("this[" + JSON.stringify(hotUpdateFunction) + "] = " +
115146
(hotInitCode
116147
.replace(/\$require\$/g, this.requireFn)
148+
.replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename)
117149
.replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename)
118150
.replace(/\$hash\$/g, JSON.stringify(hash))
119151
.replace(/\/\*foreachInstalledChunks\*\//g, chunk.chunks.length > 0 ? "for(var chunkId in installedChunks)" : "var chunkId = 0;")));
@@ -168,14 +200,13 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
168200
};
169201

170202
var hotInitCode = function() {
171-
function webpackHotUpdateCallback(newHash, chunkId, moreModules) {
203+
function webpackHotUpdateCallback(chunkId, moreModules) {
172204
for(var moduleId in moreModules) {
173205
if(moreModules[moduleId])
174206
hotUpdate[moduleId] = moreModules[moduleId];
175207
else if(!hotUpdate[moduleId])
176208
hotUpdate[moduleId] = false;
177209
}
178-
hotUpdateNewHash = newHash;
179210
if(--hotWaitingFiles === 0 && hotChunksLoading === 0) {
180211
hotUpdateDownloaded();
181212
}
@@ -298,6 +329,7 @@ var hotInitCode = function() {
298329
var hotWaitingFiles = 0;
299330
var hotChunksLoading = 0;
300331
var hotWaitingFilesMap = {};
332+
var hotAvailibleFilesMap = {};
301333
var hotCallback;
302334

303335
// The update info
@@ -312,8 +344,7 @@ var hotInitCode = function() {
312344

313345
try {
314346
var request = new XMLHttpRequest();
315-
var chunkId = 0;
316-
request.open("HEAD", modules.c + $hotChunkFilename$, true);
347+
request.open("GET", modules.c + $hotMainFilename$, true);
317348
request.send(null);
318349
} catch(err) {
319350
return callback(err);
@@ -327,26 +358,43 @@ var hotInitCode = function() {
327358

328359
} else {
329360

361+
try {
362+
var update = JSON.parse(request.responseText);
363+
} catch(e) {
364+
hotSetStatus("idle");
365+
callback(null, null);
366+
return;
367+
}
368+
369+
hotAvailibleFilesMap = {};
330370
hotWaitingFilesMap = {};
371+
for(var i = 0; i < update.c.length; i++)
372+
hotAvailibleFilesMap[update.c[i]] = true;
373+
hotUpdateNewHash = update.h;
374+
331375
hotSetStatus("prepare");
332376
hotCallback = callback;
333377
hotUpdate = {};
334378
/*foreachInstalledChunks*/ {
335379
hotDownloadUpdateChunk(chunkId);
336380
}
337-
381+
if(hotChunksLoading === 0 && hotWaitingFiles === 0) {
382+
hotUpdateDownloaded();
383+
}
338384
}
339385
};
340386
}
341387

342388
function hotDownloadUpdateChunk(chunkId) {
343-
hotWaitingFiles++;
344-
var head = document.getElementsByTagName('head')[0];
345-
var script = document.createElement('script');
346-
script.type = 'text/javascript';
347-
script.charset = 'utf-8';
348-
script.src = modules.c + $hotChunkFilename$;
349-
head.appendChild(script);
389+
if(hotAvailibleFilesMap[chunkId]) {
390+
hotWaitingFiles++;
391+
var head = document.getElementsByTagName('head')[0];
392+
var script = document.createElement('script');
393+
script.type = 'text/javascript';
394+
script.charset = 'utf-8';
395+
script.src = modules.c + $hotChunkFilename$;
396+
head.appendChild(script);
397+
}
350398
hotWaitingFilesMap[chunkId] = true;
351399
}
352400

‎lib/JsonpHotUpdateChunkTemplate.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ JsonpHotUpdateChunkTemplate.prototype = Object.create(HotUpdateChunkTemplate.pro
1313
JsonpHotUpdateChunkTemplate.prototype.renderHeader = function(id, modules, hash) {
1414
var buf = HotUpdateChunkTemplate.prototype.renderHeader.call(this, id, modules, hash);
1515
var jsonpFunction = this.outputOptions.hotUpdateFunction || ("webpackHotUpdate" + (this.outputOptions.library || ""));
16-
buf.unshift(jsonpFunction + "(" + JSON.stringify(hash) + "," + JSON.stringify(id) + ",");
16+
buf.unshift(jsonpFunction + "(" + JSON.stringify(id) + ",");
1717
return buf;
1818
};
1919

‎lib/optimize/UglifyJsPlugin.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ UglifyJsPlugin.prototype.apply = function(compiler) {
2525
files.push(file);
2626
});
2727
});
28+
compilation.additionalChunkAssets.forEach(function(file) {
29+
files.push(file);
30+
});
2831
files.forEach(function(file) {
2932
try {
3033
var input = compilation.assets[file].source();

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack",
3-
"version": "0.11.0-beta5",
3+
"version": "0.11.0-beta6",
44
"author": "Tobias Koppers @sokra",
55
"description": "Packs CommonJs/AMD/Labeled Modules for the browser. Allows to split your codebase into multiple bundles, which can be loaded on demand. Support loaders to preprocess files, i.e. json, jade, coffee, css, less, ... and your custom stuff.",
66
"dependencies": {

‎test/browsertest/middlewareTest.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ app.configure(function() {
3434
output: {
3535
publicPath: "http://localhost:8080/js/",
3636
path: "/",
37-
filename: "web.js"
37+
filename: "web.js",
38+
chunkFilename: "[chunkhash].chunk.js"
3839
}
3940
}), {
4041
lazy: false,

0 commit comments

Comments
 (0)