Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gaetanlegac/ts-alias
Parse module aliases from tsconfig ; Apply / remove them from pathnames ; Generate config for webpack & module-alias.
https://github.com/gaetanlegac/ts-alias
alias aliases tsconfig typescript webpack
Last synced: about 2 months ago
JSON representation
Parse module aliases from tsconfig ; Apply / remove them from pathnames ; Generate config for webpack & module-alias.
- Host: GitHub
- URL: https://github.com/gaetanlegac/ts-alias
- Owner: gaetanlegac
- Created: 2021-10-01T09:08:55.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-02-11T09:52:04.000Z (almost 2 years ago)
- Last Synced: 2024-04-22T19:44:49.689Z (9 months ago)
- Topics: alias, aliases, tsconfig, typescript, webpack
- Language: TypeScript
- Homepage:
- Size: 26.4 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Typescript Aliases Parser
A NodeJS library to parse, process and convert Typescript aliases from tsconfig.json.
[![npm](https://img.shields.io/npm/v/ts-alias)](https://www.npmjs.com/package/ts-alias) [![npm](https://img.shields.io/npm/dw/ts-alias)](https://www.npmjs.com/package/ts-alias)
## Use cases
We all agree that module aliases are useful to maintain a clean and readable code.
But it's frequent that, in large projects, you have to define your same aliases in multiple tools in order to ensure everything runs correctly.
Also, some tools doesn't support defining custom node_module paths for these aliases, which can be locking in some complex projects.
**I wrote this library to avoid the redondancy between my tsconfig.js, Webpack and module-alias configuration in my projects.**
I assumed that the tsconfig.js format is the most universal, and can be reliably converted to aliases list for other tools like [module-alias](https://github.com/ilearnio/module-alias) and the [Webpack](https://github.com/webpack/webpack) package.
By parsing your tsconfig, defining custom rules if necessary, and exporting them for your other aliasing tools, it lightens your maintenance and debugging work.
**Are you using this module for another purpose ? Don't hesitate to create a PR so we can list it here!**
-----------
This project helped you ? Let me know,
⭐ Give it a Star :)------------
## Install
```bash
npm i --save ts-alias
```## Instanciate
The constructor loads the Typescript aliases in the memory.
It can be loaded by two different ways:### From a tsconfig.json file
1. By default, it will search the tsconfig in the current working directory of the process (`process.cwd()`).
```typescript
import Aliases from 'ts-alias';
const aliases = new Aliases();
```2. You can specify in path of the directory containing the tsconfig with the `rootDir` option.
This path can be absolute, or relative (from the process working directory).```typescript
const aliases = new Aliases({
rootDir: './packages/module-containing-a-tsconfig'
});
```3. It's possible to directly provide the tsconfig file path:
```typescript
const aliases = new Aliases({
rootDir: './packages/module-containing-a-tsconfig/tsconfig.json'
});
```An Error will be throwed if rootDir doesn't exists.
### From an AliasList object
If for any reason, you already loaded the tsconfig aliases in memory, you can provide them via the `aliases` option:
```typescript
const list = [{
alias: '@server',
// A list of destination paths
pathnames: ['./src/server'],
// If exact = true, only "@server" will be matched
// If exact = false, "@server" and "@server/*" will be matched
exact: false
}, {
alias: 'react',
// pathnames can also be module names
pathnames: ['preact'],
exact: true
}]const aliases = new Aliases({ aliases: list });
```### Specify the module path
As you saw upper, alias destinations can also be package names.
Thanks to the `modulesDir` option, you can define in which node_modules directory your package should be looked for.```typescript
const aliases = new Aliases({
modulesDir: ['./node_modules', '../../global_node_modules']
});
```**Warning**: This feature is experimental. It could lead to resolution problems in some cases.
**Note**: Only relative paths are supported for now.
### Debug
Are you facing to a resolution problem ? Do you balieve these is a bug in this lib ?
That's not impossible 🤔
To better understands what ts-alias actually does in your case, you can enable advanced logs with the `debug` option:
```typescript
const aliases = new Aliases({
debug: true
});
```## Test if a path can be shorten with an alias
```typescript
public isAliased( filename: string ): boolean;
``````typescript
aliases.isAliased("./src/server/services/user");
// Result: truealiases.isAliased("./src");
// Result: false
```## Shorten / Replace real path by alias
```typescript
public apply( realpath: string, strict?: false ): string;
public apply( realpath: string, strict: true ): string | null;
public apply( realpath: string, strict?: boolean ): string | null;
``````typescript
aliases.apply("./src/server/services/user");
// Result: "@server/services/user"aliases.apply("react");
// Result: "./node_modules/react"
```When the realpath couldn't be replaced with an alias:
* When strict is true, null will be returned.
* Otherwise, the original realpath will be returned, without any alias## Test if a path contains an alias
```typescript
public containsAlias( filename: string ): boolean;
``````typescript
aliases.containsAlias("@server/services/user");
// Result: truealiases.containsAlias("./src/server/services/user");
// Result: false
```## Replace alias by real path
```typescript
public realpath( request: string, strict?: false): string;
public realpath( request: string, strict: true): string | null;
public realpath( request: string, strict?: boolean): string | null;
``````typescript
aliases.realpath("@server/services/user");
// Result: "/home/gaetan/projects/myproject/src/server/services/user"aliases.realpath("./node_modules/react");
// Result: "preact"
```## Convert the aliases list for [Webpack 5](https://github.com/webpack/webpack)
```typescript
const webpackAliases = aliases.forWebpack();module.export = {
...
resolve: {
alias: webpackAliases
}
...
}
```You can pass options in forWebpack():
```typescript
const webpackAliases = aliases.forWebpack({// The path where to resolve node modules
modulesPath: string,// Set to true if you want forWebpack to output the package name (ex: `ts-alias/src/index.ts`) instead of the path to node_modules (ex: `./node_modules/ts-alias/src/index.ts`)
shortenPaths: boolean,
// When set to true, it will return a { aliases, externals } object with the aliases for webpack,
// and a nodeExternals function for webpack
nodeExternals: boolean
});
```## Convert the aliases list for [module-alias](https://github.com/ilearnio/module-alias)
```typescript
import moduleAlias from 'module-alias';moduleAlias.addAliases( aliases.forModuleAlias() );
```-----------
This project helped you ? Let me know,
⭐ Give it a Star :)-----------
## Changelog
### 0.0.7 (16 December 2022)
* Use a TOutputOptions object for passing options in forWebpack()
```typescript
public forWebpack({
modulesPath, shortenPaths, nodeExternals
}: TOutputOptions): TWebpackOutput
```* Added a shortenPaths option for forWebpack()
Set to true if you want forWebpack to output the package name (ex: `ts-alias/src/index.ts`) instead of the path to node_modules (ex: `./node_modules/ts-alias/src/index.ts`)* Store aliases in array instead of indexing by alias.
It allows you to create an exact & prefix version for the same alias.
By example:
```typescript
{
// Exact version: only match "@server"
"@server": ["./server"],
// Prefix version: match any import that starts by "@server/"
"@server/*": ["./server/*"],
}
```## TODO
* Tests (the current version lacks of tests)
* Strict types checking
* Better path resolving (traverse extends)