Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you choose which ES6 features to transpile with Babel?

For example, is there an option to pass Babel to only transpile arrow functions, or let/const?

My use case is to remove transpiling for certain features as browsers widely support them.

like image 539
bronzehedwick Avatar asked Jun 26 '15 17:06

bronzehedwick


People also ask

What does Babel Transpile to?

Babel is a JavaScript transpiler, meaning it converts a newer version of ECMAScript, such as ES9, to a standard version (ES5).

Is Babel required for ES6?

Absolutely can and do use ES6 W/O babel. All major browsers support the vast majority of features natively (see CanIUse.com), in fact, the only major feature not supported is the import/export of modules. For these, you still have to manually import your modules in the correct order using script tags in the HTML.

Does Babel Transpile to ES5?

Babel is a transpiler because it translates JavaScript ES6 to JavaScript ES5. In contrast, a compiler translates source code from a higher level language to a lower level. An example of compilation would be C++ to machine code or Java to JVM bytecode.

Does Babel Transpile or compile?

Babel is a JavaScript compiler and not a webpack loader. It compiles (transpiles) higher JavaScript code (ES6+) to lower JavaScript code (such as ES5).


1 Answers

Yes, you can pass a whitelist option to specify specific transformations to run, or a blacklist to specific transformations to disable.

They are listed here: http://babeljs.io/docs/advanced/transformers/. See also: http://babeljs.io/docs/usage/options/

Update:

The answer above applies to Babel 5. In Babel 6, all plugins are explicitly enabled either directly or via "presets" which bundle plugins together. You cannot blacklist specific plugins, but you may list only the plugins you want, excluding the ones you do not wish to run.

like image 121
loganfsmyth Avatar answered Oct 12 '22 23:10

loganfsmyth