Babel File Is Copied Without Being Transformed

Babel file is copied without being transformed

Babel is a transformation framework. Pre-6.x, it enabled certain transformations by default, but with the increased usage of Node versions which natively support many ES6 features, it has become much more important that things be configurable. By default, Babel 6.x does not perform any transformations. You need to tell it what transformations to run:

npm install babel-preset-env

and run

babel --presets env proxy.js --out-file proxified.js

or create a .babelrc file containing

{
"presets": [
"env"
]
}

and run it just like you were before.

env in this case is a preset which basically says to compile all standard ES* behavior to ES5. If you are using Node versions that support some ES6, you may want to consider doing

{
"presets": [
["env", { "targets": { "node": "true" } }],
]
}

to tell the preset to only process things that are not supported by your Node version. You can also include browser versions in your targets if you need browser support.

Babel Outputting Exact Copy of Angular2 File

Unfortunately babel 6 still has some issues, for example failing silently when a plugin is not found.

Install the following plugins:

  • babel-plugin-syntax-decorators
  • babel-plugin-transform-decorators

Include transform-decorators plugin too in the options:

"plugins": [
"syntax-decorators",
"transform-decorators"
]

Most likely you will need a module transformer as well


But all of this is superfluous if your code is eventually TypeScript.

Babel has no effect on my Javascript

This caught me out too. The new version of Babel requires you to install a preset, probably the ES2015 one, and explicitly specify it in the CLI command.

Depending on where Babel is installed (globally/locally) that's where you need to install the preset:

npm install babel-preset-es2015

Then you can run Babel. This, for example, transforms all the files in one folder to another using the preset.

babel [srcFolder] --out-dir [outFolder] --presets es2015


Related Topics



Leave a reply



Submit