在这个项目中,我使用 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/