forked-webpack-conditional-loader

1.0.13 • Public • Published

webpack-conditional-loader


Inspired by C conditionals directive, conditional loader decides if a given block should be included in the final bundle.

Useful for removing instrumentation code and making your final production bundle smaller (therefore faster).

Installation

# with npm:
npm install --save-dev forked-webpack-conditional-loader
# or with yarn:
yarn add --dev forked-webpack-conditional-loader

Usage

In your webpack.config.js

Put forked-webpack-conditional-loader as the last loader in the array, so it will process the code before all others.

module: {
  rules: [{
    test: /\.js$/,
    loader: ['babel-loader', 'forked-webpack-conditional-loader']
  }]
}

Get an example config file here

On your code

Use // #if expression and // #endif to wrap blocks of code you want to be removed if a given predicate is false.

// #if process.env.NODE_ENV === 'DEVELOPMENT'
console.log('lorem')
console.log('ipsum')
// #endif

In the example above, the code will be removed if the enviroment variable NODE_ENV is not DEVELOPMENT, removing unnecessary code from your production bundle.

The same technique can be used to prevent loading packages in the production bundle.

// #if process.env.NODE_ENV !== 'BUILD'
import reduxLogger from 'redux-logger'
// #endif

Credits


caiogondim.com  ·  GitHub @caiogondim  ·  Twitter @caio_gondim

Readme

Keywords

none

Package Sidebar

Install

npm i forked-webpack-conditional-loader

Weekly Downloads

1

Version

1.0.13

License

MIT

Unpacked Size

7.4 kB

Total Files

9

Last publish

Collaborators

  • lkrazy