Skip to content

Commit

Permalink
expose applyImportMap
Browse files Browse the repository at this point in the history
  • Loading branch information
wenerme committed Sep 25, 2022
1 parent 6376e51 commit d7c88cd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
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.applyImportMap = 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.applyImportMap = 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.applyImportMap({
"imports": {
[moduleId]: "./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.applyImportMap({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 d7c88cd

Please sign in to comment.