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
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,19 @@ npm install systemjs
## Example Usage

### Loading a System.register module
You can load [System.register](/docs/system-register.md) modules with a script element in your HTML:

```html
<script type="systemjs-module" src="/js/main.js"></script>
<script type="systemjs-module" src="import:name-of-module"></script>
<script src="system.js"></script>
<script>
System.import('/js/main.js');
</script>
```
Using an [`import:` URL](https://github.com/WICG/import-maps/#import-urls) the `src` attribute in HTML can reference [import maps](/docs/import-maps.md).
### Loading with System.import
You can also dynamically load modules at any time with `System.import()`:

```js
System.import('/js/main.js');
```

where `main.js` is a module available in the System.register module format.
Expand Down
2 changes: 1 addition & 1 deletion src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export function resolveIfNotPlainOrUrl (relUrl, parentUrl) {
*
*/

function resolveUrl (relUrl, parentUrl) {
export function resolveUrl (relUrl, parentUrl) {
return resolveIfNotPlainOrUrl(relUrl, parentUrl) || (relUrl.indexOf(':') !== -1 ? relUrl : resolveIfNotPlainOrUrl('./' + relUrl, parentUrl));
}

Expand Down
13 changes: 13 additions & 0 deletions src/features/script-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

import { systemJSPrototype } from '../system-core';
import { hasDocument, baseUrl, resolveUrl } from '../common';

const systemRegister = systemJSPrototype.register;
systemJSPrototype.register = function (deps, declare) {
Expand Down Expand Up @@ -46,3 +47,15 @@ systemJSPrototype.instantiate = function (url, firstParentUrl) {
});
};

if (hasDocument) {
window.addEventListener('DOMContentLoaded', loadScriptModules);
loadScriptModules();
}

function loadScriptModules() {
document.querySelectorAll('script[type=systemjs-module]').forEach(function (script) {
if (script.src) {
System.import(script.src.slice(0, 7) === 'import:' ? script.src.slice(7) : resolveUrl(script.src, baseUrl));
}
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