2020-05-07 00:21:13 +02:00
|
|
|
## Import maps
|
|
|
|
|
|
|
|
Deno supports [import maps](https://github.com/WICG/import-maps).
|
|
|
|
|
2020-10-20 13:30:59 +01:00
|
|
|
You can use import maps with the `--import-map=<FILE>` CLI flag.
|
2020-05-07 00:21:13 +02:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2020-06-10 18:43:44 +01:00
|
|
|
**import_map.json**
|
2020-05-07 00:21:13 +02:00
|
|
|
|
2020-06-10 18:43:44 +01:00
|
|
|
```js
|
2020-05-07 00:21:13 +02:00
|
|
|
{
|
|
|
|
"imports": {
|
2020-07-31 11:12:20 +02:00
|
|
|
"fmt/": "https://deno.land/std@$STD_VERSION/fmt/"
|
2020-05-07 00:21:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-06-10 18:43:44 +01:00
|
|
|
**color.ts**
|
2020-05-07 00:21:13 +02:00
|
|
|
|
2020-06-10 18:43:44 +01:00
|
|
|
```ts
|
|
|
|
import { red } from "fmt/colors.ts";
|
2020-05-07 00:21:13 +02:00
|
|
|
|
2020-06-10 18:43:44 +01:00
|
|
|
console.log(red("hello world"));
|
2020-05-07 00:21:13 +02:00
|
|
|
```
|
|
|
|
|
2020-06-10 18:43:44 +01:00
|
|
|
Then:
|
|
|
|
|
2020-05-07 00:21:13 +02:00
|
|
|
```shell
|
2021-03-01 12:41:22 +01:00
|
|
|
$ deno run --import-map=import_map.json color.ts
|
2020-05-07 00:21:13 +02:00
|
|
|
```
|
2020-06-27 20:52:07 +09:00
|
|
|
|
2021-03-22 20:13:50 +00:00
|
|
|
To use your project root for absolute imports:
|
2020-06-27 20:52:07 +09:00
|
|
|
|
2021-02-17 10:18:39 +09:00
|
|
|
**import_map.json**
|
2020-06-27 20:52:07 +09:00
|
|
|
|
2021-02-17 10:18:39 +09:00
|
|
|
```jsonc
|
2020-06-27 20:52:07 +09:00
|
|
|
{
|
|
|
|
"imports": {
|
2021-03-22 20:13:50 +00:00
|
|
|
"/": "./",
|
|
|
|
"./": "./"
|
2020-06-27 20:52:07 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-02-17 10:18:39 +09:00
|
|
|
**main.ts**
|
2020-06-27 20:52:07 +09:00
|
|
|
|
2021-02-17 10:18:39 +09:00
|
|
|
```ts
|
2020-06-27 20:52:07 +09:00
|
|
|
import { MyUtil } from "/util.ts";
|
|
|
|
```
|
|
|
|
|
2021-03-22 20:13:50 +00:00
|
|
|
This causes import specifiers starting with `/` to be resolved relative to the
|
|
|
|
import map's URL or file path.
|