Skip to content

Commit

Permalink
feat: expose addImportMap (#2429)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenerme committed Sep 27, 2022
1 parent 6376e51 commit 1e41039
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
13 changes: 13 additions & 0 deletions docs/api.md
Expand Up @@ -139,3 +139,16 @@ for (const [id, ns] of System.entries()) {
console.log(ns); // { exportName: 'value' }
};
```

#### System.addImportMap(map [, base])
Type: `Function`

Allows adding an import map without using the DOM.

```js
System.addImportMap({
"imports": {
"y": "/path/to/y.js",
}
})
```
15 changes: 15 additions & 0 deletions docs/import-maps.md
Expand Up @@ -200,6 +200,21 @@ Any existing mappings are replaced, although in future this may be an error.

Previous versions of the import maps spec had support for multiple import maps in a single web page ([explanation](https://github.com/WICG/import-maps/issues/199)). SystemJS added support for multiple import maps during that time and has decided to keep support for multiple import maps as an experimental feature ([discussion](https://github.com/systemjs/systemjs/issues/2095)). Note that the Chrome implementation of import maps does not yet allow for multiple maps, and use of multiple import maps within SystemJS should be considered experimental and subject to change.

### Dynamically add Import Maps

> Non-standard Extension
The `addImportMap` method is available to dynamically extend additional mappings into the import map at any time:

```js
System.addImportMap({
"imports": {
"y": "/path/to/y.js",
}
})
```

Any existing map entries will be overridden with the new values.
### Handling Import Map Errors

For handling errors when fetching external import maps (specifically for [SystemJS Warning #W4](https://github.com/systemjs/systemjs/blob/master/docs/errors.md#w4)), we can use the `onerror` attribute in the `<script type="systemjs-importmap">` tag.
Expand Down
3 changes: 3 additions & 0 deletions src/features/import-maps.js
Expand Up @@ -23,6 +23,9 @@ if (hasDocument) {
processScripts();
window.addEventListener('DOMContentLoaded', processScripts);
}
systemJSPrototype.addImportMap = function (newMap, mapBase) {
resolveAndComposeImportMap(newMap, mapBase || baseUrl, importMap);
}

function processScripts () {
[].forEach.call(document.querySelectorAll('script'), function (script) {
Expand Down
3 changes: 3 additions & 0 deletions src/system-node.js
Expand Up @@ -23,6 +23,9 @@ systemJSPrototype.resolve = function () {
}
return originalResolve.apply(this, arguments);
};
systemJSPrototype.addImportMap = function (newMap, mapBase){
applyImportMap(this, newMap, mapBase)
}

export function applyImportMap(loader, newMap, mapBase) {
ensureValidSystemLoader(loader);
Expand Down
13 changes: 13 additions & 0 deletions test/browser/dynamic-import-maps.js
Expand Up @@ -67,4 +67,17 @@ suite('Dynamic import maps', function () {
return importAfterImportMap(moduleId);
});
});

test('Loading manually added import map', function () {
const moduleId = 'manual-map-1';
return importNonExistent(moduleId)
.then(function () {
System.addImportMap({
"imports": {
[moduleId]: "./fixtures/browser/esm.js"
}
})
return importAfterImportMap(moduleId);
});
});
});
2 changes: 1 addition & 1 deletion test/system-node.mjs
Expand Up @@ -43,7 +43,7 @@ describe('NodeJS version of SystemJS', () => {
});

it('can load a module from disk without setting base url, before prepareImport is called', async () => {
applyImportMap(System, {imports: {"foo": 'file://' + path.join(process.cwd(), 'test/fixtures/register-modules/export.js')}});
System.addImportMap({imports: {"foo": 'file://' + path.join(process.cwd(), 'test/fixtures/register-modules/export.js')}});
const foo = await System.import('foo');
assert.equal(foo.p, 5);
});
Expand Down

0 comments on commit 1e41039

Please sign in to comment.