3

I think I'm going insane, because every search engine throws me only results on how to remove the comments from output file and that's completely opposite of what I searched for

I use all defaults from Webpack 5, where Terser is built-in, so nothing in my webpack.config.js about this. My js rules look like this:

      {
        test: /\.js/,
        exclude: /(node_modules|bower_components)/,
        loader: 'babel-loader',
      },

I have my main.js file that is minified as expected (required comments extracted to main.js.LICENSE.txt), but I also have a simple custom.js file which only has a comment and it should remain there in production:

/*!
 * Keep this comment in production
 */

Few years back /*! or //! did the trick, but now nothing seems to work, even adding @license or @preserve to the comment like the docs suggest

If there's only comment, then production file is empty. If I add a console.log() to have required bit of code, then it does leave the comment, but only in another file custom.js.LICENSE.txt. What config do I need to keep this one file custom.js untouched with just this comment inside? I suppose I could use file-loader instead for this one, but I would like to have it compiled just like any other .js in case I need to add some code there. Just to always leave this one comment inside without extracting it to another file.

Sharak
  • 888
  • 8
  • 17

1 Answers1

0

It's been a while since this question was asked, but here is the answer for those still struggling with this issue.

// webpack.config.file
optimization: {
   minimizer: [new TerserJSPlugin({
            terserOptions: {
                format: {
                    comments: /HELLO/,
                },
            },
        })
}

so terser keeps comments that match /HELLO/ regex like COMMENT_HELLO

and also a simple way mentioned here stackoverflow.com/a/34299912/9477042

Mohammad
  • 29
  • 6