Member-only story

Quickly delete logs from JavaScript projects

Beck Moulton
2 min readMay 9, 2023

--

In JavaScript project development in most cases we will write a lot of logs, when the development is completed in most cases we want to delete these logs, this article will introduce two programs

Option one

The webpack loader is essentially a function. We can match the string we want to delete according to the regularity inside this function and replace it.

The custom loaders/ignore-console-log-loader.js code is simple, as follows:

const reg = /(console.log\()(.*)(\))/g

module.exports = function (sourceCode) {
return sourceCode.replace(reg, '')
}

To use it, add a custom loader to the webpack.config.js configuration file:

module: {
rules: [
{
test: /\.js$/,
loader: ['ignore-console-log-loader'],
},
]
},
resolveLoader: {
modules: ['node_modules', path.resolve(__dirname, 'loaders')]
},

Option two

For example, several core steps of the principle of webpack packaging are to use babel (simply mention it, do not expand the principle of packaging, and write a separate article later)

  • Get the main entry content
  • Analyze the main entrance module (@babel/parser package, transfer AST)

--

--

Beck Moulton
Beck Moulton

Written by Beck Moulton

Focus on the back-end field, do actual combat technology sharing Buy me a Coffee if You Appreciate My Hard Work https://www.buymeacoffee.com/BeckMoulton

No responses yet