2020-05-06 18:21:13 -04:00
|
|
|
## Import maps
|
|
|
|
|
|
|
|
Deno supports [import maps](https://github.com/WICG/import-maps).
|
|
|
|
|
2020-10-20 08:30:59 -04:00
|
|
|
You can use import maps with the `--import-map=<FILE>` CLI flag.
|
2020-05-06 18:21:13 -04:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2020-06-10 13:43:44 -04:00
|
|
|
**import_map.json**
|
2020-05-06 18:21:13 -04:00
|
|
|
|
2020-06-10 13:43:44 -04:00
|
|
|
```js
|
2020-05-06 18:21:13 -04:00
|
|
|
{
|
|
|
|
"imports": {
|
2020-07-31 05:12:20 -04:00
|
|
|
"fmt/": "https://deno.land/std@$STD_VERSION/fmt/"
|
2020-05-06 18:21:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-06-10 13:43:44 -04:00
|
|
|
**color.ts**
|
2020-05-06 18:21:13 -04:00
|
|
|
|
2020-06-10 13:43:44 -04:00
|
|
|
```ts
|
|
|
|
import { red } from "fmt/colors.ts";
|
2020-05-06 18:21:13 -04:00
|
|
|
|
2020-06-10 13:43:44 -04:00
|
|
|
console.log(red("hello world"));
|
2020-05-06 18:21:13 -04:00
|
|
|
```
|
|
|
|
|
2020-06-10 13:43:44 -04:00
|
|
|
Then:
|
|
|
|
|
2020-05-06 18:21:13 -04:00
|
|
|
```shell
|
2021-03-01 06:41:22 -05:00
|
|
|
$ deno run --import-map=import_map.json color.ts
|
2020-05-06 18:21:13 -04:00
|
|
|
```
|
2020-06-27 07:52:07 -04:00
|
|
|
|
2020-08-15 09:47:14 -04:00
|
|
|
To use starting directory for absolute imports:
|
2020-06-27 07:52:07 -04:00
|
|
|
|
2021-02-16 20:18:39 -05:00
|
|
|
**import_map.json**
|
2020-06-27 07:52:07 -04:00
|
|
|
|
2021-02-16 20:18:39 -05:00
|
|
|
```jsonc
|
2020-06-27 07:52:07 -04:00
|
|
|
{
|
|
|
|
"imports": {
|
|
|
|
"/": "./"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-02-16 20:18:39 -05:00
|
|
|
**main.ts**
|
2020-06-27 07:52:07 -04:00
|
|
|
|
2021-02-16 20:18:39 -05:00
|
|
|
```ts
|
2020-06-27 07:52:07 -04:00
|
|
|
import { MyUtil } from "/util.ts";
|
|
|
|
```
|
|
|
|
|
|
|
|
You may map a different directory: (eg. src)
|
|
|
|
|
2021-02-16 20:18:39 -05:00
|
|
|
**import_map.json**
|
2020-06-27 07:52:07 -04:00
|
|
|
|
2021-02-16 20:18:39 -05:00
|
|
|
```jsonc
|
2020-06-27 07:52:07 -04:00
|
|
|
{
|
|
|
|
"imports": {
|
2020-11-27 13:33:43 -05:00
|
|
|
"/": "./src/"
|
2020-06-27 07:52:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|