What is tsconfig-paths-webpack-plugin?
The tsconfig-paths-webpack-plugin is a plugin for webpack that allows you to leverage the paths defined in your tsconfig.json file. This means you can alias paths in your TypeScript code and have those aliases correctly resolved by webpack when bundling your application. It simplifies the module resolution process when using TypeScript with webpack.
What are tsconfig-paths-webpack-plugin's main functionalities?
Path Resolution
This feature allows webpack to resolve paths based on the aliases defined in the tsconfig.json file. The code sample shows how to include the TsconfigPathsPlugin in your webpack configuration.
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
module.exports = {
// ... other webpack config settings ...
resolve: {
plugins: [
new TsconfigPathsPlugin({ configFile: './path/to/tsconfig.json' })
]
}
};
Other packages similar to tsconfig-paths-webpack-plugin
babel-plugin-module-resolver
This Babel plugin allows you to add new 'root' directories that contain your modules. It also allows you to map a path to another and to safely ignore certain modules during the Babel process. It's similar to tsconfig-paths-webpack-plugin but is used in the context of Babel rather than webpack.
enhanced-resolve
This is the library that webpack uses under the hood for resolving modules. It can be used directly in Node.js projects to resolve files or directories using webpack's resolution algorithm. It's more low-level compared to tsconfig-paths-webpack-plugin and doesn't directly use the tsconfig.json file.
aliasify
Aliasify is a transform for Browserify that lets you rewrite module calls in your browserify project. This can be used to alias module paths similar to what tsconfig-paths-webpack-plugin does for webpack, but it's specific to Browserify.
tsconfig-paths-webpack-plugin
Use this to load modules whose location is specified in the paths
section of tsconfig.json
when using webpack.
How to install
yarn add --dev tsconfig-paths-webpack-plugin
or
npm install --save-dev tsconfig-paths-webpack-plugin
How to use
In your webpack config add this:
const { TsConfigPathsPlugin } = require('tsconfig-paths-webpack-plugin');
resolve: {
plugins: [
new TsConfigPathsPlugin(/* { configFileName: "path/to/tsconfig.json" } */)
]
}
Prior work
This project uses work done in the awesome-typescript-loader.