Skip to content

Commit 2245c4a

Browse files
committed
Promise based chunk loading API
don't flag entry module with `0` id anymore use extra argument in chunk instead this allows to require entry points
1 parent 66df346 commit 2245c4a

37 files changed

Lines changed: 293 additions & 266 deletions

hot/dev-server.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,7 @@ if(module.hot) {
99
return lastData.indexOf(__webpack_hash__) >= 0;
1010
};
1111
var check = function check() {
12-
module.hot.check(true, function(err, updatedModules) {
13-
if(err) {
14-
if(module.hot.status() in {abort: 1, fail: 1}) {
15-
console.warn("[HMR] Cannot apply update. Need to do a full reload!");
16-
console.warn("[HMR] " + err.stack || err.message);
17-
window.location.reload();
18-
} else {
19-
console.warn("[HMR] Update failed: " + err.stack || err.message);
20-
}
21-
return;
22-
}
23-
12+
module.hot.check(true).then(function(updatedModules) {
2413
if(!updatedModules) {
2514
console.warn("[HMR] Cannot find update. Need to do a full reload!");
2615
console.warn("[HMR] (Probably because of restarting the webpack-dev-server)");
@@ -38,6 +27,14 @@ if(module.hot) {
3827
console.log("[HMR] App is up to date.");
3928
}
4029

30+
}).catch(function(err) {
31+
if(module.hot.status() in {abort: 1, fail: 1}) {
32+
console.warn("[HMR] Cannot apply update. Need to do a full reload!");
33+
console.warn("[HMR] " + err.stack || err.message);
34+
window.location.reload();
35+
} else {
36+
console.warn("[HMR] Update failed: " + err.stack || err.message);
37+
}
4138
});
4239
};
4340
var addEventListener = window.addEventListener ? function(eventName, listener) {

hot/only-dev-server.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,7 @@ if(module.hot) {
99
return lastData.indexOf(__webpack_hash__) >= 0;
1010
};
1111
var check = function check() {
12-
module.hot.check(function(err, updatedModules) {
13-
if(err) {
14-
if(module.hot.status() in {abort: 1, fail: 1}) {
15-
console.warn("[HMR] Cannot check for update. Need to do a full reload!");
16-
console.warn("[HMR] " + err.stack || err.message);
17-
} else {
18-
console.warn("[HMR] Update check failed: " + err.stack || err.message);
19-
}
20-
return;
21-
}
22-
12+
module.hot.check().then(function(updatedModules) {
2313
if(!updatedModules) {
2414
console.warn("[HMR] Cannot find update. Need to do a full reload!");
2515
console.warn("[HMR] (Probably because of restarting the webpack-dev-server)");
@@ -49,6 +39,13 @@ if(module.hot) {
4939
console.log("[HMR] App is up to date.");
5040
}
5141
});
42+
}).catch(function(err) {
43+
if(module.hot.status() in {abort: 1, fail: 1}) {
44+
console.warn("[HMR] Cannot check for update. Need to do a full reload!");
45+
console.warn("[HMR] " + err.stack || err.message);
46+
} else {
47+
console.warn("[HMR] Update check failed: " + err.stack || err.message);
48+
}
5249
});
5350
};
5451
var addEventListener = window.addEventListener ? function(eventName, listener) {

hot/poll.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,21 @@ if(module.hot) {
77
var hotPollInterval = +(__resourceQuery.substr(1)) || (10 * 60 * 1000);
88
function checkForUpdate(fromUpdate) {
99
if(module.hot.status() === "idle") {
10-
module.hot.check(true, function(err, updatedModules) {
11-
if(err) {
12-
if(module.hot.status() in {abort: 1, fail: 1}) {
13-
console.warn("[HMR] Cannot apply update.");
14-
console.warn("[HMR] " + err.stack || err.message);
15-
console.warn("[HMR] You need to restart the application!");
16-
} else {
17-
console.warn("[HMR] Update failed: " + err.stack || err.message);
18-
}
19-
return;
20-
}
10+
module.hot.check(true).then(function(updatedModules) {
2111
if(!updatedModules) {
2212
if(fromUpdate) console.log("[HMR] Update applied.");
2313
return;
2414
}
2515
require("./log-apply-result")(updatedModules, updatedModules);
2616
checkForUpdate(true);
17+
}).catch(function(err) {
18+
if(module.hot.status() in {abort: 1, fail: 1}) {
19+
console.warn("[HMR] Cannot apply update.");
20+
console.warn("[HMR] " + err.stack || err.message);
21+
console.warn("[HMR] You need to restart the application!");
22+
} else {
23+
console.warn("[HMR] Update failed: " + err.stack || err.message);
24+
}
2725
});
2826
}
2927
}

hot/signal.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,7 @@
55
/*globals __resourceQuery */
66
if(module.hot) {
77
function checkForUpdate(fromUpdate) {
8-
module.hot.check(function(err, updatedModules) {
9-
if(err) {
10-
if(module.hot.status() in {abort: 1, fail: 1}) {
11-
console.warn("[HMR] Cannot apply update.");
12-
console.warn("[HMR] " + err.stack || err.message);
13-
console.warn("[HMR] You need to restart the application!");
14-
} else {
15-
console.warn("[HMR] Update failed: " + err.stack || err.message);
16-
}
17-
return;
18-
}
8+
module.hot.check().then(function(updatedModules) {
199
if(!updatedModules) {
2010
if(fromUpdate)
2111
console.log("[HMR] Update applied.");
@@ -42,6 +32,14 @@ if(module.hot) {
4232

4333
checkForUpdate(true);
4434
});
35+
}).catch(function(err) {
36+
if(module.hot.status() in {abort: 1, fail: 1}) {
37+
console.warn("[HMR] Cannot apply update.");
38+
console.warn("[HMR] " + err.stack || err.message);
39+
console.warn("[HMR] You need to restart the application!");
40+
} else {
41+
console.warn("[HMR] Update failed: " + err.stack || err.message);
42+
}
4543
});
4644
}
4745

lib/APIPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var REPLACEMENTS = {
1717
__webpack_modules__: "__webpack_require__.m", // eslint-disable-line camelcase
1818
__webpack_chunk_load__: "__webpack_require__.e", // eslint-disable-line camelcase
1919
__non_webpack_require__: "require", // eslint-disable-line camelcase
20-
"require.onError": "__webpack_require__.onError" // eslint-disable-line camelcase
20+
"require.onError": "__webpack_require__.oe" // eslint-disable-line camelcase
2121
};
2222
var REPLACEMENT_TYPES = {
2323
__webpack_public_path__: "string", // eslint-disable-line camelcase

lib/ChunkTemplate.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ ChunkTemplate.prototype.render = function(chunk, moduleTemplate, dependencyTempl
1717
var core = this.applyPluginsWaterfall("modules", modules, chunk, moduleTemplate, dependencyTemplates);
1818
var source = this.applyPluginsWaterfall("render", core, chunk, moduleTemplate, dependencyTemplates);
1919
if(chunk.modules.some(function(module) {
20-
return (module.id === 0);
20+
return (module.entry);
2121
})) {
2222
source = this.applyPluginsWaterfall("render-with-entry", source, chunk);
2323
}
@@ -29,4 +29,4 @@ ChunkTemplate.prototype.updateHash = function(hash) {
2929
hash.update("ChunkTemplate");
3030
hash.update("2");
3131
this.applyPlugins("hash", hash);
32-
};
32+
};

lib/Compilation.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function Compilation(compiler) {
4343
this._modules = {};
4444
this.cache = null;
4545
this.records = null;
46-
this.nextFreeModuleId = 1;
46+
this.nextFreeModuleId = 0;
4747
this.nextFreeChunkId = 0;
4848
this.nextFreeModuleIndex = 0;
4949
this.nextFreeModuleIndex2 = 0;
@@ -241,12 +241,6 @@ Compilation.prototype.addModuleDependencies = function(module, dependencies, bai
241241
dependantModule.optional = isOptional();
242242
}
243243

244-
if(dependantModule.id === 0) {
245-
return errorOrWarningAndCallback(
246-
new ModuleNotFoundError(module, new Error("a dependency to an entry point is not allowed"))
247-
);
248-
}
249-
250244
dependencies.forEach(function(dep) {
251245
dep.module = dependantModule;
252246
dependantModule.addReason(module, dep);
@@ -418,17 +412,14 @@ Compilation.prototype.addEntry = function process(context, entry, name, callback
418412
entry.module = module;
419413
this.entries.push(module);
420414
module.issuer = null;
421-
module.id = 0;
415+
module.entry = true;
422416

423417
}.bind(this), function(err, module) {
424418
if(err) {
425419
return callback(err);
426420
}
427421

428422
if(module) {
429-
if(module.reasons.length > 0) {
430-
return callback(new Error("module cannot be added as entry point, because it's already in the bundle"));
431-
}
432423
this.preparedChunks.push({
433424
name: name,
434425
module: module
@@ -491,6 +482,7 @@ Compilation.prototype.seal = function seal(callback) {
491482
chunk.initial = chunk.entry = true;
492483
chunk.addModule(module);
493484
module.addChunk(chunk);
485+
chunk.entryModule = module;
494486
if(typeof module.index !== "number") {
495487
module.index = this.nextFreeModuleIndex++;
496488
}

lib/ContextModule.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ ContextModule.prototype.source = function() {
9595
"\treturn __webpack_require__(webpackContextResolve(req));\n",
9696
"};\n",
9797
"function webpackContextResolve(req) {\n",
98-
"\treturn map[req] || (function() { throw new Error(\"Cannot find module '\" + req + \"'.\") }());\n",
98+
"\tvar id = map[req];\n",
99+
"\tif(!(id + 1)) // check for number\n",
100+
"\t\tthrow new Error(\"Cannot find module '\" + req + \"'.\");\n",
101+
"\treturn id;\n",
99102
"};\n",
100103
"webpackContext.keys = function webpackContextKeys() {\n",
101104
"\treturn Object.keys(map);\n",

lib/HotModuleReplacement.runtime.js

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,25 @@ module.exports = function() {
3232
fn[name] = $require$[name];
3333
}
3434
}
35-
fn.e = function(chunkId, callback) {
35+
fn.e = function(chunkId) {
3636
if(hotStatus === "ready")
3737
hotSetStatus("prepare");
3838
hotChunksLoading++;
39-
$require$.e(chunkId, function() {
40-
try {
41-
callback.call(null, fn);
42-
} finally {
43-
finishChunkLoading();
44-
}
45-
function finishChunkLoading() {
46-
hotChunksLoading--;
47-
if(hotStatus === "prepare") {
48-
if(!hotWaitingFilesMap[chunkId]) {
49-
hotEnsureUpdateChunk(chunkId);
50-
}
51-
if(hotChunksLoading === 0 && hotWaitingFiles === 0) {
52-
hotUpdateDownloaded();
53-
}
39+
return $require$.e(chunkId).then(finishChunkLoading, function(err) {
40+
finishChunkLoading();
41+
throw err;
42+
});
43+
function finishChunkLoading() {
44+
hotChunksLoading--;
45+
if(hotStatus === "prepare") {
46+
if(!hotWaitingFilesMap[chunkId]) {
47+
hotEnsureUpdateChunk(chunkId);
48+
}
49+
if(hotChunksLoading === 0 && hotWaitingFiles === 0) {
50+
hotUpdateDownloaded();
5451
}
5552
}
56-
});
53+
}
5754
};
5855
return fn;
5956
}
@@ -134,27 +131,19 @@ module.exports = function() {
134131
var hotWaitingFilesMap = {};
135132
var hotRequestedFilesMap = {};
136133
var hotAvailibleFilesMap = {};
137-
var hotCallback;
134+
var hotDeferred;
138135

139136
// The update info
140137
var hotUpdate, hotUpdateNewHash;
141138

142-
function hotCheck(apply, callback) {
139+
function hotCheck(apply) {
143140
if(hotStatus !== "idle") throw new Error("check() is only allowed in idle status");
144-
if(typeof apply === "function") {
145-
hotApplyOnUpdate = false;
146-
callback = apply;
147-
} else {
148-
hotApplyOnUpdate = apply;
149-
callback = callback || function(err) { if(err) throw err; };
150-
}
141+
hotApplyOnUpdate = apply;
151142
hotSetStatus("check");
152-
hotDownloadManifest(function(err, update) {
153-
if(err) return callback(err);
143+
return hotDownloadManifest().then(function(update) {
154144
if(!update) {
155145
hotSetStatus("idle");
156-
callback(null, null);
157-
return;
146+
return null;
158147
}
159148

160149
hotRequestedFilesMap = {};
@@ -165,7 +154,12 @@ module.exports = function() {
165154
hotUpdateNewHash = update.h;
166155

167156
hotSetStatus("prepare");
168-
hotCallback = callback;
157+
var promise = new Promise(function(resolve, reject) {
158+
hotDeferred = {
159+
resolve: resolve,
160+
reject: reject
161+
};
162+
});
169163
hotUpdate = {};
170164
/*foreachInstalledChunks*/ { // eslint-disable-line no-lone-blocks
171165
/*globals chunkId */
@@ -174,6 +168,7 @@ module.exports = function() {
174168
if(hotStatus === "prepare" && hotChunksLoading === 0 && hotWaitingFiles === 0) {
175169
hotUpdateDownloaded();
176170
}
171+
return promise;
177172
});
178173
}
179174

@@ -203,33 +198,29 @@ module.exports = function() {
203198

204199
function hotUpdateDownloaded() {
205200
hotSetStatus("ready");
206-
var callback = hotCallback;
207-
hotCallback = null;
208-
if(!callback) return;
201+
var deferred = hotDeferred;
202+
hotDeferred = null;
203+
if(!deferred) return;
209204
if(hotApplyOnUpdate) {
210-
hotApply(hotApplyOnUpdate, callback);
205+
hotApply(hotApplyOnUpdate).then(function(result) {
206+
deferred.resolve(result);
207+
}, function(err) {
208+
deferred.reject(err);
209+
});
211210
} else {
212211
var outdatedModules = [];
213212
for(var id in hotUpdate) {
214213
if(Object.prototype.hasOwnProperty.call(hotUpdate, id)) {
215214
outdatedModules.push(+id);
216215
}
217216
}
218-
callback(null, outdatedModules);
217+
deferred.resolve(outdatedModules);
219218
}
220219
}
221220

222-
function hotApply(options, callback) {
221+
function hotApply(options) {
223222
if(hotStatus !== "ready") throw new Error("apply() is only allowed in ready status");
224-
if(typeof options === "function") {
225-
callback = options;
226-
options = {};
227-
} else if(options && typeof options === "object") {
228-
callback = callback || function(err) { if(err) throw err; };
229-
} else {
230-
options = {};
231-
callback = callback || function(err) { if(err) throw err; };
232-
}
223+
options = options || {};
233224

234225
function getAffectedStuff(module) {
235226
var outdatedModules = [module];
@@ -289,11 +280,11 @@ module.exports = function() {
289280
if(options.ignoreUnaccepted)
290281
continue;
291282
hotSetStatus("abort");
292-
return callback(new Error("Aborted because " + moduleId + " is not accepted"));
283+
return Promise.reject(new Error("Aborted because " + moduleId + " is not accepted"));
293284
}
294285
if(result instanceof Error) {
295286
hotSetStatus("abort");
296-
return callback(result);
287+
return Promise.reject(result);
297288
}
298289
appliedUpdate[moduleId] = hotUpdate[moduleId];
299290
addAllToSet(outdatedModules, result[0]);
@@ -426,10 +417,10 @@ module.exports = function() {
426417
// handle errors in accept handlers and self accepted module load
427418
if(error) {
428419
hotSetStatus("fail");
429-
return callback(error);
420+
return Promise.reject(error);
430421
}
431422

432423
hotSetStatus("idle");
433-
callback(null, outdatedModules);
424+
Promise.resolve(outdatedModules);
434425
}
435-
};
426+
};

0 commit comments

Comments
 (0)