项目作者: YetiGoodbye

项目描述 :
Webpack loader for stripping debug statements in production
高级语言: JavaScript
项目地址: git://github.com/YetiGoodbye/cleanup-debug-loader.git
创建时间: 2018-07-28T11:59:23Z
项目社区:https://github.com/YetiGoodbye/cleanup-debug-loader

开源协议:

下载


cleanup-debug-loader

It’s just couple of regex in build process, intended to filter out lines or blocks of unnecessary code depending on running evironment (development or production). It allows you not to cary about cleaning out debug code from production.
Now instead of

  1. if(node.ENV === 'development'){
  2. console.log('blablabla');
  3. }

you just can write

  1. #- console.log('...');

Installation

  1. npm i --save-dev cleanup-debug-loader
webpack.config.js

It must be the last js loader in pipeline, or you get a syntax error

  1. {
  2. module: {
  3. rules: [{
  4. test: /\.js$/,
  5. exclude: /node_modules/,
  6. loader: 'any-other-js-loader',
  7. },{
  8. test: /\.js$/,
  9. exclude: /node_modules/,
  10. use: [{ loader: 'cleanup-debug-loader') }],
  11. }],
  12. },
  13. };

Configuration

Loader supports several options. To find out if this is development environment, loader use mode webpack option. So it should be presented in configuration file. You can also change start marker (default is #) and hallmarks for dev and nondev modes (defaults + and -).

little attention

All options are injected directly into regexp. So if changing defaults, do it with caution if using special regexp characters (should be wrapped with square brackets).

Configuration example:

  1. {
  2. module: {
  3. mode: 'development',
  4. rules: [{
  5. test: /\.js$/,
  6. exclude: /node_modules/,
  7. use: [{
  8. loader: 'cleanup-debug-loader',
  9. options: {
  10. marker: '@@', // change default
  11. dropInDev: '[$]', // it's regexp special
  12. dropInProd: 'p',
  13. }
  14. }],
  15. }],
  16. },
  17. };

Usage

Prepend lines you wish to keep only in dev mode with #- (any whitespace before and after is allowed). If there is something you want to strip out from development, prepend it with with #+.
You can also wrap entire blocks as shown below. Nesting is not supported.

  1. #- console.log('This line will be keeped only in development')
  2. #+ console.log('This line will be dropped from development')
  3. #-[
  4. console.log('This entire block will')
  5. console.log('be keeped only in development')
  6. #-]
  7. #+[
  8. console.log("Hello, I'm turtle")
  9. #+]

Warning!

Loader relies on _compilation property of loader context https://webpack.js.org/api/loaders/#this-_compilation.
Webpack is not recommending to do so. But it’s still working for now, and I’m ok with that.

Issues

If you have any issues with it please report https://github.com/YetiGoodbye/cleanup-debug-loader/issues

Thanks and enjoy!