Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Babel: root programmatic options

I seem to absolutely 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 to the right direction?

like image 318
hurrtz Avatar asked Oct 23 '18 20:10

hurrtz


People also ask

What are Babel presets?

@babel/preset-env is a smart preset that allows you to use the latest JavaScript without needing to micromanage which syntax transforms (and optionally, browser polyfills) are needed by your target environment(s). This both makes your life easier and JavaScript bundles smaller!

Do I need a Babelrc file?

babelrc would be useful if you want to run certain transformations / plugins on a subset of files /directories. Maybe you have 3rd party libraries that you don't want to be transformed/changed by babel.

What is a Babelrc?

The . babelrc file is your local configuration for your code in your project. Generally you would put it in the root of your application repo. It will affect all files that Babel processes that are in the same directory or in sibling directories of the .


2 Answers

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.

like image 171
Ser Avatar answered Sep 30 '22 12:09

Ser


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.

like image 45
Rainning Avatar answered Sep 30 '22 14:09

Rainning