Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove comments from transpiled code using babel-cli

I've been looking around for some .babelrc option to remove comments from the transpiled code, but I haven't had any luck. I tried this:

{   "comments": false } 

as well as

{   "options": {     "comments": false   } } 

and neither works. I'm out of ideas, and I was unable to find any decent documentation anywhere.

like image 630
ffxsam Avatar asked Jul 05 '16 19:07

ffxsam


People also ask

Can the NPM view Babel CLI versions command will display all the versions of Babel?

You can also check the version of babel-cli by finding the babel-cli folder in node_modules and looking at the version property of the package. json that is at the base of that folder. If babel-cli was installed globally via -g flag of npm install , you could check the version by executing command babel --version .

What is Babel choose the correct option?

Babel is a JavaScript compiler Babel is a toolchain that is mainly used to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript in current and older browsers or environments.

What is Babel RC?

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 . babelrc .


1 Answers

Using .babelrc is always recommended:

{   comments: false } 

If using babel-cli, you can use the --no-comments options to achieve the same behaviour.

The latest version of babel-cli includes tests that check for this behaviour to be implemented correctly.


EDIT

It does look like a problem with babel CLI ignoring the comments in .babelrc , a workaround is to use the --no-comments option.

In your package.json

"build": "babel ./index.js --out-dir ./dist/index.js --no-comments" 

To know all the options of babel-cli

./node_modules/.bin/babel -h 

ORIGINAL

Where are you running babel from? Gulp?

Check that you have the .babelrc file in the same or a parent directory of the files beign transpiled

From babeljs.io:

Babel will look for a .babelrc in the current directory of the file being transpiled. If one does not exist, it will travel up the directory tree until it finds either a .babelrc, or a package.json with a "babel": {} hash within.

I have a project with this structure:

  • dist
    • index.js
  • .babelrc
  • index.js
  • gulpfile.js
  • node_modules
    • ...

The relevant task in gulpfile.js

gulp.task('babel', () => {     return gulp.src('index.js')         .pipe(babel({             presets: ['es2015']         }))         .pipe(gulp.dest('./dist/')); }); 

Contents of .babelrc

{     "comments": false } 

The comments are being succesfully removed.

Also check if you're not setting the comments option to true in your gulpfile, for example.

like image 172
Marco Scabbiolo Avatar answered Sep 18 '22 08:09

Marco Scabbiolo