Skip to content

Commit 0002212

Browse files
committed
Breaking: Refactor ImportMap constructor parameters for improved clarity
1 parent 58b20cb commit 0002212

6 files changed

Lines changed: 9 additions & 9 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ npm i @esm.sh/import-map
1515

1616
## API
1717

18-
### `new ImportMap(baseURL?: string, raw?: ImportMapRaw)`
18+
### `new ImportMap(init?: ImportMapRaw, baseURL?: string)`
1919

2020
Create an import map instance:
2121

@@ -28,7 +28,7 @@ const im = new ImportMap();
2828
You can also initialize from a raw object:
2929

3030
```ts
31-
const im = new ImportMap("https://example.com/app/", {
31+
const im = new ImportMap({
3232
config: { cdn: "https://esm.sh", target: "es2022" },
3333
imports: { react: "https://esm.sh/react@19.2.4/es2022/react.mjs" },
3434
scopes: {

src/importmap.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe("ImportMap", () => {
2626
bad: 123,
2727
},
2828
} as unknown as ImportMapRaw;
29-
const im = new ImportMap("file:///", init);
29+
const im = new ImportMap(init, "file:///");
3030

3131
expect(im.config).toEqual({
3232
cdn: "https://esm.sh",

src/importmap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class ImportMap {
1111
scopes: Record<string, Record<string, string>> = {};
1212
integrity: Record<string, string> = {};
1313

14-
constructor(baseURL?: string | URL, init?: ImportMapRaw) {
14+
constructor(init?: ImportMapRaw, baseURL?: string | URL) {
1515
this.#baseURL = new URL(baseURL ?? globalThis.location?.href ?? "file:///");
1616
if (init) {
1717
this.config = sanitizeStringMap(init.config) as ImportMapConfig;

src/parse.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { isObject, sanitizeScopes, sanitizeStringMap } from "./sanitize.ts";
33

44
/** Parse the import map from a JSON string. */
55
export function parseFromJson(json: string, baseURL?: string): ImportMap {
6-
const im = new ImportMap(baseURL);
6+
const im = new ImportMap({}, baseURL);
77
const v = JSON.parse(json);
88
if (isObject(v)) {
99
const { config, imports, scopes, integrity } = v;
@@ -23,5 +23,5 @@ export function parseFromHtml(html: string, baseURL?: string): ImportMap {
2323
if (scriptEl) {
2424
return parseFromJson(scriptEl.textContent!, baseURL);
2525
}
26-
return new ImportMap(baseURL);
26+
return new ImportMap({}, baseURL);
2727
}

src/resolve.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe("resolve", () => {
6363
});
6464

6565
test("preserves query/hash and normalizes relative mapped urls", () => {
66-
const im = new ImportMap("https://example.com/app/");
66+
const im = new ImportMap({}, "https://example.com/app/");
6767
im.imports = {
6868
local: "./mod.ts",
6969
};
@@ -74,7 +74,7 @@ describe("resolve", () => {
7474
});
7575

7676
test("normalizes root-relative mapped urls against base url", () => {
77-
const im = new ImportMap("https://example.com/app/");
77+
const im = new ImportMap({}, "https://example.com/app/");
7878
im.imports = {
7979
root: "/shared/mod.ts",
8080
};

types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export interface ImportMapRaw {
2525

2626
/** The import map class. */
2727
export class ImportMap {
28-
constructor(baseURL?: string | URL, init?: Record<string, any>);
28+
constructor(init?: Record<string, any>, baseURL?: string | URL);
2929

3030
/** The config for generating the new import url from CDN. */
3131
config: ImportMapConfig;

0 commit comments

Comments
 (0)