使用带有gull插件的UglifyJS2时,请保留许可证注释



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

在项目页面上,

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

这个答案展示了如何将minimy选项传递给插件。

UglifyJS自述中指出,为了保存许可证文本

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"也不会导致许可证属性注释,因此我假设minimy选项comments的行为与命令行参数--comments不同。

我也尝试了这里找到的preserveComments,但它只会生成:

[13:37:42] GulpUglifyError: unable to minify JavaScript
Caused by: DefaultsError: `preserveComments` is not a supported option

有没有一种方法可以通过gulp-uglify插件实现命令行参数所建议的内容?如果不可能,我可以使用webpack插件吗?

有一种通过指定regexp来解决的方法,但如果可能的话,我想直接使用UglifyJS中的功能。此外,它也不会保留这样的许可证标题。

我也遇到了同样的问题。我注意到UglifyJS评论文档建议

您可以传递--comments all以保留所有注释,或者传递有效的JavaScript正则表达式以仅保留与此正则表达式匹配的注释。例如,--comments /^!/将保留类似/*! Copyright Notice */的注释。

所以我试着"comments: /^!/":

.pipe(uglify({
mangle: true,
output: {
beautify: true,
comments: /^!/
}
}))

我现在看到了由此产生的未经验证的代码中的版权注释。

最新更新