@awalgawe/esbuild-typescript-paths-plugin
v1.0.1
Published
A plugin for esbuild that resolves TypeScript path aliases during the build process.
Downloads
12
Maintainers
Readme
@awalgawe/esbuild-typescript-paths-plugin
A plugin for esbuild that resolves TypeScript path aliases during the build process.
Installation
You can install the plugin using npm or yarn:
npm install --save-dev @awalgawe/esbuild-typescript-paths-plugin
or
yarn add --dev @awalgawe/esbuild-typescript-paths-plugin
Usage
To use the TypeScript Paths Plugin, you need to import it and add it to your esbuild plugins list. Here's an example of how you can set it up:
import { build } from 'esbuild';
import { tsPathsPlugin } from '@awalgawe/esbuild-typescript-paths-plugin';
build({
entryPoints: ['src/main.ts'],
bundle: true,
outfile: 'dist/bundle.js',
plugins: [tsPathsPlugin()],
tsconfig: './path/to/your/tsconfig.json', // Optional: Path to your TypeScript configuration file.
absWorkingDir: __dirname, // Optional: Absolute working directory for resolving paths.
}).catch(() => process.exit(1));
or without bundling:
import { build } from 'esbuild';
import { tsPathsPlugin } from '@awalgawe/esbuild-typescript-paths-plugin';
build({
entryPoints: ['src/**/*.ts'],
outdir: 'dist',
plugins: [tsPathsPlugin()],
tsconfig: './path/to/your/tsconfig.json', // Optional: Path to your TypeScript configuration file.
absWorkingDir: __dirname, // Optional: Absolute working directory for resolving paths.
}).catch(() => process.exit(1));
Make sure you have a valid tsconfig.json
file with the required path mappings in your project root.
{
"compilerOptions": {
// ...
"baseUrl": ".",
"paths": {
"#app/*": ["src/*"]
}
}
}
How It Works
The plugin works by intercepting the import statements in your TypeScript files and resolving the path aliases defined in your tsconfig.json
file. It uses the TypeScript compiler API to parse and resolve the import paths. If an alias is found, it replaces the import path with the actual resolved path during the build process.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
This plugin is inspired by the need to handle TypeScript path aliases in projects built with esbuild.
Contributing
Contributions are welcome! If you find a bug or want to add a new feature, feel free to open an issue or submit a pull request.