Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alias with array of paths #443

Open
JacobDel opened this issue Feb 18, 2023 · 1 comment
Open

Alias with array of paths #443

JacobDel opened this issue Feb 18, 2023 · 1 comment

Comments

@JacobDel
Copy link

JacobDel commented Feb 18, 2023

In #261 it was discussed there could be two paths defined for one alias (if one path fails, there other could be used).
Is it possible that this implementation is broken? #376

used versions:

"babel-plugin-module-resolver": "^5.0.0",
"react-native": "0.70.6",

This does not work for me (babel.config.js):

module.exports = {
  presets: ['module:metro-react-native-babel-preset', '@babel/preset-react'],
  env: {
    production: {
      plugins: ['react-native-paper/babel'],
    },
  },
  plugins: [
    [
      'module-resolver',
      {
        root: [
          './src',
        ],
        alias: {
          //'@brand': './src/Apps/brands/default',
          '@brand': ['./src/Apps/brands/example','./src/Apps/brands/default'],
        },
        extentions: ['.js', '.jsx', '.ts', '.tsx'],
      },
    ],
  ],
};

This does work (difference is the path after '@brand' alias):

module.exports = {
  presets: ['module:metro-react-native-babel-preset', '@babel/preset-react'],
  env: {
    production: {
      plugins: ['react-native-paper/babel'],
    },
  },
  plugins: [
    [
      'module-resolver',
      {
        root: [
          './src',
        ],
        alias: {
          '@brand': './src/Apps/brands/default',
          //'@brand': ['./src/Apps/brands/example','./src/Apps/brands/default'],
        },
        extentions: ['.js', '.jsx', '.ts', '.tsx'],
      },
    ],
  ],
};
@rvasseur31
Copy link
Contributor

I had the same problem this morning, it was because I forget to add the extensions property. Is your file is like somefile.ios.ts, in that case, you should add put this in your extensions property : ["android.tsx", "ios.tsx", ".tsx", "android.ts", "ios.ts", ".ts", ".json"]

He is my full babel.config.js (I use expo and ts) :

const whiteLabel = process.env.WHITE_LABEL;
const brandPath = `./src/brands/${whiteLabel}`;

function generateAlias(name) {
  return [`${brandPath}/${name}`, `./src/${name}`];
}

module.exports = function (api) {
  api.cache(true);
  return {
    presets: ["babel-preset-expo"],
    plugins: [
      [
        "module-resolver",
        {
          root: ["."],
          extensions: ["android.tsx", "ios.tsx", ".tsx", "android.ts", "ios.ts", ".ts", ".json"],
          alias: {
            components: generateAlias("components"),
          },
        },
      ],
    ],
  };
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants