当前位置:网站首页>Tragedy caused by deleting the console statement
Tragedy caused by deleting the console statement
2022-07-07 16:20:00 【Bashan queer dialect】
Preface :
although babel-plugin-transform-remove-console It has not been updated for several years , But when packing, remove console The sentence is still relatively time-saving and labor-saving , So it is used in the project , I didn't expect it would be hard
The original question was simple , But sometimes not careful , It may waste a lot of time solving problems , So write a blog , Convenient for others
Quickly quickly your mother call you. , Please detour
1、 Installation and use
install
npm i babel-plugin-transform-remove-console
To configure (babel.config.js)
// Store the plug-ins needed in the project release phase
const productPlugins = []
// If the user is running a package command (npm run build) Inject plug-ins into the array
if (process.env.NODE_ENV === 'production') {
// Release stage
productPlugins.push("transform-remove-console")
}
module.exports = {
presets: [
'@vue/cli-plugin-babel/preset'
],
plugins: [
[
"component",
{
"libraryName": "element-ui",
"styleLibraryName": "theme-chalk"
}
],
...productPlugins
]
}
here , Use npm run build Command packaging will remove... From the file console sentence , After testing , That's true
2. Delete map file
later , In order to further reduce dist Volume of catalog , It was decided that productionSourceMap Set to false, The purpose is not to generate map The mapping file , This process is apart from the problem
2.1 What is? map file ?
Have a look first , Generated by default dist Catalog
- 4 individual js file ,4 individual map file
- js Catalog total 14M,4 individual map The file takes up 11M

These are a few map What exactly do documents do ?
Its main function is after packaging js Establish a mapping between the file and the source code file before packaging , In this way, although the code before and after packaging looks different , But when an error occurs at runtime , You can still locate the source file where the error occurred
Let's demonstrate
such as , We deliberately App.vue The following codes are included in
methods: {
f1() {
return a + 5
},
},
created() {
this.f1()
},
Under development mode , Running the page , The error information is as follows

Click on the wrong file , You can also directly locate

After the project is packaged ?
After we pack , Running the code , Found a blank article in the console , Not that as long as there is map file , After a mistake , It will not only print errors on the console , Can you still locate the wrong source file ?

2.2 Vanished console
Think about it , When the program reports an error , Who outputs the error message in the console ?
For example, the following mistakes , Tips a is not defined
Obviously , By vue adopt console.error Printed on the console

But let's look at what we see above 4 individual js Document and 4 individual map After the file found that , None of them console sentence , Why? ?
The reason is clear , It is written by developers themselves console Statements and some dependencies introduced by programs console sentence , All be babel-plugin-transform-remove-console Plug in removed
original babel-plugin-transform-remove-console The plug-in can delete all files console sentence ( Isn't that for granted )
2.3 How to solve
Actually , Official documents have long said

therefore , modify babel.config.js Middle configuration , Add exception for deletion , In this way, all the files console.error and console.warn Just keep it

here , You can view the package generated js and map In file , Filled with a large number of the above two statements
After repacking , Run again

It was found that the output was wrong , And prompt which source file the error occurred in
2.4 Cancel map file
Last , You can cancel the generation map The file
vue.config.js in

REPACK , And then run , Still include , But we can't locate the specific source file where the error occurred , But the generated file

go through dist/js Catalog , There is no such thing as map The file , Of course dist The volume of the directory is much smaller
When requested by the client , Will not download map file , So the speed will also increase
3. summary
babel-plugin-transform-remove-console The use of plug-ins is very simple , But if you read your posts carefully , It seems that they are all copied from the same template , Few people mention exclude The function of attributes , If you are a newcomer and encounter problems similar to the above , It may take a lot of time to solve , So write this blog , Convenient for others
边栏推荐
- prometheus api删除某个指定job的所有数据
- 强化实时数据管理,英方软件助力医保平台安全建设
- Step by step monitoring platform ZABBIX
- Unity drawing plug-in = = [support the update of the original atlas]
- 分类模型评价标准(performance measure)
- 招标公告:2022年云南联通gbase数据库维保公开比选项目(第二次)比选公告
- Shipping companies' AI products are mature, standardized and applied on a large scale. CIMC, the global leader in port and shipping AI / container AI, has built a benchmark for international shipping
- 修改配置文件后tidb无法启动
- 航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆
- laravel怎么获取到public路径
猜你喜欢
随机推荐
95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
Eye of depth (VI) -- inverse of matrix (attachment: some ideas of logistic model)
ThinkPHP URL 路由简介
PHP实现执行定时任务的几种思路详解
121. 买卖股票的最佳时机
Description of vs common shortcut keys
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
修改配置文件后tidb无法启动
prometheus api删除某个指定job的所有数据
Leetcode-136-只出现一次的数(用异或来解答)
laravel 是怎么做到运行 composer dump-autoload 不清空 classmap 映射关系的呢?
asyncio 概念和用法
three.js打造酷炫下雪效果
Odoo集成Plausible埋码监控平台
laravel post提交数据时显示异常
Continuous creation depends on it!
强化实时数据管理,英方软件助力医保平台安全建设
95. (cesium chapter) cesium dynamic monomer-3d building (building)
Leetcode-231-2的幂
Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition








