babel-plugin-tsconfig-paths-module-resolver

Combines babel-plugin-module-resolver and tsconfig-paths to make a babel plugin that resolves tsconfig paths.
Use tsconfig-paths in any bundler that supports a custom babel config.
This library is a re-export of babel-plugin-module-resolver pre-configured with tsconfig paths support via the package tsconfig-paths.
It aims to be stable by relying on these already widely-used packages to power the heavy logic:
These dependencies are automatically updated via renovate bot and semantic release.
How is this different from babel-plugin-tsconfig-paths?
Installation
npm install --save-dev babel-plugin-tsconfig-paths-module-resolver
or
yarn add --dev babel-plugin-tsconfig-paths-module-resolver
Specify the plugin in your .babelrc (or equivalent configuration file).
{
"presets": [
"@babel/preset-typescript",
],
"plugins": [
"tsconfig-paths-module-resolver"
]
}
That's it! Paths from your tsconfig.json should now work!
Advanced usage
babel-plugin-tsconfig-paths-module-resolver accepts the same options as babel-plugin-module-resolver.
You can supply those extra options in your babel configuration file like so:
{
"presets": [
"@babel/preset-typescript",
],
"plugins": [
[
"tsconfig-paths-module-resolver",
{
}
]
]
};
resolvePath and createResolvePath
babel-plugin-module-resolver includes a configuration option to allow you to programmatically resolve your imports.
This plugin provides a resolvePath implementation powered by tsconfig-paths. If you'd like to implement your own resolvePath implementation while still utilizing this plugin's default implementation, you can separately import createResolvePath that returns a resolvePath implementation.
const createResolvePath = require('babel-plugin-tsconfig-paths-module-resolver/create-resolve-path');
const defaultResolvePath = createResolvePath();
function customResolvePath(sourceFile, currentFile, opts) {
const result = defaultResolvePath(sourceFile, currentFile, opts);
return result;
}
module.exports = {
presets: [
'@babel/preset-typescript',
],
plugins: [
[
'tsconfig-paths-module-resolver',
{
resolvePath: customResolvePath,
},
],
],
};