babel / minify

:scissors: An ES6+ aware minifier based on the Babel toolchain (beta)
https://babeljs.io/repl
MIT License
4.39k stars 225 forks source link

Remove comments but keep 'loud' comments #961

Closed Markshall closed 5 years ago

Markshall commented 5 years ago

Describe the bug

I'm setting

{
  "comments": false
}

...in my babel.config.js which obviously, as expected, removes all comments.

What I want to do is keep 'loud' comments, i.e. comments that start with /*! rather than just /* or //. I know Sass/Scss allows this functionality but I've done some digging around and it looks as though Babel doesn't support it.

To Reproduce

Minimal code to reproduce the bug

/*!
here is my loud comment
here is some more stuff to comment
*/

//here is a single-line comment

/*
here is a multi-line comment
*/

Actual Output

Expected Output

/*!
here is my loud comment
here is some more stuff to comment
*/

Configuration

module.exports = {
  "presets": [
    [
      "minify",
      {
        "keepFnName": false
      }
    ]
  ],
  "plugins": [
    ["@babel/plugin-transform-arrow-functions"],
    ["@babel/plugin-transform-destructuring"],
    ["@babel/plugin-transform-template-literals"]
  ],
  "comments": false
};

Possible solution

I'd suggest being able to specify which types of comments shouldn't be stripped out. If no value is passed, then all comments should be removed.

For example, all comments would be removed here:

{
  "comments": []
}

All comments except for loud comments will be removed:

{
  "comments": ['loud']
}

Only single-line comments will be removed:

{
  "comments": ['loud', 'multi-line']
}
xgvargas commented 5 years ago

Why is this closed? There is already a way to accomplish that?

cbdeveloper commented 4 years ago

There is nothing in the docs about this. Is there a way to remove comments?

I get: Invalid option "comments"