webpack - 使用带有 gulp 插件的 UglifyJS2 时保留许可证注释

标签 webpack gulp uglifyjs gulp-uglify uglifyjs2

在这个项目中,我使用 gulp-uglify版本 3.0.1,我想在输出中保留包含许可证文本的注释。

在项目页面上指出

Most of the minify options from the UglifyJS API are supported.

this answer展示如何通过 the minify options到插件。

UglifyJS Readme据说为了保护许可证文本
You can pass --comments to retain certain comments in the output. By default it will keep JSDoc-style comments that contain "@preserve", "@license" or "@cc_on" (conditional compilation for IE)

所以我试过:
.pipe(uglify({
    mangle: true,
    output: {
        beautify: true,
        comments: "all"
    }
}))

但是因为即使指定 "all"结果没有许可证归属评论,我假设缩小选项 comments行为不同于命令行参数 --comments .

我也试过 preserveComments找到 here但这只会产生:
[13:37:42] GulpUglifyError: unable to minify JavaScript
Caused by: DefaultsError: `preserveComments` is not a supported option

有什么方法可以实现commandline argument通过 gulp-uglify 提出建议插入?如果不可能,我可以使用 webpack plugin ?

this workaround通过指定 a regexp但如果可能的话,我想直接从 UglifyJS 使用该功能。此外,它也不会保留这样的许可证 header 。

最佳答案

我有同样的问题。我注意到 the UglifyJS comments documentation建议

You can pass --comments all to keep all the comments, or a valid JavaScript regexp to keep only comments that match this regexp. For example --comments /^!/ will keep comments like /*! Copyright Notice */.


所以我尝试了“comments: /^!/”:
.pipe(uglify({
    mangle: true,
    output: {
        beautify: true,
        comments: /^!/
    }
}))
我现在在生成的丑化代码中看到版权注释。

关于webpack - 使用带有 gulp 插件的 UglifyJS2 时保留许可证注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53853402/

相关文章:

node.js - 使用 uglify 将 TypeScript 转为 JavaScript

node.js - WebPack:如何查找 Bower.json 和 .bower.json

javascript - 无法使 Bootstrap 与 Webpack 一起工作

javascript - webpack 创建的脚本执行两次

javascript - 如何只在本地运行 gulp.task?

javascript - 如何从 Webhook 运行 Gulp

angularjs - "Unknown provider: aProvider <- a"如何找到原始提供商?

javascript - Vue cli 3 项目别名 src 到 @ 或 ~/不工作

javascript - 浏览器同步不会在监视任务完成时重新加载页面

javascript - 如何使用 RequireJS 优化器删除评论?