[英]How to remove comments when building TypeScript into JavaScripts using rollup
我正在使用汇总来构建我的 TypeScript 源代码。 我只想删除注释,没有任何缩小,以便在调试时热更新代码。
我试过rollup-plugin-terser
,它可以删除注释,但它也会以某种方式缩小我的代码,我不能完全禁用缩小。
我怎样才能做到这一点? 谢谢!
就像@jujubes 在评论中回答的那样, rollup-plugin-cleanup将完成任务。 我想扩大一点。
三件事:
ts
添加到扩展列表,如extensions: ["js", "ts"]
— 否则不会处理源,即使编译步骤 typescript typescript()
在它之前 — 我最初来到这里是为了研究为什么rollup-plugin-cleanup
获胜不能处理 TS 文件,只是缺少ts
扩展名🤦♂️istanbul
语句,比如/* istanbul ignore else */
所以最好排除它们, comments: "istanbul",
console.log
是一个单独的挑战,它是通过@rollup/plugin-strip 完成的,它与rollup-plugin-cleanup
协同rollup-plugin-cleanup
。 在我的情况下,取决于它是“dev”还是“prod”Rollup 构建(由 CLI 标志--dev
,如rollup -c --dev
),我仅在prod
构建中删除console.log
。 但是在dev和prod版本上的评论都被删除了。目前,我使用:
import cleanup from "rollup-plugin-cleanup";
...
{
input: "src/main.ts",
output: ...,
external: ...,
plugins: [
...
cleanup({ comments: "istanbul", extensions: ["js", "ts"] }),
...
这是使用我的 Rollup 配置的rollup-plugin-cleanup
的示例,这是我的 Rollup 配置生成器(在 monorepos 中,Rollup 配置很难手动维护,所以我生成了它们)。 如果您决定连接--dev
CLI 标志,那么问题是您必须在脚本结束之前从commandLineArgs
删除该标志,否则 Rollup 将抛出,请参阅原始提示和操作。
您应该也可以使用rollup-plugin-terser
来实现这rollup-plugin-terser
。 它基于terser所以更多信息实际上可以在它的 README 中找到,这里是与minification相关的部分。 因此,在您的情况下, rollup.config.js
这一部分应如下所示:
plugins: [
terser({
// remove all comments
format: {
comments: false
},
// prevent any compression
compress: false
}),
],
请记住,您还可以仅为生产启用部分配置。 所以在你的rollup.config.js
中声明了production
const 你可以这样做:
import { terser } from 'rollup-plugin-terser';
const production = !process.env.ROLLUP_WATCH;
export default {
plugins: [
production && terser({
// terser plugin config here
}),
],
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.