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

Loading type=systemjs-module scripts. Resolves #2002. #2015

Merged
merged 11 commits into from
Sep 2, 2019
14 changes: 14 additions & 0 deletions src/features/script-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,17 @@ systemJSPrototype.instantiate = function (url, firstParentUrl) {
});
};

if (typeof window !== 'undefined') {
guybedford marked this conversation as resolved.
Show resolved Hide resolved
window.addEventListener('load', loadScriptModules);
guybedford marked this conversation as resolved.
Show resolved Hide resolved
}

function loadScriptModules() {
window.removeEventListener('load', loadScriptModules);
guybedford marked this conversation as resolved.
Show resolved Hide resolved
document.querySelectorAll('script[type=systemjs-module]').forEach(function (script) {
if (script.src) {
System.import(script.src.slice(0, 7) === 'import:' ? script.src.slice(7) : script.src);
joeldenning marked this conversation as resolved.
Show resolved Hide resolved
} else {
console.warn('inline systemjs-module scripts are not yet supported');
}
guybedford marked this conversation as resolved.
Show resolved Hide resolved
});
}
12 changes: 12 additions & 0 deletions test/browser/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,16 @@ suite('SystemJS Standard Tests', function() {
assert.ok(err.message.indexOf("'.html' modules not implemented") !== -1);
});
});

test('should load <script type=systemjs-module>', function () {
const resolved = System.resolve('/test/fixtures/browser/systemjs-module-script.js');
assert.ok(System.has(resolved));
assert.equal(System.get(resolved).foo, 'bar');
});

test('should remove import: prefix from <script type=systemjs-module>', function () {
const resolved = System.resolve('/test/fixtures/browser/systemjs-module-script2.js');
assert.ok(System.has(resolved));
assert.equal(System.get(resolved).hello, 'there');
});
});
7 changes: 7 additions & 0 deletions test/fixtures/browser/systemjs-module-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
System.register([], function(_export) {
return {
execute: function () {
_export('foo', 'bar');
}
};
});
7 changes: 7 additions & 0 deletions test/fixtures/browser/systemjs-module-script2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
System.register([], function(_export) {
return {
execute: function () {
_export('hello', 'there');
}
};
});
2 changes: 2 additions & 0 deletions test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

<script type="systemjs-importmap" src="fixtures/browser/importmap.json"></script>
<script src="../dist/system.js"></script>
<script type="systemjs-module" src="/test/fixtures/browser/systemjs-module-script.js"></script>
<script type="systemjs-module" src="import:/test/fixtures/browser/systemjs-module-script2.js"></script>

<script>
mocha.setup('tdd');
Expand Down