How to remove @license comments from source during npm build (will add manually)?

1.2k Views Asked by At

I am developing a Vite-React webapp to be used by me only, and heres my simple build command in package.json: "build": "vite build --emptyOutDir --base=./".

It uses the Google Firebase libraries that contains several submodules wherein each has the exact same @license comment leading to 48 times the same repeated text contributing to 80% of my build since everything else I have gzipped anyway. Now I have no issue with manually, thankfully, and carefully adding all unique licenses wherever appropriate, but at first I wish to remove licenses during the build.

I realize the "@license" is probably the marker the minifier looks for, and I searched around but could not find a way to turn it off. Please advise. And Thanks!

2

There are 2 best solutions below

0
ninhjs.dev On BEST ANSWER

The default minifier now is esbuild (https://vitejs.dev/config/build-options.html#build-minify).

Here is the new config that works:

export default defineConfig({
  // ...
  build: {
    // ...
  },
  esbuild: { legalComments: 'none' },
})

https://esbuild.github.io/api/#legal-comments

3
rschristian On

You need to configure terser through your vite.config.js:

export default defineConfig({
  build: {
    terserOptions: {
      format: {
        comments: false
      }
    }
  }
});

comments (default "some") -- by default it keeps JSDoc-style comments that contain "@license", "@copyright", "@preserve" or start with !, pass true or "all" to preserve all comments, false to omit comments in the output, a regular expression string (e.g. /^!/) or a function.

Terser's Docs