New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

webpack-if

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

webpack-if

Helpers to create webpack configurations

latest
Source
npmnpm
Version
0.1.2
Version published
Weekly downloads
37
-19.57%
Maintainers
1
Weekly downloads
 
Created
Source

webpack-if

Build Status

webpack-if provides some helper methods to create webpack configurations with tests inside of an object literal so that your configuration can mutate due to the state of the execution environment without needing to split up construction that can leading to config builders or files with hard to follow files.

Install

Use it as a dev dependency with

npm install --save-dev webpack-if

or

yarn install -D webpack-if

Usage

webpack-if's two main helpers are its ifElse member and its exported function.

ifElse called with a condition or test returns a function that can be called with two arguments. If the condition is true the first argument is returned. Otherwise the second argument is returned.

const webpackIf = require('webpack-if');

const ifProd = webpackIf.ifElse(process.env.NODE_ENV === 'production');

module.exports = {
  entry: ifProd('./src/main.prod.js', './src/main.js'),
  // ...
  plugins: [
    ifProd(() => new UglifyJsPlugin()),
  ],
};

ifElse simplest use is for testing some environment variable to determine the intent of the build. Is it for development or production where you may want to use different entry points, or plugins and other configuration.

In the above case if not part of a production build ifProd(() => new UglifyJsPlugin()) will return null. Normally webpack will have an issue with null in the plugins array or other parts of the configuration. To help clean up those null values the exported value for webpack-if itself is a function to do that job.

module.exports = webpackIf({
  // ...
});

Calling the webpackIf with the configuration as an argument will let the function recursively go through the configuraiton and remove object keys and array indices with null or undefined values. After that empty objects and arrays are also cleaned up.

Here's a possible full webpack configuration using this library.

const webpackIf = require('webpack-if');

const {join} = require('path');

const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
const wepback = require('webpack');

const CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin;
const DefinePlugin = webpack.DefinePlugin;
const UglifyJsPlugin = webpack.optimize.UglifyJsPlugin;

if (!process.env.NODE_ENV) {
  process.env.NODE_ENV === 'development';
}

const nodeEnv = process.env.NODE_ENV;

const ifDev = webpackIf.ifElse(nodeEnv === 'development');
const ifProd = webpackIf.ifElse(nodeEnv === 'production');

module.exports = webpackIf({
  context: __dirname,
  entry: {
    main: './src/main.js',
    vendor: ifProd(['react', 'react-dom']),
  },
  output: {
    path: join(__dirname, 'dist'),
    filename: ifProd('[name].[hash].js', '[name].js'),
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        options: {
          presets: ['react', 'env'],
        },
      },
      {
        test: /\.css$/,
        use: ifProd(
          () => ExtractTextPlugin.extract({
            use: [{
              loader: 'css-loader',
              options: {
                modules: true,
              },
            }],
            fallback: 'style-loader',
          }),
          () => ['style-loader', {
            loader: 'css-loader',
            options: {
              modules: true,
              localIdentName: '[path][name]__[local]--[hash:base64:5]',
            },
          }],
        ),
      },
    ],
  },
  plugins: [
    ifProd(() => new UglifyJsPlugin()),
    ifProd(() => new CommonsChunkPlugin('vendor')),
    ifProd(() => new ExtractTextPlugin('[contenthash].css')),
    ifProd(() => new DefinePlugin({
      'process.env.NODE_ENV': '"production"',
    })),
    ifDev(() => new HardSourceWebpackPlugin()),
  ],
});

Keywords

webpack

FAQs

Package last updated on 13 Jun 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts