npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

webpack-if

v0.1.2

Published

Helpers to create webpack configurations

Downloads

286

Readme

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()),
  ],
});