Babel plugin when merging env parameters with parameters other than env

Babylon plug-in documents say that:

  • Plugins run before presets.
  • Ordering a plugin will last primarily.
  • Preset ordering is canceled (first to first).

The babel . babelrc docs say that:

Environment-specific parameters are combined and replaced with non-standard parameters.

Documents do not say exactly how they are combined.

I am using the React react-slingshot template project, and I want to use the conversion of class properties. The project uses babel-preset-stage-1 , which includes babel-plugin-transform-class-properties . Converting class properties should allow me to write code as follows:

 class Example extends Component { static propTypes = { ... } } 

.babelrc in this project:

 { "presets": [ "react", "stage-1" ], "env": { "development": { "presets": [ "latest", "react-hmre" ] }, "production": { "presets": [ ["latest", { "es2015": { "modules": false } }] ], "plugins": [ "transform-react-constant-elements", "transform-react-remove-prop-types" ] }, "test": { "presets": [ "latest" ] } } } 

But when using this as is, I get:

 Module build failed: SyntaxError: Missing class properties transform. 5 | class Example extends Component { > 6 | static propTypes = { | ^ 7 | ... 8 | } 9 | 

There is a conversion of class properties, but apparently the order is not in order.

I got it to compile without any errors by manually merging non-env presets in env presets, but now there are a lot of repetitions:

 { "env": { "development": { "presets": [ "latest", "react-hmre", "stage-1", "react" ] }, "production": { "presets": [ ["latest", { "es2015": { "modules": false } }], "stage-1", "react" ], "plugins": [ "transform-react-constant-elements", "transform-react-remove-prop-types" ] }, "test": { "presets": [ "latest", "stage-1", "react" ] } } } 

Is it possible to specify the order of plugins when using the env and non-env options?

+6
source share
1 answer

.babelrc env inheritance as a class, these are not deep presets / plugins. Therefore, presets and plugins for developers are created for Babel, which can be used in different ways. You can combine presets / plugins to share something .. babelrc env inheritance as shown below:

 Object.assgin({presets:['es2015']},env.development); //it not merge your presets,that I have tested. // if your env.development have a presets:['es2017'] then use es2017 otherwise use es2015 
0
source

Source: https://habr.com/ru/post/1015326/


All Articles