1 | # Import maps
|
2 |
|
3 | Reference implementation playground for import maps proposal.
|
4 |
|
5 | **Copy from WICG's [import-maps implementation](https://github.com/WICG/import-maps/tree/master/reference-implementation).**
|
6 |
|
7 | ## Install
|
8 |
|
9 | Using npm:
|
10 |
|
11 | ```sh
|
12 | npm install import-maps
|
13 | ```
|
14 |
|
15 | or using yarn:
|
16 |
|
17 | ```sh
|
18 | yarn add import-maps
|
19 | ```
|
20 |
|
21 | ## Usage
|
22 |
|
23 | The import-maps files:
|
24 |
|
25 | ```json
|
26 | {
|
27 | "imports": {
|
28 | "a": "/1",
|
29 | "a/": "/2/",
|
30 | "a/b": "/3",
|
31 | "a/b/": "/4/"
|
32 | }
|
33 | }
|
34 | ```
|
35 |
|
36 | ### JavaScript
|
37 |
|
38 | ```js
|
39 | import { resolve, parseFromString } from "import-maps";
|
40 |
|
41 | const importMapBaseURL = "https://example.com/app/index.html";
|
42 | const scriptBaseURL = "https://example.com/js/app.mjs";
|
43 | const rawImportMaps = `{
|
44 | "imports": {
|
45 | "a": "/1",
|
46 | "a/": "/2/",
|
47 | "a/b": "/3",
|
48 | "a/b/": "/4/"
|
49 | }
|
50 | }`;
|
51 |
|
52 | try {
|
53 | const parsedImportMaps = parseFromString(rawImportMaps, importMapBaseURL);
|
54 | const parsedUrl = resolve('a', parsedImportMaps, new URL(scriptBaseURL));
|
55 | console.log(parsedUrl);
|
56 | // URL {
|
57 | // href: 'https://example.com/1',
|
58 | // origin: 'https://example.com',
|
59 | // protocol: 'https:',
|
60 | // username: '',
|
61 | // password: '',
|
62 | // host: 'example.com',
|
63 | // hostname: 'example.com',
|
64 | // port: '',
|
65 | // pathname: '/1',
|
66 | // search: '',
|
67 | // searchParams: URLSearchParams {},
|
68 | // hash: ''
|
69 | // }
|
70 | } catch (e) {
|
71 | console.trace(e);
|
72 | }
|
73 | ```
|
74 |
|
75 | ### TypeScript
|
76 |
|
77 | ```ts
|
78 | import { resolve, parseFromString, ImportMaps } from "import-maps";
|
79 |
|
80 | const rawImportMaps: string = `{
|
81 | "imports": {
|
82 | "a": "/1",
|
83 | "a/": "/2/",
|
84 | "a/b": "/3",
|
85 | "a/b/": "/4/"
|
86 | }
|
87 | }`;
|
88 |
|
89 | const importMapBaseURL: string = "https://example.com/app/index.html";
|
90 | const scriptBaseURL: string = "https://example.com/js/app.mjs";
|
91 |
|
92 | try {
|
93 | const parsedImportMaps: ImportMaps = parseFromString(rawImportMaps, importMapBaseURL);
|
94 | const parsedUrl: URL = resolve('a', parsedImportMaps, new URL(scriptBaseURL));
|
95 | console.log(parsedUrl);
|
96 | // URL {
|
97 | // href: 'https://example.com/1',
|
98 | // origin: 'https://example.com',
|
99 | // protocol: 'https:',
|
100 | // username: '',
|
101 | // password: '',
|
102 | // host: 'example.com',
|
103 | // hostname: 'example.com',
|
104 | // port: '',
|
105 | // pathname: '/1',
|
106 | // search: '',
|
107 | // searchParams: URLSearchParams {},
|
108 | // hash: ''
|
109 | // }
|
110 | } catch (e) {
|
111 | console.trace(e);
|
112 | }
|
113 | ```
|
114 |
|
115 | ## Note ⚠️
|
116 |
|
117 | **The return value of `resolve` is a `URL` instead of a `string`.**
|
118 |
|
119 | ## Scope inheritance
|
120 |
|
121 | Scopes "inherit" from each other in an intentionally-simple manner, merging but overriding as they go. For example, the following import map:
|
122 |
|
123 | ```json
|
124 | {
|
125 | "imports": {
|
126 | "a": "/a-1.mjs",
|
127 | "b": "/b-1.mjs",
|
128 | "c": "/c-1.mjs"
|
129 | },
|
130 | "scopes": {
|
131 | "/scope2/": {
|
132 | "a": "/a-2.mjs"
|
133 | },
|
134 | "/scope2/scope3/": {
|
135 | "b": "/b-3.mjs"
|
136 | }
|
137 | }
|
138 | }
|
139 | ```
|
140 |
|
141 | would give the following resolutions:
|
142 |
|
143 | |Specifier|Referrer |Resulting URL |
|
144 | |---------|-----------------------|--------------|
|
145 | |a |/scope1/foo.mjs |/a-1.mjs |
|
146 | |b |/scope1/foo.mjs |/b-1.mjs |
|
147 | |c |/scope1/foo.mjs |/c-1.mjs |
|
148 | | | | |
|
149 | |a |/scope2/foo.mjs |/a-2.mjs |
|
150 | |b |/scope2/foo.mjs |/b-1.mjs |
|
151 | |c |/scope2/foo.mjs |/c-1.mjs |
|
152 | | | | |
|
153 | |a |/scope2/scope3/foo.mjs |/a-2.mjs |
|
154 | |b |/scope2/scope3/foo.mjs |/b-3.mjs |
|
155 | |c |/scope2/scope3/foo.mjs |/c-1.mjs |
|
156 |
|
157 | ## License
|
158 |
|
159 | All Reports in this Repository are licensed by Contributors under the [W3C Software and Document License](http://www.w3.org/Consortium/Legal/2015/copyright-software-and-document).
|
160 |
|
161 | Contributions to Specifications are made under the [W3C CLA](https://www.w3.org/community/about/agreements/cla/).
|