Empty Module Loader
A webpack loader that replaces module with empty function.
Install
npm install --save-dev empty-module-loader
Usage
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /path-to-match-module/,
loader: 'empty-module-loader'
}
]
}
}
That is it! All the related codes should be removed in production.
Example
This config is for demonstration only.
webpack.config.js
const path = require('path')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
module.exports = {
entry: './src/index.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /debug/,
loader: require.resolve('empty-module-loader')
},
],
},
optimization: {
minimizer: [
new UglifyJsPlugin({
uglifyOptions: {
warnings: false,
mangle: false,
ie8: false,
output: {
preserve_line: true,
comments: true,
indent_level: 2,
},
},
}),
],
},
}
src/index.js
import debug from './debug'
console.log('normal code')
debug('booting %o', 3)
console.log('normal code')
src/debug.js
export default function debug (...args) {
console.log('debug:', ...args)
}
Run Commands At Project Root
npm init -y
npm install webpack webpack-cli uglifyjs-webpack-plugin --save-dev
npx webpack --config webpack.config.js
Output
([
function(module,__webpack_exports__,__webpack_require__){
"use strict"
;__webpack_require__.r(__webpack_exports__);
console.log("normal code"),
console.log("normal code")
}
]);