繁体   English   中英

NuxtJS:在生产环境中禁用 console.log

[英]NuxtJS: Disable console.log in production env

我正在寻找一种为生产环境禁用console.log()的方法。 类似于将以下代码nuxt.config.jsindex.js

if (process.env.NODE_ENV !== "development") {
  console.log = () => {};
}

我试过了,但它不起作用。 任何帮助,将不胜感激。

我的 nuxt.config.js 在这里https://gist.github.com/somaria/9a2b0e06497d13a35fe9eee141a15d07

Nuxt 的构建过程包括terser ,它可以配置为自动从生产构建中删除控制台语句。 您可以设置build.terser.terserOptions

// nuxt.config.js
export default {
  build: {
    terser: {
      // https://github.com/terser/terser#compress-options
      terserOptions: {
        compress: {
          drop_console: true
        }
      }
    }
  }
}

作为替代方案,这也可以通过插件来完成。

Plugins文件夹下,我们可以创建一个名为disableLogs.js的文件,如下所示:

// plugins/disableLogs.js

export function disableLogs() {
  console.log = () => {};
  // or you can override any other stuff you want
}

process.env.NODE_ENV === "production" ? disableLogs() : null;

然后我们就可以注册这个插件在nuxt.config.js里面nuxt.config.js

// nuxt.config.js
plugins: [
  { src: "~/plugins/disableLogs.js" },
  { src: "~/plugins/any-other-plugin.js"
],

这将在实例化根 Vue.js 应用程序之前运行。

还有其他的东西,你可以配置它运行客户端或服务器端等。更多信息在这里 - https://nuxtjs.org/guide/plugins#vue-plugins

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM