|
使用gulp-uglify插件进行js压缩,代码如下:
gulp.task("uglifyJs",function(){
return gulp.src("./src/public/js/test/*.js").
pipe(concat("doubleyong.min.js")).
pipe(uglify({
mangle:false, // 修改变量名,默认为true
compress:false,
preserveComments:'all'//保留所有注释
//保留所有注释的设置,不保留就不写这个参数
})).pipe(gulp.dest("./dist/public/js"))
})在添加“preserveComments:'all'//保留所有注释” 时,报如下错误:
events.js:182 throw er; // Unhandled 'error' event ^ GulpUglifyError: unable to minify JavaScript at createError (e:\guoxinan\classproject\153\gulp\day02\node_modules\gulp-uglify\lib\create-error.js:6:14) at apply (e:\guoxinan\classproject\153\gulp\day02\node_modules\lodash\_apply.js:16:25) at wrapper (e:\guoxinan\classproject\153\gulp\day02\node_modules\lodash\_createCurry.js:41:12) at e:\guoxinan\classproject\153\gulp\day02\node_modules\gulp-uglify\lib\minify.js:54:15 at DestroyableTransform._transform (e:\guoxinan\classproject\153\gulp\day02\node_modules\gulp-uglify\composer.js:10:23) at DestroyableTransform.Transform._read (e:\guoxinan\classproject\153\gulp\day02\node_modules\readable-stream\lib\_stream_transform.js:182:10) at DestroyableTransform.Transform._write (e:\guoxinan\classproject\153\gulp\day02\node_modules\readable-stream\lib\_stream_transform.js:170:83) at doWrite (e:\guoxinan\classproject\153\gulp\day02\node_modules\readable-stream\lib\_stream_writable.js:406:64) at writeOrBuffer (e:\guoxinan\classproject\153\gulp\day02\node_modules\readable-stream\lib\_stream_writable.js:395:5) at DestroyableTransform.Writable.write (e:\guoxinan\classproject\153\gulp\day02\node_modules\readable-stream\lib\_stream_writable.js:322:11) Process finished with exit code 1 但是,不加上面的preserveComments ,就可以正常执行. 在网上百度下,发现网上这样写的,它们的gulp-uglify插件都是1.x版本 看了下,当前package.json的配置,目前,下载gulp-uglify插件是3.0.0版本. 解决方案: 将 gulp-uglify 的下载成 1.1版本,然后代码便可以正常执行了. 关于gulp-uglify3.0下如何去保留注释,目前还没有查找到相应的API说明,如有知道的,欢迎分享哦. 下载指定版本的gulp-uglify命令如下: npm install gulp-uglify@1.1 --save-dev 本文原创,转载请写明链接地址: 【bug收集】:http://bugshouji.com/mybug3/t436 |
|
|
|
沙发#
发布于:2020-11-24 20:38
厉害厉害,我找了两三天都没找到原因,下面那个贴出来的官方API是什么意思啊?
|
|
|
板凳#
发布于:2018-03-28 22:42
|
|
|
地板#
发布于:2018-03-26 12:41
|
|
|
|
4楼#
发布于:2018-03-26 12:33
3.0细化了配置选项
官方API minify-options-structure 注释处理在 Output options 中 官方说明 comments (default false) -- pass true or "all" to preserve all comments, "some" to preserve some comments, a regular expression string (e.g. /^!/) or a function. 具体用法 gulp.task('yasuo',function(){ return gulp.src('./src/demo.js') .pipe(uglify({ output:{ comments:'all' } })) .pipe(gulp.dest('./dist/js')) }) |
|