[英]NuxtJS: Disable console.log in production env
我正在尋找一種為生產環境禁用console.log()
的方法。 類似於將以下代碼nuxt.config.js
或index.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.