網(wǎng)站首頁 編程語言 正文
一、webpack中清除console的方法
當(dāng)然想要清除console我們可以使用babel-loader結(jié)合babel-plugin-transform-remove-console插件來實(shí)現(xiàn)。
- 安裝babel-loader和babel-plugin-transform-remove-console插件
npm install babel-loader babel-plugin-transform-remove-console -D
- 在webpack配置文件中配置loader
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
plugins: [['transform-remove-console', {exclude: ['error', 'warn']}]]
}
}
}
]
}
};
其中,babel-plugin-transform-remove-console
插件中的exclude選項(xiàng)可以指定保留哪些日志級(jí)別的console語句。上面的示例中保留了error
和warn
級(jí)別的console語句,其它級(jí)別的將被移除。如果不指定exclude選項(xiàng),則會(huì)移除所有console語句。
二、自定義實(shí)現(xiàn)clean-console-loader
上面我們是借助babel插件實(shí)現(xiàn)的清除console,那么經(jīng)過前面章節(jié)的鋪墊,我們了解了loader的本質(zhì)、執(zhí)行順序、以及分類還有常用的api,我們是否可以自己嘗試寫一個(gè)類似功能的loader呢?
下面我們就一起來實(shí)現(xiàn)一下吧
其實(shí)很簡單就用正則匹配替換一下就行了,哈哈哈
- clean-console.loader
module.exports = function (content) {
// 清除文件內(nèi)容中console.log(xxx)
return content.replace(/console\.log\(.*\);?/g, "");
};
配置使用
- webpack.config.js
module: {
rules: [
{
test: /\.js$/,
loader: "./loaders/clean-log-loader",
},
]
}
這樣就可以啦
- main.js
我們?cè)趍ain.js中寫兩個(gè)console
打包看看效果
npm run dev
可以看到控制臺(tái)干干凈凈什么都沒有,說明我們自定義的清除console語句的loader生效啦。
原文鏈接:https://blog.csdn.net/jieyucx/article/details/131339662
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-06-06 PyTorch?device與cuda.device用法介紹_python
- 2022-05-12 Python 正則替換內(nèi)容
- 2022-04-23 配置基于域名訪問的網(wǎng)站以及指定用戶可訪問的網(wǎng)站
- 2022-07-17 一起詳細(xì)聊聊C#中的Visitor模式_C#教程
- 2022-05-18 python操作jira添加模塊的方法_python
- 2022-10-24 React中父子組件通信詳解_React
- 2022-05-06 如何利用Python處理excel表格中的數(shù)據(jù)_python
- 2022-12-24 Kubernetes?kubectl中Pod創(chuàng)建流程源碼解析_云和虛擬化
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支