Skip to content

Commit

Permalink
Add new getOverrideScopes API (#121)
Browse files Browse the repository at this point in the history
* Add new getOverrideScopes API

* Remove manual test
  • Loading branch information
joeldenning authored Oct 7, 2024
1 parent 2247529 commit 1381dc0
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-colts-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"import-map-overrides": minor
---

Add new getOverrideScopes API for inheriting scoped dependencies for overridden modules
40 changes: 40 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,46 @@ const overrideMapWithDisabledOverrides =
*/
```

### getOverrideScopes

Returns a Promise that resolves with an import map containing scopes for overridden modules. The scopes are inherited from the default map.

```html
<!--
When overriding the `a` module, it's possible to inherit the scopes for the not-overridden `a` module by calling the
`getOverrideScopes()` api and injecting an import map with those scopes
-->
<script type="importmap">
{
"imports": {
"a": "https://cdn.example.com/a/index.js",
"rxjs": "https://cdn.example.com/deps/rxjs@7.8.1/index.js"
},
"scopes": {
"https://cdn.example.com/a/": {
"rxjs": "https://cdn.example.com/deps/rxjs@7.6.0/index.js"
}
}
}
</script>
<script src="/import-map-overrides.js"></script>
<script>
window.importMapOverrides.addOverride("a", "http:localhost:8080/a.js");
window.importMapOverrides.getOverrideScopes().then((map) => {
console.log(map);
/*
{
"scopes": {
"http://localhost:8080/": {
"rxjs": "https://cdn.example.com/deps/rxjs@7.6.0/index.js"
}
}
}
*/
});
</script>
```

### addOverride

A function that accepts a string `moduleName` and a string `url` as arguments. This will set up an override **which takes effect
Expand Down
1 change: 0 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export default [
},

rules: {
"es5/no-es6-methods": "error",
"es5/no-es6-static-methods": "error",
"es5/no-binary-and-octal-literals": "error",
"es5/no-classes": "off",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,5 @@
"dependencies": {
"cookie": "^0.6.0"
},
"packageManager": "pnpm@9.11.0"
"packageManager": "pnpm@9.12.1"
}
28 changes: 28 additions & 0 deletions src/api/js-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,34 @@ function init() {
fireChangedEvent();
return hasItem;
},
async getOverrideScopes() {
const scopes = {};
const defaultMap = await imo.getDefaultMap();
const overrideMap = imo.getOverrideMap();
for (let moduleName in overrideMap.imports) {
const defaultUrl = defaultMap.imports[moduleName];
if (defaultUrl) {
const defaultResolution = new URL(defaultUrl, window.location.href)
.href;
const overrideUrl = new URL(
overrideMap.imports[moduleName],
window.location.href,
).href;
const overrideBase =
overrideUrl.slice(0, overrideUrl.lastIndexOf("/")) + "/";
for (let scope in defaultMap.scopes || {}) {
if (defaultResolution.startsWith(scope)) {
scopes[overrideBase] = {
...(scopes[overrideBase] || {}),
...defaultMap.scopes[scope],
};
}
}
}
}

return { scopes };
},
resetOverrides() {
Object.keys(imo.getOverrideMap(true).imports).forEach((moduleName) => {
imo.removeOverride(moduleName);
Expand Down

0 comments on commit 1381dc0

Please sign in to comment.