Ajay Poshak
Ajay Poshak

Reputation: 726

Can I have multiple babel.config.js in a monorepo?

I have a monorepo with the structure like below

babel.config.js
packages/
|---mobile/
   |----package.json
   |----src/index.js

|---desktop/
   |----package.json
   |----src/index.js

|---server/
   |----package.json
   |----src/index.js

So my babel configuration for mobile and desktop packages are same, whereas configuration for serverpackage is different.

Now, how can I have that configuration done? One solution, that I can think of is that to have a babel.config.js at the root of monorepo which would have configurations for mobile and desktop packages and a separate configuration for server package in a babel.config.js at server package level. I am not sure, can we even have multiple babel.config.js.

Upvotes: 8

Views: 5964

Answers (2)

loganfsmyth
loganfsmyth

Reputation: 161517

Personally, I think using separate files would lead to confusion. Assuming you've set up your system in a way that works already and you're just asking how to specify different configs for different locations, you can use the "overrides" option. For example, your config can do

module.exports = {
  overrides: [{
    test: [
      './desktop',
      './mobile',
    ],

    // put all your normal babel options for these folders here
  }, {
    test: [
      './server',
    ],

    // put all your normal babel options for the server here
  }],
};

Upvotes: 3

Shubham Gupta
Shubham Gupta

Reputation: 2646

Yes, you can achieve what you asked by having multiple config files. It is mentioned as File-relative configuration in babel documentation.

Create a .babelrc or .babelrc.js file in each of your directories (mobile, desktop, server)

For more details look at https://babeljs.io/docs/en/next/config-files

Upvotes: 4

Related Questions