Re-map import paths
The imports field in ende.json is an import map: any specifier prefix can be
re-mapped to any location. Besides naming packages, it replaces the paths
section of tsconfig.json for path aliases like @/.
Alias a directory
A key ending in / maps a whole prefix. The common convention aliases the
source root:
{
"imports": {
"@/": "./src/"
}
}Imports anywhere in the project can now address files from the root instead of
counting ../ segments:
import { greet } from "@/greet.ts";Alias a single module
A key without a trailing slash maps one bare name to one file, which reads nicely for modules used everywhere:
{
"imports": {
"@/": "./src/",
"logger": "./src/utils/logger.ts"
}
}import { greet } from "@/greet.ts";
import { log } from "logger";
log(greet("import maps"));$ ende run main.ts
[log] Hello, import maps!
Editor support
The Ende language server reads the same file, so completions and
go-to-definition follow the aliases with no extra tsconfig.json configuration.
If you migrate a project that has compilerOptions.paths, move those entries
into imports.
Scoped overrides
The standard import map scopes field also works, remapping a specifier only
for imports that originate under a given path prefix:
{
"imports": {
"logger": "./src/utils/logger.ts"
},
"scopes": {
"./vendor/": {
"logger": "./vendor/legacy_logger.ts"
}
}
}Modules under vendor/ get the legacy logger; everything else gets the default.
Note
The import map redirects specifiers in your own code. To replace a dependency with a local copy, see Overriding dependencies.
For how packages themselves are named and versioned in the import map, see Add and remove dependencies and the modules documentation.