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

Handle inline scripts when systemjs itself is inline #2369

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/features/script-load.js
Expand Up @@ -30,24 +30,30 @@ systemJSPrototype.createScript = function (url) {
};

// Auto imports -> script tags can be inlined directly for load phase
var lastAutoImportUrl, lastAutoImportDeps, lastAutoImportTimeout;
var lastAutoImportDeps, lastAutoImportTimeout;
var autoImportCandidates = {};
var systemRegister = systemJSPrototype.register;
var inlineScriptCount = 0;
systemJSPrototype.register = function (deps, declare) {
if (hasDocument && document.readyState === 'loading' && typeof deps !== 'string') {
var scripts = document.querySelectorAll('script[src]');
var lastScript = scripts[scripts.length - 1];
var lastAutoImportUrl;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you move this inside of register? To avoid race conditions with multiple scripts being executed in the same tick of the event loop? I didn't think it would be possible for two inline scripts to execute before a setTimeout tick occurs.

lastAutoImportDeps = deps;
if (lastScript) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the ideal implementation would use the src if it's present and the __inline_script__ if it's not. Something like this:

Suggested change
if (lastScript) {
if (lastScript.src) {

lastAutoImportUrl = lastScript.src;
lastAutoImportDeps = deps;
// if this is already a System load, then the instantiate has already begun
// so this re-import has no consequence
var loader = this;
lastAutoImportTimeout = setTimeout(function () {
autoImportCandidates[lastScript.src] = [deps, declare];
loader.import(lastScript.src);
});
}
else {
inlineScriptCount++
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
inlineScriptCount++
inlineScriptCount++;

lastAutoImportUrl = document.location.href + "__inline_script__" + inlineScriptCount;
}
// if this is already a System load, then the instantiate has already begun
// so this re-import has no consequence
var loader = this;
lastAutoImportTimeout = setTimeout(function () {
autoImportCandidates[lastAutoImportUrl] = [deps, declare];
loader.import(lastAutoImportUrl);
});
}
else {
lastAutoImportDeps = undefined;
Expand Down