hurrtz
hurrtz

Reputation: 2003

Babel: root programmatic options

I seem to not grasp where to put root programmatic options for the babel.

If I have a monorepo and need to tell the different sub packages that they shall look upwards for my babel.config.js then I should put rootMode: "upwards" into the .babelrc of the sub packages, correct? This does not work, because of the resulting error

Error: .rootMode is only allowed in root programmatic options

Somehow I simply can't find any example of where to put/use root programmatic options... Can anyone point me in the right direction?

Upvotes: 13

Views: 5338

Answers (3)

NeoZoom.lua
NeoZoom.lua

Reputation: 2911

Any API-related options are called programmatic options. Take a look at my discussion with the primary maintainer of Babel: https://github.com/babel/babel/discussions/14405.

It's when you specify them directly to Babel (babel.transformSync(code, programmaticOptions) or to the Babel integration you are using (e.g. babel-loader, which can pass them to its internal babel.transform call). In other words, not in presets or config files. [...]

by @nicolo-ribaudo - Babel core team.

Upvotes: 3

Carl G
Carl G

Reputation: 18270

I got this error using my (probably non-standard) monorepo setup where I have top-level subdirectories for each of my packages. No top-level package. When I upgraded to Babel 7, my Jest tests were no longer transforming packages that were yarn linked into the package where I was running Jest.

I added a top-level babel.config.js as part of Babel's monorepo instructions. I had rootMode: "upwards" in these three places:

  • ui-package/webpack.config.js for transforming the app.

  • ui-package/babel-jest.js for the tests, where it appeared like:

    module.exports = require("babel-jest").createTransformer({
      rootMode: "upward",
    })
    

    and was referenced from jest.config.js in that same dir like:

    transform: {
      "^.+\\.jsx?$": "./babel-jest.js",
    },
    
  • And in /babel.config.js, the newly added top-level babel conf file.

Removing it from the last one removed the error.

Upvotes: 0

Ser
Ser

Reputation: 2880

If you are using Webpack, you need to put it there.

module: {
  [..]
  rules: [
    // Transpile ES6 Javascript into ES5 with babel loader
    {
      test: /\.jsx?$/,
      exclude: [/node_modules/, /json/],
      loader: 'babel-loader',
      options: {
        rootMode: 'upward'
      },
    },
    [..]
  ],
  [..]
},

Otherwise I had the same issue than you, I can't put it in the package.json file using the key babel.

Upvotes: 2

Related Questions