Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

empty-module-loader

1.0.0 • Public • Published

Empty Module Loader

A webpack loader that replaces module with empty function.

npm deps Npm Downloads Total

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

/************************************************************************/
/******/([
/* 0 */
/***/function(module,__webpack_exports__,__webpack_require__){
 
"use strict"
;__webpack_require__.r(__webpack_exports__);// removed by empty-module-loader
// CONCATENATED MODULE: ./src/index.js
 
 
 
 
 
console.log("normal code"),
 
 
 
console.log("normal code")
/***/}
/******/]);

Install

npm i empty-module-loader

DownloadsWeekly Downloads

13

Version

1.0.0

License

MIT

Unpacked Size

4.31 kB

Total Files

4

Last publish

Collaborators

  • avatar