Vue Cli3 打包配置并自动忽略console.log语句的方法
网络编程 2021-07-04 15:02www.168986.cn编程入门
这篇文章主要介绍了Vue Cli3 打包配置并自动忽略console.log语句的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们狼蚁网站SEO优化随着长沙网络推广来一起学习学习吧
下载插件
npm i -D uglifyjs-webpack-plugin
在 vue.config.js 引入使用
const UglifyJsPlugin = require('uglifyjs-webpack-plugin') module.exports = { configureWebpack: { plugins: [ new UglifyJsPlugin({ uglifyOptions: { press: { drop_console: true, }, }, }), ], }, devServer: { proxy: { '/xxx': { target: 'http://192.168.150.17:8080/', changeOrigin: true, ws: true, pathRewrite: { '^/xxx': 'xxx', }, }, }, }, publicPath: './', }
这时执行 npm run build
打包后的文件就没有 console.log
语句了。
不过这时会有一个问题,就是在开发环境的时候编译会非常慢。例如修改了一个变量的值,我的电脑要编译 10 秒才能重新刷出来页面,一直卡在 92% chunk asset optimization
。
由于去掉 console.log
语句这个功能只有在打包时才需要,所以我们可以加一个判断,只在生产环境时才把上述配置代码加上。
所以正确的配置如下:
const UglifyJsPlugin = require('uglifyjs-webpack-plugin') const config = { devServer: { proxy: { '/xxx': { target: 'http://192.168.150.17:8080/', changeOrigin: true, ws: true, pathRewrite: { '^/xxx': 'xxx', }, }, }, }, publicPath: './', } if (process.env.NODE_ENV === 'production') { config.configureWebpack = { plugins: [ new UglifyJsPlugin({ uglifyOptions: { press: { drop_console: true, }, }, }), ], } } module.exports = config
vue-cli3.0 生产包去除console.log
不安装插件去除console.log的方法
vue-cli3.0在打包过程中就使用了terser-webpack-plugin插件进行优化,具体配置可以node_modules/@vue/cli-service/lib/config/prod.js中看到。
这里使用了环境变量进行控制,只有打生产包的时候才会调用这个插件进行打包优化。
terser-webpack-plugin的具体配置在同一个文件夹下terserOptions.js中,只要在这个文件中press对象中加入以下几个属性就可以了
warnings: false, drop_console: true, drop_debugger: true, pure_funcs: ['console.log']
到此这篇关于Vue Cli3 打包配置并自动忽略console.log语句的方法的文章就介绍到这了,更多相关Vue Cli3打包并忽略console.log内容请搜索狼蚁SEO以前的文章或继续浏览狼蚁网站SEO优化的相关文章希望大家以后多多支持狼蚁SEO!
上一篇:js实现简单的贪吃蛇游戏
下一篇:vue项目打包之开发环境和部署环境的实现
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程