Sum*_*Sun 10 javascript rollup typescript webpack
我正在使用 rollup 来构建我的 TypeScript 源代码。我只想删除注释,而不进行任何缩小,以便在调试时热更新代码。
我已经尝试过rollup-plugin-terser
,它可以删除注释,但它也会以某种方式缩小我的代码,我无法完全禁用缩小。
我怎样才能做到这一点?谢谢!
rev*_*elt 11
就像@jubes 在评论中回答的那样,rollup-plugin-cleanup将完成该任务。我想扩展一下。
\n三件事:
\nts
to extensions list, like extensions: ["js", "ts"]
\xe2\x80\x94 otherwise sources won\'t be processed, even if transpiling step typescript()
is before it \xe2\x80\x94 I originally came here investigating why rollup-plugin-cleanup
won\'t work on TS files and it was just ts
extension missing \xe2\x80\x8d\xe2\x99\x82\xef\xb8\x8fistanbul
statements like /* istanbul ignore else */
so it\'s good to exclude them, comments: "istanbul",
console.log
is a separate challenge which is done with @rollup/plugin-strip and it goes in tandem to rollup-plugin-cleanup
. In my case, depending is it a "dev" or "prod" Rollup build (controlled by a CLI flag --dev
, as in rollup -c --dev
), I remove console.log
on prod
builds only. But comments are removed on both dev and prod builds.currently, I use:
\nimport cleanup from "rollup-plugin-cleanup";\n...\n{\n input: "src/main.ts",\n output: ...,\n external: ...,\n plugins: [\n ...\n cleanup({ comments: "istanbul", extensions: ["js", "ts"] }),\n ...\n
Run Code Online (Sandbox Code Playgroud)\nHere\'s an example of rollup-plugin-cleanup
being used my Rollup config, here\'s my Rollup config generator (in monorepos, Rollup configs are hard to maintain by hand so I generate them). If you decide to wire up --dev
CLI flag, the gotcha is you have to remove the flag from the commandLineArgs
before script ends, otherwise Rollup will throw, see the original tip and it in action.
您也应该能够仅通过rollup-plugin-terser
. 它基于terser ,因此更多信息实际上可以在其 README 中找到,这是与minification相关的部分。所以在你的情况下,这部分rollup.config.js
应该看起来像:
plugins: [
terser({
// remove all comments
format: {
comments: false
},
// prevent any compression
compress: false
}),
],
Run Code Online (Sandbox Code Playgroud)
请记住,您还可以仅为生产启用部分配置。因此,在您声明production
const后,rollup.config.js
您可以这样做:
import { terser } from 'rollup-plugin-terser';
const production = !process.env.ROLLUP_WATCH;
export default {
plugins: [
production && terser({
// terser plugin config here
}),
],
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6391 次 |
最近记录: |