将UglifyJS2与gulp插件一起使用时保留许可证注释

时间:2018-12-19 14:34:04

标签: 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"也不会导致许可证归因注释,所以我认为minify选项comments的行为不同于命令行参数--comments

我还尝试了here中找到的preserveComments,但这只会生成:

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

是否有一种方法可以通过gulp-uglify插件实现commandline argument的建议?如果不可能,我可以使用webpack plugin吗?

通过指定this workarounda regexp,但我想尽可能地直接从UglifyJS使用该功能。此外,它也不保留这样的许可证标头。

1 个答案:

答案 0 :(得分:0)

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

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

所以我尝试了“ comments: /^!/”:

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

我现在在生成的丑化代码中看到版权评论。