>npm run build在一个中等规模的 Svelte 项目上生成一个大的 public/build/bundle.js 文件。Javascript 代码被最小化为一系列单行代码
function(t){return new qr((function(e){...
但在中间(或有时在中间)每一行都是许可证的大注释块
* @license
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
... 9 more lines
* limitations under the License.
Run Code Online (Sandbox Code Playgroud)
日期不同,2017-2019。还有一些 Microsoft 许可证。代码中散布着大约 70 个这样的许可证,使其膨胀到 800kb。
我没有搞乱汇总配置或任何东西。这是 package.json 相关部分:
"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"
},
Run Code Online (Sandbox Code Playgroud)
我试过删除 node_modules 并重npm install做无效。如果这很重要,我正在 Windows 10 上运行。
Svelte 官方模板在生产模式 ( )下运行时使用terser进行缩小npm run build。
显然,默认情况下,terser保留许可注释(来自他们的文档):
--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.
Run Code Online (Sandbox Code Playgroud)
由于您发布的评论示例包含一个@license标签,我坚信这就是原因。
您应该能够通过将选项添加到 Rollup 配置中的 terser 插件来删除这些评论(不知道这是否合法):
--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.
Run Code Online (Sandbox Code Playgroud)