Skip to content

Commit 6001520

Browse files
committed
docs: document tsconfig paths for aliasing
closes #76
1 parent 7c70503 commit 6001520

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

README.md

+29-9
Original file line numberDiff line numberDiff line change
@@ -141,28 +141,48 @@ When generating type declarations (`.d.ts` files), this also bundles and tree-sh
141141
```
142142

143143
### Aliases
144-
Aliases can be configured in the [import map](https://nodejs.org/api/packages.html#imports), defined in `package.json#imports`.
145144

146-
For native Node.js import mapping, all entries must be prefixed with `#` to indicate an internal [subpath import](https://nodejs.org/api/packages.html#subpath-imports). _Pkgroll_ takes advantage of this behavior to define entries that are _not prefixed_ with `#` as an alias.
145+
#### Import map
147146

148-
Native Node.js import mapping supports conditional imports (eg. resolving different paths for Node.js and browser), but _Pkgroll_ does not.
147+
You can configure aliases using the [import map](https://nodejs.org/api/packages.html#imports) in `package.json#imports`.
149148

150-
> ⚠️ Aliases are not supported in type declaration generation. If you need type support, do not use aliases.
149+
In Node.js, import mappings must start with `#` to indicate an internal [subpath import](https://nodejs.org/api/packages.html#subpath-imports). However, _Pkgroll_ allows defining aliases **without** the `#` prefix.
150+
151+
> [!NOTE]
152+
> While Node.js supports conditional imports (e.g., different paths for Node.js vs. browsers), _Pkgroll_ does not.
153+
154+
Example:
151155

152156
```json5
153157
{
154-
// ...
155-
156158
"imports": {
157-
// Mapping '~utils' to './src/utils.js'
159+
// Alias '~utils' points to './src/utils.js'
158160
"~utils": "./src/utils.js",
159161

160-
// Native Node.js import mapping (can't reference ./src)
161-
"#internal-package": "./vendors/package/index.js",
162+
// Native Node.js subpath import (must use '#', can't reference './src')
163+
"#internal-package": "./vendors/package/index.js"
162164
}
163165
}
164166
```
165167

168+
#### Tsconfig paths
169+
170+
You can also define aliases in `tsconfig.json` using `compilerOptions.paths`:
171+
172+
```json5
173+
{
174+
"compilerOptions": {
175+
"paths": {
176+
"@foo/*": ["./src/foo/*"],
177+
"~bar": ["./src/bar/index.ts"]
178+
}
179+
}
180+
}
181+
```
182+
183+
> [!TIP]
184+
> The community is shifting towards using import maps (`imports`) as the source of truth for aliases because of their wider support across tools like Node.js, TypeScript, Vite, Webpack, and esbuild.
185+
166186
### Target
167187

168188
_Pkgroll_ uses [esbuild](https://esbuild.github.io/) to handle TypeScript and JavaScript transformation and minification.

0 commit comments

Comments
 (0)