metro: [0.80.11] breaks aliases with babel-plugin-module-resolver on imports (?)
Do you want to request a feature or report a bug?
bug, using react-native 0.75.2, Paper, watchman@2024.08.26.00
Tried:
1.rm -rf node_modules
2.--reset-cache
3. no luck
downgrading to 0.80.10 works as a charm.
What is the current behavior?
throws:
error: Error: Unable to resolve module ./ from /Users/stathis/IdeaProjects/xxxxxx/src/xxxxxx/posts/components/comments/PostComment/components/ContentCommentLines.tsx:
None of these files exist:
* src/flows/posts/components/comments/PostComment/components/index(.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.svg|.native.svg|.svg)
3 | import isEqual from 'react-fast-compare';
4 |
> 5 | import {
| ^
6 | ContentLeftBorderInnerView,
7 | ContentLeftBorderOuterView
8 | } from '@flows/posts/components/comments/PostComment/components';
at ModuleResolver.resolveDependency (/Users/stathis/IdeaProjects/xxxxxx/node_modules/metro/src/node-haste/DependencyGraph/ModuleResolution.js:112:15)
Click for simulator screenshot
Click for babel config
const moduleResolver = [
[
'module-resolver',
{
root: ['./src'],
extensions: [
'.ios.ts',
'.android.ts',
'.ts',
'.ios.tsx',
'.android.tsx',
'.tsx',
'.jsx',
'.js',
'.json'
],
alias: {
'@assets': './src/assets',
'@components': './src/components',
'@contexts': './src/contexts',
'@flows': './src/flows',
'@functions': './src/functions',
'@generated': './src/generated',
'@hooks': './src/hooks',
'@store': './src/store',
'@styles': './src/styles',
'@themes': './src/themes',
'@typings': './src/types',
'@utils': './src/utils'
}
}
]
];
module.exports = {
presets: [
'module:@react-native/babel-preset'
],
plugins:
process.env.NODE_ENV === 'production'
? [
'transform-remove-console',
...moduleResolver,
'macros',
'react-native-reanimated/plugin'
]
: [...moduleResolver, 'macros', 'react-native-reanimated/plugin']
};
Click for tsconfig
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"allowJs": true,
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
"allowUnreachableCode": false,
"experimentalDecorators": true,
"allowUnusedLabels": false,
"noStrictGenericChecks": false,
"esModuleInterop": true,
"importHelpers": true,
"isolatedModules": true,
"jsx": "react-native",
"lib": ["esnext"],
"moduleResolution": "node",
"noEmit": true,
"module": "esnext",
"paths": {
"*": ["src/*"],
"@assets/*": ["src/assets/*"],
"@components/*": ["src/components/*", "components/*"],
"@contexts/*": ["src/contexts/*"],
"@flows/*": ["src/flows/*"],
"@functions/*": ["src/functions/*"],
"@generated/*": ["src/generated/*"],
"@hooks/*": ["src/hooks/*"],
"@store/*": ["src/store/*"],
"@styles/*": ["src/styles/*"],
"@themes/*": ["src/themes/*"],
"@typings/*": ["src/types/*"],
"@utils/*": ["src/utils/*"]
},
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "esnext",
"typeRoots": ["./typings"]
},
"exclude": [
"node_modules",
"./node_modules/",
"**/node_modules",
"ios",
"android",
"babel.config.js",
"metro.config.js"
],
"include": ["src", "__tests__", "typings"]
}
If the current behavior is a bug, please provide the steps to reproduce and a minimal repository on GitHub that we can yarn install and yarn test.
What is the expected behavior?
expect to import packages with aliases
Please provide your exact Metro configuration and mention your Metro, node, yarn/npm version and operating system.
metro config:
const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
const defaultConfig = getDefaultConfig(__dirname);
const { assetExts, sourceExts } = defaultConfig.resolver;
metroConfig = (() => {
return {
transformer: {
babelTransformerPath: require.resolve('react-native-svg-transformer/react-native')
},
resolver: {
assetExts: [...assetExts.filter((ext) => ext !== 'svg'), 'bin', 'lottie'],
sourceExts: [...sourceExts, 'svg']
}
};
})();
module.exports = mergeConfig(defaultConfig, metroConfig);
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 19 (10 by maintainers)
Third-party npm packages are (I hope š¬š¤) not relying on Babel to transform their imports. If they ship an
import * from './'expecting it to resolve to something in the parent directory, I believe thatād already be broken under Node and Webpack. Metro is aligning with others here - please correct me if Iām wrong.A Babel plugin shouldnāt be performing any IO - that makes transform caching impossible - but I think if it transformed to
.without the slash that would be fine. But yep fixing this in the plugin is the right way forward here.Metro resolving ā./ā to āā¦/dirname.jsā was always a bug. We didnāt anticipate it affecting
babel-plugin-module-resolverlike this, but I donāt think we should revert the fix.Yeah, Iām going to close this as a āwonāt fixā - itās a
babel-plugin-module-resolverbug.Thanks for the reports though folks - itās good to know about this.
Thanks for the report - the progress bar is indicative at best (it never really knows how much of the graph is undiscovered) but moving to 100% immediately is a bug - seems like itās racing such that instead of going from 0/1 to 1/n, itās going to 1/1 after the first transform, and then by design it never goes backwards.
hey Rob, sorry for the late reply, yeah youāre right, need to get rid of this bad file naming š, itās confusing.
will try to fix it tomorrow and update the issue.
Ah, I missed that you were using a Babel module resolver.
We donāt support that, but we didnāt intend to break it. Let me try to repro thisā¦
Thanks for the quick report @efstathiosntonas. Could you share how
@flows/postsis set up for you? In particular, is that a barepackage.jsonor does it defineexports? (Iām guessing thereās apackage.jsonatsrc/flows/postsorsrc/flows?Also, what file should this be resolving to? Is it included within the files Metro is claiming donāt exist?