Skip to content

Commit eb02ef1

Browse files
committed
add hot self accept with error handler
1 parent 73650a5 commit eb02ef1

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

lib/HotModuleReplacementPlugin.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ var hotInitCode = Template.getFunctionContent(function() {
232232
accept: function(dep, callback) {
233233
if(typeof dep === "undefined")
234234
hot._selfAccepted = true;
235+
else if(typeof dep === "function")
236+
hot._selfAccepted = dep;
235237
else if(typeof dep === "number")
236238
hot._acceptedDependencies[dep] = callback;
237239
else for(var i = 0; i < dep.length; i++)
@@ -414,7 +416,10 @@ var hotInitCode = Template.getFunctionContent(function() {
414416
for(var i = 0; i < outdatedModules.length; i++) {
415417
var moduleId = outdatedModules[i];
416418
if(installedModules[moduleId] && installedModules[moduleId].hot._selfAccepted)
417-
outdatedSelfAcceptedModules.push(moduleId);
419+
outdatedSelfAcceptedModules.push({
420+
module: moduleId,
421+
errorHandler: installedModules[moduleId].hot._selfAccepted
422+
});
418423
}
419424

420425
// Now in "dispose" phase
@@ -496,16 +501,31 @@ var hotInitCode = Template.getFunctionContent(function() {
496501
}
497502
}
498503
}
499-
if(error) {
500-
hotSetStatus("fail");
501-
return callback(error);
502-
}
503504

504505
// Load self accepted modules
505506
for(var i = 0; i < outdatedSelfAcceptedModules.length; i++) {
506-
var moduleId = outdatedSelfAcceptedModules[i];
507+
var item = outdatedSelfAcceptedModules[i];
508+
var moduleId = item.module;
507509
hotCurrentParent = moduleId;
508-
$require$(moduleId);
510+
try {
511+
$require$(moduleId);
512+
} catch(err) {
513+
if(typeof item.errorHandler === "function") {
514+
try {
515+
item.errorHandler(err);
516+
} catch(err) {
517+
if(!error)
518+
error = err;
519+
}
520+
} else if(!error)
521+
error = err;
522+
}
523+
}
524+
525+
// handle errors in accept handlers and self accepted module load
526+
if(error) {
527+
hotSetStatus("fail");
528+
return callback(error);
509529
}
510530

511531
hotSetStatus("idle");
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
if(module.hot) {
2+
it("should parse a self accept with error handler", function() {
3+
module.hot.accept(function(err) {
4+
5+
});
6+
});
7+
}

0 commit comments

Comments
 (0)