This is my folder structure:
/
|-- src
| |-- models
| |-- configs
| |-- ...many more
|-- .babelrc
And I'd like to use the @models, @configs syntax
The question is: What regex pattern will work on this scenario (because I have more than just models and configs). I tried:
"^@(.+)": "./src/\\1" and got Unable to resolve module './babel/runtime/helpers/classCallCheck'"@(.+)": "./src/\\1" and got Unable to resolve module @navigators"^\\@(.+)": "./src/\\1" same with 1"\\@(.+)": "./src/\\1" same with 2Something like '^@(.+)': './src/\\1', should work perfectly, we have it tested: https://github.com/tleunen/babel-plugin-module-resolver/blob/master/test/index.test.js#L479
I suggest clearing the Babel cache after chaning your config, it's often a source of the problems.
So in your case it's option 1. Look at the error: Unable to resolve module './babel/runtime/helpers/classCallCheck' - it suggests something wrong with the built-ins configuration (see help for Babel's useBuiltIns option).
@fatfisz can you be more specific about useBuildIns (any link would help)?. This is my .babelrc config:
{
"presets": ["module:metro-react-native-babel-preset"],
"plugins": [
[
"module-resolver",
{
"root": ["./src"],
"alias": {
"^@(.+)": "./src/\\1"
}
}
]
]
}
I can only suggest you ask somewhere else; there are several options listed on Babel's page:
I don't think this is a problem with the babel-plugin-module-resolver, and I don't know the answer from the top of my head, so I won't help you there.
I find elsewhere but cannot found the solution. But anyway, thanks
@fatfisz Hi, I figured out. The problem was the @babel/core and @babel/runtime use the same prefix as I do. So now how can I exclude the node_modules folder or just resolve the src folder only?
If it's only for that simple case, then use a negative lookahead: ^@(?!babel)(.+)$. But if you'll be using other scoped packages, then the best way to handle that would be to use a custom resolver function instead: https://github.com/tleunen/babel-plugin-module-resolver/blob/master/DOCS.md#resolvepath
Inside that function just check if the path matches your desired format and if it doesn't point to an existing file in node_modules - then just resolve the path yourself.
But frankly, since @ is already used for scoped packages, I'd just choose another character, or even drop it altogether. I just don't think it's worth the work you'll need to put into that.
Thanks!
@fatfisz can you be more specific about
useBuildIns(any link would help)?. This is my.babelrcconfig:{ "presets": ["module:metro-react-native-babel-preset"], "plugins": [ [ "module-resolver", { "root": ["./src"], "alias": { "^@(.+)": "./src/\\1" } } ] ] }
It's working fine!
Most helpful comment
If it's only for that simple case, then use a negative lookahead:
^@(?!babel)(.+)$. But if you'll be using other scoped packages, then the best way to handle that would be to use a custom resolver function instead: https://github.com/tleunen/babel-plugin-module-resolver/blob/master/DOCS.md#resolvepathInside that function just check if the path matches your desired format and if it doesn't point to an existing file in
node_modules- then just resolve the path yourself.But frankly, since
@is already used for scoped packages, I'd just choose another character, or even drop it altogether. I just don't think it's worth the work you'll need to put into that.