簡體   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