解决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来做这个工作。
-
安装terser-webpack-plugin
npm install terser-webpack-plugin --save-dev -
添加或修改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
}
}
})]
},
}
}
}
}