Expoでのimport文のaliasはBabelではなくMetroで設定する
from metro.config.json
Expo等で、BabelとMetroを併用している時に、path aliasを指定する方法が2つある
babel.config.jsにbabel-plugin-module-resolverとかを使って指定する
metro.config.jsonに書く
前者だと動かない(?)
まじで勘弁してくれmrsekut.icon
そもそもpath aliasの指定の方法が、transpilerのBabelとbundlerのMetroの2箇所にあるのが分かりづらすぎる
更に、Metroは内部でBabelを使っているので、Babelの設定を一部上書きする設定があったりする
また、一方で、Metroを使えば、babel.config.jsが不要になるわけでもない
presetsとかpluginとかは普通にbabel.config.jsに書く必要がある
path aliasの設定だけ、babel.config.jsに書いてはいけない、という罠がある
Metroのdocsのこの辺を参考にする
https://docs.expo.dev/versions/unversioned/config/metro/#custom-resolving
https://docs.expo.dev/guides/customizing-metro/#aliases
例
code:metro.config.js
const { getDefaultConfig } = require('expo/metro-config');
/** @type {import('expo/metro-config').MetroConfig} */
const config = getDefaultConfig(__dirname);
module.exports = {
...config,
resolver: {
extraNodeModules: {
app: ${__dirname},
},
sourceExts: ...config.resolver.sourceExts, 'ts', 'tsx',
},
watchFolders: [${__dirname}],
};
metro.config.jsのresolver.extraNodeModules
https://metrobundler.dev/docs/resolution/#extranodemodules-string-string
node_modules と nodeModulesPaths による標準的な検索の後に参照されるパッケージ名とディレクトリのマッピング。
metro.config.jsのresolver.sourceExts
https://metrobundler.dev/docs/resolution/#sourceexts-readonlyarraystring
ディスク上に存在しないモジュールパスを解決する際に、順番に試行するファイル拡張子のリスト。 デフォルトは resolver.sourceExts です。
metro.config.jsのwatchFolders
https://metrobundler.dev/docs/configuration/#watchfolders
プロジェクトのソースファイルを格納できる、projectRoot外のディレクトリのリスト。
注: このオプションは、ファイル監視だけに関係するものではありません。 オフラインビルド(CIなど)でも、watchFoldersとprojectRootの組み合わせによって、すべてのファイルがMetroに見えるようになっていなければなりません。