Search code examples
svelterollupjs

Svelte bundle.js is large, full of @license comments, even in production mode


>npm run build on a modest sized Svelte project produces a large public/build/bundle.js file. The Javascript code is minimized into a series of one-liners

function(t){return new qr((function(e){...

but in between (or sometimes in the middle of) every line is a large comment block for licenses

 * @license
 * Copyright 2018 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 ... 9 more lines
 * limitations under the License.

The date varies, 2017-2019. There are also a few Microsoft Licenses. There are roughly 70 of these licenses sprinkled in the code, making it bloat up to 800kb.

I haven't messed with rollup config or anything. Here's package.json relevant sections:

"scripts": {
    "build": "rollup -c",
    "dev": "rollup -c -w",
    "start": "sirv public"
  },
  "devDependencies": {
    "@rollup/plugin-commonjs": "^11.0.0",
    "@rollup/plugin-node-resolve": "^7.0.0",
    "rollup": "^1.20.0",
    "rollup-plugin-livereload": "^1.0.0",
    "rollup-plugin-svelte": "^5.0.3",
    "rollup-plugin-terser": "^5.1.2",
    "svelte": "^3.0.0",
    "svelte-mui": "^0.3.3"
  },

I've tried deleting node_modules and redoing npm install to no effect. I"m running on Windows 10 if that matters.


Solution

  • Svelte official template uses terser for minification, when run in production mode (npm run build).

    And apparently, by default, terser preserves licence comments (from their docs):

    --comments [filter]         Preserve copyright comments in the output. By
                                default this works like Google Closure, keeping
                                JSDoc-style comments that contain "@license" or
                                "@preserve". You can optionally pass one of the
                                following arguments to this flag:
                                - "all" to keep all comments
                                - `false` to omit comments in the output
                                - a valid JS RegExp like `/foo/` or `/^!/` to
                                keep only matching comments.
                                Note that currently not *all* comments can be
                                kept when compression is on, because of dead
                                code removal or cascading statements into
                                sequences.
    

    Since the comment example you've posted includes a @license tag, I strongly believe this is the cause.

    You should be able to strip those comments (no idea about the legality of this) by adding the option to the terser plugin in your Rollup config:

            production && terser({ output: { comments: false } })