Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix auto import dynamic imports during loading #2245

Merged
merged 3 commits into from
Sep 12, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/features/script-load.js
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ systemJSPrototype.createScript = function (url) {
};

// Auto imports -> script tags can be inlined directly for load phase
var lastAutoImportUrl, lastAutoImportDeps;
var lastAutoImportUrl, lastAutoImportDeps, lastAutoImportAutoImport;
var autoImportCandidates = {};
var systemRegister = systemJSPrototype.register;
systemJSPrototype.register = function (deps, declare) {
@@ -42,26 +42,28 @@ systemJSPrototype.register = function (deps, declare) {
if (url) {
lastAutoImportUrl = url;
lastAutoImportDeps = deps;
autoImportCandidates[url] = [deps, declare];
// if this is already a System load, then the instantiate has already begun
// so this re-import has no consequence
this.import(url);
var loader = this;
lastAutoImportAutoImport = setTimeout(function () {
autoImportCandidates[url] = [deps, declare];
loader.import(url);
});
}
}
else {
lastAutoImportDeps = undefined;
autoImportCandidates = null;
}
return systemRegister.call(this, deps, declare);
};

var lastWindowErrorUrl, lastWindowError;
systemJSPrototype.instantiate = function (url, firstParentUrl) {
var loader = this;
var autoImportRegistration = autoImportCandidates[url];
if (autoImportRegistration) {
delete autoImportCandidates[url];
var autoImportRegistration = autoImportCandidates && autoImportCandidates[url];
if (autoImportRegistration)
return autoImportRegistration;
}
var loader = this;
return new Promise(function (resolve, reject) {
var script = systemJSPrototype.createScript(url);
script.addEventListener('error', function () {
@@ -78,7 +80,7 @@ systemJSPrototype.instantiate = function (url, firstParentUrl) {
var register = loader.getRegister();
// Clear any auto import registration for dynamic import scripts during load
if (register && register[0] === lastAutoImportDeps)
delete autoImportCandidates[lastAutoImportUrl];
clearTimeout(lastAutoImportAutoImport);
resolve(register);
}
});