Search code examples
ecmascript-6babeljstranspiler

babel-maven-plugin not transpiling spread operator to ES5


I am using DOJO toolkit and after upgrading to use the closure compiler, I noticed I needed to transpile to ES5 BEFORE the dojo build util does it's job in order to take advantage of newer ES6+ features.

So I am using babel-maven-plugin to accomplish this.

Everything is working fine with the exception that the ...spread operator is not transpiling.

Do I need to download the @babel/preset-env package as well to set the preset option? or is there an option I am missing?


Solution

  • After further discovery there is no need to download any preset package.

    babel-standalone takes in preset options through its API as defined here and use in the babel-maven-plugin here.

    The preset option is not passed to the Babel API like defined in a .babelrc config file. It is passed in without the preset- prefix. So to get the @babel/preset-env preset option you need to simply pass in env.

    So to round this out, here are the common presets and how you would pass them through to the API:

    • @babel/preset-env --> env
    • @babel/preset-react --> react
    • @babel/preset-flow --> flow
    • @babel/preset-typescript --> typescript

    So in order to use babel-maven-plugin I need to set up the pom.xml as follows:

    <plugin>
        <groupId>com.jarslab.maven</groupId>
        <artifactId>babel-maven-plugin</artifactId>
        <version>1.6</version>
        <executions>
            <execution>
                <id>js-transpile</id>
                <phase>process-resources</phase>
                <goals>
                    <goal>babel</goal>
                </goals>
                <configuration>
                    <verbose>true</verbose>                                  
                    <babelSrc>./js/babel/babel.min.js</babelSrc>
                    <sourceDir>./js</sourceDir>
                    <targetDir>./js</targetDir>
                    <presets>env</presets>
                </configuration>
            </execution>
        </executions>
    </plugin>