解决vue编译时错误 - error: Unexpected console statement (no-console)

本文介绍了解决Vue项目中编译时出现的console.log错误的方法,通过配置.eslintrc.js文件来禁用no-console规则。同时,为了进一步优化生产环境的构建,文章还介绍了如何使用terser-webpack-plugin插件移除console.log,减少运行时开销。

解决vue编译时错误

error: Unexpected console statement (no-console)

修复办法

修改 .eslintrc.js

module.exports = {
  root: true,
  env: {
    node: true
  },
  extends: ["plugin:vue/essential", "@vue/prettier", "@vue/typescript"],
  rules: {
    // "no-console": process.env.NODE_ENV === "production" ? "error" : "off",
    "no-console": "off",
    "no-debugger": process.env.NODE_ENV === "production" ? "error" : "off"
  },
  parserOptions: {
    parser: "@typescript-eslint/parser"
  }
};

去掉console.log等

但是仅仅去掉eslint错误,并不能解决console.log的运行时开销,我们需要去掉它们,当然不能手工去掉,这样工作量大,而且影响源码版本管理。可以使用terser来做这个工作。

  1. 安装terser-webpack-plugin
    npm install terser-webpack-plugin --save-dev

  2. 添加或修改vue.config.js

const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
    publicPath: process.env.NODE_ENV === 'production' ? '/file-browser/' : '/',
    configureWebpack: (config) => {
        if (process.env.NODE_ENV === 'production') {
            return {
                optimization: {
                    minimizer: [new TerserPlugin({
                        terserOptions: {
                            compress: {
                                drop_console: true
                            }
                        }
                    })]
                },
            }
        }
    }
}
[plugin:vite-plugin-eslint] D:\物联网\src\views\SceneManagement\list\index.vue 6:14 warning Expected a linebreak before this attribute vue/first-attribute-linebreak 16:78 warning Attribute "v-if" should go before ":content" vue/attributes-order 19:39 warning Attribute "v-else" should go before "class" vue/attributes-order 36:23 warning Expected a linebreak before this attribute vue/first-attribute-linebreak 38:23 warning Expected a linebreak before this attribute vue/first-attribute-linebreak 41:19 warning Expected a linebreak before this attribute vue/first-attribute-linebreak 86:23 warning Expected a linebreak before this attribute vue/first-attribute-linebreak 88:23 warning Expected a linebreak before this attribute vue/first-attribute-linebreak 108:24 warning Expected a linebreak before this attribute vue/first-attribute-linebreak 122:27 warning Expected a linebreak before this attribute vue/first-attribute-linebreak 132:22 warning Expected a linebreak before this attribute vue/first-attribute-linebreak 136:26 warning Expected a linebreak before this attribute vue/first-attribute-linebreak 148:37 warning Expected a linebreak before this attribute vue/first-attribute-linebreak 151:30 warning Expected a linebreak before this attribute vue/first-attribute-linebreak 156:31 warning Expected a linebreak before this attribute vue/first-attribute-linebreak 169:14 warning Expected a linebreak before this attribute vue/first-attribute-linebreak 225:27 warning Expected a linebreak before this attribute vue/first-attribute-linebreak 228:24 warning Expected a linebreak before this attribute vue/first-attribute-linebreak 268:19 warning Expected a linebreak before this attribute vue/first-attribute-linebreak 282:14 warning Expected a linebreak before this attribute vue/first-attribute-linebreak 299:19 warning Expected a linebreak before this attribute vue/first-attribute-linebreak 322:20 warning Expected a linebreak before this attribute vue/first-attribute-linebreak 331:24 warning Expected a linebreak before this attribute vue/first-attribute-linebreak 349:23 warning Expected a linebreak before this attribute vue/first-attribute-linebreak 364:15 warning 'Dayjs' is defined but never used @typescript-eslint/no-unused-vars 365:96 warning 'GetIrCtrlCode' is defined but never used @typescript-eslint/no-unused-vars 365:128 warning 'sendDoorControls' is defined but never used @typescript-eslint/no-unused-vars 365:146 warning 'setAirConditionParams' is defined but never used @typescript-eslint/no-unused-vars 365:233 warning 'GetModelOutputItem' is defined but never used @typescript-eslint/no-unused-vars 372:8 warning 'D:\物联网\node_modules\@arco-design\web-vue\es\icon\index.d.ts' imported multiple times import/no-duplicates 374:3 warning 'IconCalendar' is defined but never used @typescript-eslint/no-unused-vars 375:3 warning 'IconHome' is defined but never used @typescript-eslint/no-unused-vars 376:3 warning 'IconLeft' is defined but never used @typescript-eslint/no-unused-vars 377:3 warning 'IconRight' is defined but never used @typescript-eslint/no-unused-vars 378:8 warning 'D:\物联网\node_modules\@arco-design\web-vue\es\icon\index.d.ts' imported multiple times import/no-duplicates 380:8 warning 'axios' is defined but never used @typescript-eslint/no-unused-vars 381:8 warning 'JsSHA' is defined but never used @typescript-eslint/no-unused-vars 382:10 warning 'generateEventConfig' is defined but never used @typescript-eslint/no-unused-vars 388:8 warning 'DateCell' is defined but never used @typescript-eslint/no-unused-vars 456:5 error The "OperationBar" component has been registered but not used vue/no-unused-components 601:14 warning 'getStatusText' is defined but never used @typescript-eslint/no-unused-vars 611:14 warning 'getPatternText' is defined but never used @typescript-eslint/no-unused-vars 621:14 warning 'getSpeedText' is defined but never used @typescript-eslint/no-unused-vars 651:14 warning 'getControlText' is defined but never used @typescript-eslint/no-unused-vars 660:14 warning 'getDeviceNameByDoorId' is defined but never used @typescript-eslint/no-unused-vars 1064:13 warning Unexpected console statement no-console 1186:9 warning Unexpected console statement no-console 1188:9 warning Unexpected console statement no-console 1234:9 warning Unexpected console statement no-console 1266:9 warning Unexpected console statement no-console 1269:9 warning Unexpected console statement no-console 1304:7 warning Unexpected console statement no-console 1305:7 warning Unexpected console statement no-console 1308:9 warning Unexpected console statement no-console 1343:15 warning Unexpected console statement no-console 1410:7 warning Unexpected console statement no-console 1419:11 warning Unexpected console statement no-console 1426:11 warning Unexpected console statement no-console 1432:11 warning Unexpected console statement no-console 1437:7 warning Unexpected console statement no-console 1536:15 warning Unexpected console statement no-console 1642:9 warning Unexpected console statement no-console 1644:9 warning Unexpected console statement no-console 1668:13 warning Unexpected console statement no-console 1801:7 warning Unexpected console statement no-console 1803:7 warning Unexpected console statement no-console 1804:7 warning Unexpected console statement no-console 1809:9 warning Unexpected console statement no-console 1813:9 warning Unexpected console statement no-console 1838:14 warning 'fillDetailFormFromChannelList' is defined but never used @typescript-eslint/no-unused-vars 1906:7 warning Unexpected console statement no-console 1917:11 warning 'sendControl' is assigned a value but never used @typescript-eslint/no-unused-vars 1953:7 warning Unexpected console statement no-console 1968:9 warning Unexpected console statement no-console 1970:9 warning Unexpected console statement no-console 2062:9 warning Unexpected console statement no-console 2064:9 warning Unexpected console statement no-console 2068:9 warning Unexpected console statement no-console 2070:9 warning Unexpected console statement no-console 2091:9 warning Unexpected console statement no-console 2092:9 warning Unexpected console statement no-console 2095:9 warning Unexpected console statement no-console 2096:9 warning Unexpected console statement no-console 2102:9 warning Unexpected console statement no-console 2122:13 warning Unexpected console statement no-console 2126:11 warning Unexpected console statement no-console 2134:11 warning Unexpected console statement no-console 2138:9 warning Unexpected console statement no-console 2145:9 warning Unexpected console statement no-console 2146:9 warning Unexpected console statement no-console 2149:9 warning Unexpected console statement no-console 2150:9 warning Unexpected console statement no-console 2154:9 warning Unexpected console statement no-console 2156:9 warning Unexpected console statement no-console 2231:7 warning Unexpected console statement no-console 2258:9 warning Unexpected console statement no-console 2259:9 warning Unexpected console statement no-console 2356:7 warning Unexpected console statement no-console 2360:7 warning Unexpected console statement no-console ✖ 99 problems (1 error, 98 warnings) 0 errors and 25 warnings potentially fixable with the `--fix` option. D:/物联网/src/views/SceneManagement/list/index.vue at formatError (file:///D:/%E7%89%A9%E8%81%94%E7%BD%91/node_modules/vite/dist/node/chunks/dep-3e87c7b2.js:40359:46) at TransformContext.error (file:///D:/%E7%89%A9%E8%81%94%E7%BD%91/node_modules/vite/dist/node/chunks/dep-3e87c7b2.js:40355:19) at TransformContext.transform (D:\物联网\node_modules\vite-plugin-eslint\dist\index.js:1:2469) at async Object.transform (file:///D:/%E7%89%A9%E8%81%94%E7%BD%91/node_modules/vite/dist/node/chunks/dep-3e87c7b2.js:40612:30) at async loadAndTransform (file:///D:/%E7%89%A9%E8%81%94%E7%BD%91/node_modules/vite/dist/node/chunks/dep-3e87c7b2.js:36998:29 Click outside or fix the code to dismiss. You can also disable this overlay by setting server.hmr.overlay to false in vite.config.js.
最新发布
09-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值