babel-plugin-tsconfig-paths-module-resolver
Combines babel-plugin-module-resolver
and tsconfig-paths
to make a babel plugin that resolves tsconfig paths.
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.
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"
]
}
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,
},
],
],
};