webpack/lib/HotModuleReplacement.runtim...

634 lines
17 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2017-05-22 07:59:45 +08:00
/*global $hash$ $requestTimeout$ installedModules $require$ hotDownloadManifest hotDownloadUpdateChunk hotDisposeChunk modules */
module.exports = function() {
var hotApplyOnUpdate = true;
var hotCurrentHash = $hash$; // eslint-disable-line no-unused-vars
2017-05-22 07:59:45 +08:00
var hotRequestTimeout = $requestTimeout$;
var hotCurrentModuleData = {};
var hotCurrentChildModule; // eslint-disable-line no-unused-vars
var hotCurrentParents = []; // eslint-disable-line no-unused-vars
2016-06-16 05:25:40 +08:00
var hotCurrentParentsTemp = []; // eslint-disable-line no-unused-vars
2018-02-25 09:15:37 +08:00
// eslint-disable-next-line no-unused-vars
2018-02-25 09:00:20 +08:00
function hotCreateRequire(moduleId) {
var me = installedModules[moduleId];
2018-02-25 09:00:20 +08:00
if (!me) return $require$;
var fn = function(request) {
2018-02-25 09:00:20 +08:00
if (me.hot.active) {
if (installedModules[request]) {
2018-03-13 17:52:02 +08:00
if (installedModules[request].parents.indexOf(moduleId) === -1)
installedModules[request].parents.push(moduleId);
} else {
hotCurrentParents = [moduleId];
hotCurrentChildModule = request;
}
2018-03-13 17:52:02 +08:00
if (me.children.indexOf(request) === -1) me.children.push(request);
} else {
2018-02-25 09:00:20 +08:00
console.warn(
"[HMR] unexpected require(" +
request +
") from disposed module " +
moduleId
);
hotCurrentParents = [];
}
return $require$(request);
};
2016-10-29 17:11:44 +08:00
var ObjectFactory = function ObjectFactory(name) {
return {
configurable: true,
enumerable: true,
get: function() {
return $require$[name];
},
set: function(value) {
$require$[name] = value;
}
};
2017-01-11 17:51:58 +08:00
};
2018-02-25 09:00:20 +08:00
for (var name in $require$) {
if (
Object.prototype.hasOwnProperty.call($require$, name) &&
name !== "e"
) {
2016-10-29 17:11:44 +08:00
Object.defineProperty(fn, name, ObjectFactory(name));
}
}
fn.e = function(chunkId) {
2018-02-25 09:00:20 +08:00
if (hotStatus === "ready") hotSetStatus("prepare");
hotChunksLoading++;
return $require$.e(chunkId).then(finishChunkLoading, function(err) {
finishChunkLoading();
throw err;
});
2015-07-13 06:20:09 +08:00
function finishChunkLoading() {
hotChunksLoading--;
2018-02-25 09:00:20 +08:00
if (hotStatus === "prepare") {
if (!hotWaitingFilesMap[chunkId]) {
hotEnsureUpdateChunk(chunkId);
}
2018-02-25 09:00:20 +08:00
if (hotChunksLoading === 0 && hotWaitingFiles === 0) {
hotUpdateDownloaded();
}
}
}
};
return fn;
}
2018-02-25 09:15:37 +08:00
// eslint-disable-next-line no-unused-vars
2018-02-25 09:00:20 +08:00
function hotCreateModule(moduleId) {
var hot = {
// private stuff
_acceptedDependencies: {},
_declinedDependencies: {},
_selfAccepted: false,
_selfDeclined: false,
_disposeHandlers: [],
_main: hotCurrentChildModule !== moduleId,
// Module API
active: true,
accept: function(dep, callback) {
2018-02-25 09:00:20 +08:00
if (typeof dep === "undefined") hot._selfAccepted = true;
else if (typeof dep === "function") hot._selfAccepted = dep;
else if (typeof dep === "object")
for (var i = 0; i < dep.length; i++)
hot._acceptedDependencies[dep[i]] = callback || function() {};
2018-02-25 09:00:20 +08:00
else hot._acceptedDependencies[dep] = callback || function() {};
},
decline: function(dep) {
2018-02-25 09:00:20 +08:00
if (typeof dep === "undefined") hot._selfDeclined = true;
else if (typeof dep === "object")
for (var i = 0; i < dep.length; i++)
2015-07-13 06:20:09 +08:00
hot._declinedDependencies[dep[i]] = true;
2018-02-25 09:00:20 +08:00
else hot._declinedDependencies[dep] = true;
},
dispose: function(callback) {
hot._disposeHandlers.push(callback);
},
addDisposeHandler: function(callback) {
hot._disposeHandlers.push(callback);
},
removeDisposeHandler: function(callback) {
var idx = hot._disposeHandlers.indexOf(callback);
2018-02-25 09:00:20 +08:00
if (idx >= 0) hot._disposeHandlers.splice(idx, 1);
},
// Management API
check: hotCheck,
apply: hotApply,
status: function(l) {
2018-02-25 09:00:20 +08:00
if (!l) return hotStatus;
hotStatusHandlers.push(l);
},
addStatusHandler: function(l) {
hotStatusHandlers.push(l);
},
removeStatusHandler: function(l) {
var idx = hotStatusHandlers.indexOf(l);
2018-02-25 09:00:20 +08:00
if (idx >= 0) hotStatusHandlers.splice(idx, 1);
},
//inherit from previous dispose call
data: hotCurrentModuleData[moduleId]
};
hotCurrentChildModule = undefined;
return hot;
}
var hotStatusHandlers = [];
var hotStatus = "idle";
function hotSetStatus(newStatus) {
hotStatus = newStatus;
2018-02-25 09:00:20 +08:00
for (var i = 0; i < hotStatusHandlers.length; i++)
hotStatusHandlers[i].call(null, newStatus);
}
// while downloading
var hotWaitingFiles = 0;
var hotChunksLoading = 0;
var hotWaitingFilesMap = {};
var hotRequestedFilesMap = {};
2016-04-24 09:39:32 +08:00
var hotAvailableFilesMap = {};
var hotDeferred;
// The update info
var hotUpdate, hotUpdateNewHash;
function toModuleId(id) {
2018-02-25 09:00:20 +08:00
var isNumber = +id + "" === id;
2015-07-16 06:39:56 +08:00
return isNumber ? +id : id;
}
function hotCheck(apply) {
2018-02-25 09:00:20 +08:00
if (hotStatus !== "idle")
throw new Error("check() is only allowed in idle status");
hotApplyOnUpdate = apply;
hotSetStatus("check");
2017-05-22 07:59:45 +08:00
return hotDownloadManifest(hotRequestTimeout).then(function(update) {
2018-02-25 09:00:20 +08:00
if (!update) {
hotSetStatus("idle");
return null;
}
hotRequestedFilesMap = {};
hotWaitingFilesMap = {};
hotAvailableFilesMap = update.c;
hotUpdateNewHash = update.h;
hotSetStatus("prepare");
var promise = new Promise(function(resolve, reject) {
hotDeferred = {
resolve: resolve,
reject: reject
};
});
hotUpdate = {};
2015-07-13 06:20:09 +08:00
/*foreachInstalledChunks*/
2018-02-25 09:00:20 +08:00
{
// eslint-disable-line no-lone-blocks
/*globals chunkId */
hotEnsureUpdateChunk(chunkId);
}
2018-02-25 09:00:20 +08:00
if (
hotStatus === "prepare" &&
hotChunksLoading === 0 &&
hotWaitingFiles === 0
) {
hotUpdateDownloaded();
}
return promise;
});
}
2018-02-25 09:15:37 +08:00
// eslint-disable-next-line no-unused-vars
2018-02-25 09:00:20 +08:00
function hotAddUpdateChunk(chunkId, moreModules) {
if (!hotAvailableFilesMap[chunkId] || !hotRequestedFilesMap[chunkId])
return;
hotRequestedFilesMap[chunkId] = false;
2018-02-25 09:00:20 +08:00
for (var moduleId in moreModules) {
if (Object.prototype.hasOwnProperty.call(moreModules, moduleId)) {
hotUpdate[moduleId] = moreModules[moduleId];
}
}
2018-02-25 09:00:20 +08:00
if (--hotWaitingFiles === 0 && hotChunksLoading === 0) {
hotUpdateDownloaded();
}
}
function hotEnsureUpdateChunk(chunkId) {
2018-02-25 09:00:20 +08:00
if (!hotAvailableFilesMap[chunkId]) {
hotWaitingFilesMap[chunkId] = true;
} else {
hotRequestedFilesMap[chunkId] = true;
hotWaitingFiles++;
hotDownloadUpdateChunk(chunkId);
}
}
function hotUpdateDownloaded() {
hotSetStatus("ready");
var deferred = hotDeferred;
hotDeferred = null;
2018-02-25 09:00:20 +08:00
if (!deferred) return;
if (hotApplyOnUpdate) {
// Wrap deferred object in Promise to mark it as a well-handled Promise to
// avoid triggering uncaught exception warning in Chrome.
// See https://bugs.chromium.org/p/chromium/issues/detail?id=465666
2018-02-25 09:00:20 +08:00
Promise.resolve()
.then(function() {
return hotApply(hotApplyOnUpdate);
})
.then(
function(result) {
deferred.resolve(result);
},
function(err) {
deferred.reject(err);
}
);
} else {
var outdatedModules = [];
2018-02-25 09:00:20 +08:00
for (var id in hotUpdate) {
if (Object.prototype.hasOwnProperty.call(hotUpdate, id)) {
outdatedModules.push(toModuleId(id));
}
}
deferred.resolve(outdatedModules);
}
}
function hotApply(options) {
2018-02-25 09:00:20 +08:00
if (hotStatus !== "ready")
throw new Error("apply() is only allowed in ready status");
options = options || {};
2016-01-19 08:52:28 +08:00
var cb;
var i;
var j;
var module;
var moduleId;
2016-07-02 23:30:21 +08:00
function getAffectedStuff(updateModuleId) {
var outdatedModules = [updateModuleId];
var outdatedDependencies = {};
var queue = outdatedModules.slice().map(function(id) {
return {
chain: [id],
id: id
2017-01-11 17:51:58 +08:00
};
});
2018-02-25 09:00:20 +08:00
while (queue.length > 0) {
var queueItem = queue.pop();
var moduleId = queueItem.id;
var chain = queueItem.chain;
2016-01-19 08:52:28 +08:00
module = installedModules[moduleId];
2018-02-25 09:00:20 +08:00
if (!module || module.hot._selfAccepted) continue;
if (module.hot._selfDeclined) {
return {
type: "self-declined",
chain: chain,
moduleId: moduleId
2017-01-11 17:51:58 +08:00
};
}
2018-02-25 09:00:20 +08:00
if (module.hot._main) {
return {
type: "unaccepted",
chain: chain,
moduleId: moduleId
};
}
2018-02-25 09:00:20 +08:00
for (var i = 0; i < module.parents.length; i++) {
var parentId = module.parents[i];
var parent = installedModules[parentId];
2018-02-25 09:00:20 +08:00
if (!parent) continue;
if (parent.hot._declinedDependencies[moduleId]) {
return {
type: "declined",
chain: chain.concat([parentId]),
moduleId: moduleId,
parentId: parentId
2017-01-11 17:51:58 +08:00
};
}
2018-03-13 17:52:02 +08:00
if (outdatedModules.indexOf(parentId) !== -1) continue;
2018-02-25 09:00:20 +08:00
if (parent.hot._acceptedDependencies[moduleId]) {
if (!outdatedDependencies[parentId])
outdatedDependencies[parentId] = [];
addAllToSet(outdatedDependencies[parentId], [moduleId]);
continue;
}
delete outdatedDependencies[parentId];
outdatedModules.push(parentId);
queue.push({
chain: chain.concat([parentId]),
id: parentId
});
}
}
return {
type: "accepted",
2016-07-02 23:30:21 +08:00
moduleId: updateModuleId,
outdatedModules: outdatedModules,
outdatedDependencies: outdatedDependencies
};
}
2015-07-13 06:20:09 +08:00
function addAllToSet(a, b) {
2018-02-25 09:00:20 +08:00
for (var i = 0; i < b.length; i++) {
var item = b[i];
2018-03-13 17:52:02 +08:00
if (a.indexOf(item) === -1) a.push(item);
}
}
// at begin all updates modules are outdated
// the "outdated" status can propagate to parents if they don't accept the children
var outdatedDependencies = {};
var outdatedModules = [];
var appliedUpdate = {};
2016-10-29 17:11:44 +08:00
var warnUnexpectedRequire = function warnUnexpectedRequire() {
2018-02-25 09:00:20 +08:00
console.warn(
"[HMR] unexpected require(" + result.moduleId + ") to disposed module"
);
2016-10-29 17:11:44 +08:00
};
2018-02-25 09:00:20 +08:00
for (var id in hotUpdate) {
if (Object.prototype.hasOwnProperty.call(hotUpdate, id)) {
2016-01-19 08:52:28 +08:00
moduleId = toModuleId(id);
/** @type {TODO} */
var result;
2018-02-25 09:00:20 +08:00
if (hotUpdate[id]) {
result = getAffectedStuff(moduleId);
} else {
result = {
type: "disposed",
moduleId: id
};
}
2018-03-28 14:27:10 +08:00
/** @type {Error|false} */
var abortError = false;
var doApply = false;
var doDispose = false;
var chainInfo = "";
2018-02-25 09:00:20 +08:00
if (result.chain) {
chainInfo = "\nUpdate propagation: " + result.chain.join(" -> ");
}
2018-02-25 09:00:20 +08:00
switch (result.type) {
case "self-declined":
2018-02-25 09:00:20 +08:00
if (options.onDeclined) options.onDeclined(result);
if (!options.ignoreDeclined)
abortError = new Error(
"Aborted because of self decline: " +
result.moduleId +
chainInfo
);
break;
case "declined":
2018-02-25 09:00:20 +08:00
if (options.onDeclined) options.onDeclined(result);
if (!options.ignoreDeclined)
abortError = new Error(
"Aborted because of declined dependency: " +
result.moduleId +
" in " +
result.parentId +
chainInfo
);
break;
case "unaccepted":
2018-02-25 09:00:20 +08:00
if (options.onUnaccepted) options.onUnaccepted(result);
if (!options.ignoreUnaccepted)
abortError = new Error(
"Aborted because " + moduleId + " is not accepted" + chainInfo
);
break;
case "accepted":
2018-02-25 09:00:20 +08:00
if (options.onAccepted) options.onAccepted(result);
doApply = true;
break;
case "disposed":
2018-02-25 09:00:20 +08:00
if (options.onDisposed) options.onDisposed(result);
doDispose = true;
break;
default:
throw new Error("Unexception type " + result.type);
}
2018-02-25 09:00:20 +08:00
if (abortError) {
hotSetStatus("abort");
return Promise.reject(abortError);
}
2018-02-25 09:00:20 +08:00
if (doApply) {
appliedUpdate[moduleId] = hotUpdate[moduleId];
addAllToSet(outdatedModules, result.outdatedModules);
2018-02-25 09:00:20 +08:00
for (moduleId in result.outdatedDependencies) {
if (
Object.prototype.hasOwnProperty.call(
result.outdatedDependencies,
moduleId
)
) {
if (!outdatedDependencies[moduleId])
outdatedDependencies[moduleId] = [];
2018-02-25 09:00:20 +08:00
addAllToSet(
outdatedDependencies[moduleId],
result.outdatedDependencies[moduleId]
);
}
}
}
2018-02-25 09:00:20 +08:00
if (doDispose) {
addAllToSet(outdatedModules, [result.moduleId]);
2016-10-29 17:11:44 +08:00
appliedUpdate[moduleId] = warnUnexpectedRequire;
}
}
}
// Store self accepted outdated modules to require them later by the module system
var outdatedSelfAcceptedModules = [];
2018-02-25 09:00:20 +08:00
for (i = 0; i < outdatedModules.length; i++) {
2016-01-19 08:52:28 +08:00
moduleId = outdatedModules[i];
2018-02-25 09:00:20 +08:00
if (
installedModules[moduleId] &&
installedModules[moduleId].hot._selfAccepted
)
outdatedSelfAcceptedModules.push({
module: moduleId,
errorHandler: installedModules[moduleId].hot._selfAccepted
});
}
// Now in "dispose" phase
hotSetStatus("dispose");
Object.keys(hotAvailableFilesMap).forEach(function(chunkId) {
2018-02-25 09:00:20 +08:00
if (hotAvailableFilesMap[chunkId] === false) {
hotDisposeChunk(chunkId);
}
});
2016-01-19 08:52:28 +08:00
var idx;
var queue = outdatedModules.slice();
2018-02-25 09:00:20 +08:00
while (queue.length > 0) {
2016-01-19 08:52:28 +08:00
moduleId = queue.pop();
module = installedModules[moduleId];
2018-02-25 09:00:20 +08:00
if (!module) continue;
var data = {};
// Call dispose handlers
var disposeHandlers = module.hot._disposeHandlers;
2018-02-25 09:00:20 +08:00
for (j = 0; j < disposeHandlers.length; j++) {
2016-01-19 08:52:28 +08:00
cb = disposeHandlers[j];
cb(data);
}
hotCurrentModuleData[moduleId] = data;
// disable module (this disables requires from this module)
module.hot.active = false;
// remove module from cache
delete installedModules[moduleId];
// when disposing there is no need to call dispose handler
delete outdatedDependencies[moduleId];
// remove "parents" references from all children
2018-02-25 09:00:20 +08:00
for (j = 0; j < module.children.length; j++) {
var child = installedModules[module.children[j]];
2018-02-25 09:00:20 +08:00
if (!child) continue;
2016-01-19 08:52:28 +08:00
idx = child.parents.indexOf(moduleId);
2018-02-25 09:00:20 +08:00
if (idx >= 0) {
child.parents.splice(idx, 1);
}
}
}
// remove outdated dependency from module children
2016-01-19 08:52:28 +08:00
var dependency;
var moduleOutdatedDependencies;
2018-02-25 09:00:20 +08:00
for (moduleId in outdatedDependencies) {
if (
Object.prototype.hasOwnProperty.call(outdatedDependencies, moduleId)
) {
2016-01-19 08:52:28 +08:00
module = installedModules[moduleId];
2018-02-25 09:00:20 +08:00
if (module) {
2016-11-10 15:52:00 +08:00
moduleOutdatedDependencies = outdatedDependencies[moduleId];
2018-02-25 09:00:20 +08:00
for (j = 0; j < moduleOutdatedDependencies.length; j++) {
2016-11-10 15:52:00 +08:00
dependency = moduleOutdatedDependencies[j];
idx = module.children.indexOf(dependency);
2018-02-25 09:00:20 +08:00
if (idx >= 0) module.children.splice(idx, 1);
2016-11-10 15:52:00 +08:00
}
}
}
}
// Not in "apply" phase
hotSetStatus("apply");
hotCurrentHash = hotUpdateNewHash;
// insert new code
2018-02-25 09:00:20 +08:00
for (moduleId in appliedUpdate) {
if (Object.prototype.hasOwnProperty.call(appliedUpdate, moduleId)) {
modules[moduleId] = appliedUpdate[moduleId];
}
}
// call accept handlers
var error = null;
2018-02-25 09:00:20 +08:00
for (moduleId in outdatedDependencies) {
if (
Object.prototype.hasOwnProperty.call(outdatedDependencies, moduleId)
) {
2016-01-19 08:52:28 +08:00
module = installedModules[moduleId];
2018-02-25 09:00:20 +08:00
if (module) {
moduleOutdatedDependencies = outdatedDependencies[moduleId];
var callbacks = [];
2018-02-25 09:00:20 +08:00
for (i = 0; i < moduleOutdatedDependencies.length; i++) {
dependency = moduleOutdatedDependencies[i];
cb = module.hot._acceptedDependencies[dependency];
2018-02-25 09:00:20 +08:00
if (cb) {
2018-03-13 17:52:02 +08:00
if (callbacks.indexOf(cb) !== -1) continue;
callbacks.push(cb);
2016-07-02 23:30:21 +08:00
}
}
2018-02-25 09:00:20 +08:00
for (i = 0; i < callbacks.length; i++) {
cb = callbacks[i];
try {
cb(moduleOutdatedDependencies);
2018-02-25 09:00:20 +08:00
} catch (err) {
if (options.onErrored) {
options.onErrored({
type: "accept-errored",
moduleId: moduleId,
dependencyId: moduleOutdatedDependencies[i],
error: err
});
}
2018-02-25 09:00:20 +08:00
if (!options.ignoreErrored) {
if (!error) error = err;
}
2016-07-02 23:30:21 +08:00
}
}
}
}
}
// Load self accepted modules
2018-02-25 09:00:20 +08:00
for (i = 0; i < outdatedSelfAcceptedModules.length; i++) {
var item = outdatedSelfAcceptedModules[i];
2016-01-19 08:52:28 +08:00
moduleId = item.module;
hotCurrentParents = [moduleId];
try {
$require$(moduleId);
2018-02-25 09:00:20 +08:00
} catch (err) {
if (typeof item.errorHandler === "function") {
try {
item.errorHandler(err);
2018-02-25 09:00:20 +08:00
} catch (err2) {
if (options.onErrored) {
2016-07-02 23:30:21 +08:00
options.onErrored({
type: "self-accept-error-handler-errored",
moduleId: moduleId,
error: err2,
2017-09-04 14:45:07 +08:00
originalError: err
2016-07-02 23:30:21 +08:00
});
}
2018-02-25 09:00:20 +08:00
if (!options.ignoreErrored) {
if (!error) error = err2;
2016-07-02 23:30:21 +08:00
}
2018-02-25 09:00:20 +08:00
if (!error) error = err;
}
2016-07-02 23:30:21 +08:00
} else {
2018-02-25 09:00:20 +08:00
if (options.onErrored) {
2016-07-02 23:30:21 +08:00
options.onErrored({
type: "self-accept-errored",
moduleId: moduleId,
error: err
});
}
2018-02-25 09:00:20 +08:00
if (!options.ignoreErrored) {
if (!error) error = err;
2016-07-02 23:30:21 +08:00
}
}
}
}
// handle errors in accept handlers and self accepted module load
2018-02-25 09:00:20 +08:00
if (error) {
hotSetStatus("fail");
return Promise.reject(error);
}
hotSetStatus("idle");
return new Promise(function(resolve) {
resolve(outdatedModules);
});
}
};