From 1e410396a491e253e72bbfcf9a529757a02d781e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=9D=A8=E6=96=87?= Date: Tue, 27 Sep 2022 22:05:05 +0800 Subject: [PATCH] feat: expose addImportMap (#2429) --- docs/api.md | 13 +++++++++++++ docs/import-maps.md | 15 +++++++++++++++ src/features/import-maps.js | 3 +++ src/system-node.js | 3 +++ test/browser/dynamic-import-maps.js | 13 +++++++++++++ test/system-node.mjs | 2 +- 6 files changed, 48 insertions(+), 1 deletion(-) diff --git a/docs/api.md b/docs/api.md index 319ee8cef..1427cbb93 100644 --- a/docs/api.md +++ b/docs/api.md @@ -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", + } +}) +``` diff --git a/docs/import-maps.md b/docs/import-maps.md index 900610586..afbab8daa 100644 --- a/docs/import-maps.md +++ b/docs/import-maps.md @@ -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 `