Gathering detailed insights and metrics for babel-plugin-module-resolver-tsconfig
Gathering detailed insights and metrics for babel-plugin-module-resolver-tsconfig
Gathering detailed insights and metrics for babel-plugin-module-resolver-tsconfig
Gathering detailed insights and metrics for babel-plugin-module-resolver-tsconfig
"Write the aliases only in tsconfig. It should be that simple". A helper module that provide easy configuration of babel-plugin-module-resolver plugin for typescript projects. Write only in tsconfig.
npm install babel-plugin-module-resolver-tsconfig
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
6 Stars
63 Commits
1 Watching
1 Branches
1 Contributors
Updated on 24 Dec 2023
HTML (47.13%)
JavaScript (39.8%)
CSS (9.68%)
TypeScript (3.39%)
Cumulative downloads
Total Downloads
Last day
1,600%
17
Compared to previous day
Last week
46.8%
69
Compared to previous week
Last month
-30.8%
382
Compared to previous month
Last year
68.4%
5,917
Compared to previous year
20
You should "Write the aliases in tsconfig only. It should be that simple.".
A helper module that provides easy configuration of babel-plugin-module-resolver plugin for typescript projects. To avoid repeating writing the aliases in both tsconfig and .babelrc.js. (What a blessing)
And that by reading automatically the config from the tsconfig.json
and setting the babel
module resolver plugin` with that. After converting and mapping them to the babel resolver format.
Support extending the config as well.
Say goodbye to repetition and also the frustration of it's not working. Why => forget to add it to the other config. No more of that.
See how simple it becomes.
1const { setModuleResolverPluginForTsConfig } = require('babel-plugin-module-resolver-tsconfig') 2 3module.export = { 4 "presets": [ 5 "@babel/preset-env", 6 "@babel/preset-typescript" 7 ], 8 "plugins": [ 9 "@babel/plugin-proposal-class-properties", 10 setModuleResolverPluginForTsConfig() 11 ] 12}
it's that simple. Instead of manually adding and repeating all of the aliases. No more of it shouldn't be like this
and frustration
.
tsconfig.json
looked up automatically in the same directory as babel.config.json
.
1export declare interface IPluginConfig { 2 [configProp: string]: any; 3 root?: string | string[] | ((tsConfigBaseUrl: string) => string | string[]); 4 alias?: Record<string, string>; 5 tsconfigPath?: string; 6} 7 8declare function setModuleResolverPluginForTsConfig( 9 config?: IPluginConfig 10): [PluginTarget, IPluginConfig]; 11 12declare function readAndParseTsConfig(tsconfigPath: string): ParsedTsconfig;
If you like to know the details. Go to the end of the document.
Here bellow more examples first. For a quick start.
Note: check the toubleshooting section. Tips that can avoid you trouble.
Main rule: in tsconfig paths use ./
for relative paths. ("./src/*"
instead of "src/*"
). Go to the Tips section at the end to see more details.
Most of the time you should go with the no config option already shared above.
Relative path to the calling babel.config.json
Check the API details for how the relative resolution happens.
1setModuleResolverPluginForTsConfig({
2 tsconfigPath: './tsconfig.json'
3})
Absolute path
1setModuleResolverPluginForTsConfig({
2 tsconfigPath: path.resolve(__dirname, './tsconfig.json')
3})
The root value is very important. By default, it is deduced from tsConfig.compilerOptions.baseUrl
.
One value
1setModuleResolverPluginForTsConfig({
2 tsconfigPath: './tsconfig.json',
3 root: '.'
4});
Multiple values
1setModuleResolverPluginForTsConfig({ 2 root: ['.', './another'] 3});
Dynamic function
1setModuleResolverPluginForTsConfig({ 2 root: (tsconfigBaseUrl) => { 3 if (someCheck(tsconfigBaseUrl)) { 4 return '.' // return a string 5 } 6 return ['.', './another'] // or array 7 } 8});
When you provide a root value. It would not override the typescript one. But rather be merged and take precedence (come first)
root: './some' => babel result => root: ['./some', './tsConfgBaseUrlValue']
.
(Aliases are merged and added. Not replaced!)
1const { setModuleResolverPluginForTsConfig } = require('babel-plugin-module-resolver-tsconfig') 2 3module.export = { 4 "presets": [ 5 "@babel/preset-env", 6 "@babel/preset-typescript" 7 ], 8 "plugins": [ 9 "@babel/plugin-proposal-class-properties", 10 setModuleResolverPluginForTsConfig({ 11 alias: { 12 '@extra/*': 'src/extra/*', 13 // ... 14 } 15 }) 16 ] 17}
The above will resolve to:
1[ 2 require.resolve('babel-plugin-module-resolver'), 3 { 4 root: [tsconfig.compilerOptions.baseUrl], 5 alias: { 6 ...<resolved converted mapped typescript paths>, 7 '@extra/*': 'src/extra/\\1', 8 // ... 9 }, 10 } 11]
You can provide the tsconfigPath manually. Relative or absolute.
The Relative works relatively to the calling module file. Here it should be babel.config.js
.
If you are building a module (or package) and wrapping this package to extend it. Then you should use absolute path instead. As the calling module in such a case become your new module. module.parent.filename
was used internally to give you an idea.
If not provided. tsconfig.json
would be looked up automatically. By trying to get the babel config file path from CLI if provided. Otherwise the calling module (babel config file).
If no config file is provided in the babel CLI command. The module calling the method directory would be taken.
The root is a string
, array
, or ((tsConfigBaseUrl: string) => string | string[])
Whatever the option. It will resolve to a string array. That what babel-resolver
expect.
If the root option is not provided. The module automatically gets it from tsconfig.compilerOptions.baseUrl
.
If provided. It will still get the tsconfig baseUrl
. However, it will take precedence. The final resolution will go as follow: [...<userRoot>, <tsconfigBaseUrl>]
.
It will resolve to
1{ 2 ...tsconfigConvertedPaths, 3 ...userAlias 4}
Hugely very important. If it's not working. Check your tsconfig paths first.
First tip. Use relative paths with ./
.
this work ✅:
1"paths": { 2 "*": ["./src/*"] 3}
this doesn't ❌:
1"paths": { 2 "*": ["src/*"] 3}
Ok why doesn't the module handle that for us !! ??? => Well actually because you may refer to a no path name. Like a package. So you have to be precise. And the small rule above is simple and natural.
Any issue. Go to repo.
WAIT ... ⭐🌟✨💫 🌠 YOU HAVEN'T STARED THE REPO YET ⭐🌟✨💫 🌠 TIME TO DO => At least open the repo. And when you test the module and be ❤️ 💖 What a blessing 💖 ❤️ => That's the calling to star moment. And remember to always smile.
Like it => Star => Let know - community picked it. => trust (barrier to use)
Feel free. (Repo => calling)
Issues, Questions, Contribution, Feedback. => Repo.
This plugin was created as a need first and also part of the teaching series at the magician project. The Magician Dev. (The Magician project is about magic for life [in everything], The Magician Dev is about development)
This repo for instance does show
how to setup a package building project, eslint with prettier, commintlint with lefthook, Setting up CI (github actions), tests, and coverage and documentation badges.
And beginner topics:
json5, regex, paths, fs, root directory for npm dev tools, calling parent, js + dts ....
More details about those topics in:
[Related content will be shared soon]
Check:
[Coming soon]
Follow us at:
[Coming soon]
Support The Magician Project:
[Coming later]
No vulnerabilities found.
No security vulnerabilities found.