Code Monkey home page Code Monkey logo

webpack-config-utils's Introduction

webpack-config-utils

Utilities to help your webpack config be easier to read

Build Status Code Coverage Dependencies version downloads MIT License

All Contributors PRs Welcome Donate Code of Conduct Roadmap Examples

The problem

Webpack configuration is a JavaScript object which is awesomely declarative. However, the webpack config file can easily turn into an imperative mess in the process of creating the configuration object.

This solution

The goal of this project is to provide utilities to make it easier to compose your config object so it's easier for people to read. It has some custom methods and also comes bundled with some other projects to expose some helpful utility functions.

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's devDependencies:

npm install --save-dev webpack-config-utils

Usage

It is expected that you use this in your webpack.config.js file.

Protip: You can name your config file webpack.config.babel.js and it'll be automagically transpiled! (Make sure you have babel-register installed.) So you could use ES6 module imports rather than CommonJS requires. But this example will stick to CommonJS because we love you ❀️

const webpack = require('webpack')
const {getIfUtils, removeEmpty} = require('webpack-config-utils')

const {ifProduction, ifNotProduction} = getIfUtils(process.env.NODE_ENV)

module.exports = {
  // ... your config
  mode: ifProduction('production', 'development'),
  entry: removeEmpty({
     app: ifProduction('./indexWithoutCSS', './indexWithCSS'),
     css: ifProduction('./style.scss')
  }),
  output: {
    chunkFilename: ifProduction('js/[id].[contenthash].js', 'js/[name].js'),
    filename: ifProduction('js/[id].[contenthash].js', 'js/[name].js'),
  },
  module: {
    rules: [
      {
        test: /\.(sc|c)ss$/,
        exclude: /node_modules/,
        use: removeEmpty([
          ifProduction(MiniCssExtractPlugin.loader),
          ifNotProduction({loader: 'style-loader', options: {sourceMap: true}}),
          {loader: 'css-loader', options: {sourceMap: true}},
          {loader: 'postcss-loader', options: {sourceMap: true}},
          {loader: 'sass-loader', options: {sourceMap: true}},
        ]),
      },
  },
  plugins: removeEmpty([
    ifProduction(
      new MiniCssExtractPlugin({
        filename: 'css/[id].[contenthash].css',
      })
    ),
    ifProduction(new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"',
      },
    })),
    new HtmlWebpackPlugin({
      template: './index.html',
      inject: 'head',
    }),
    ifProduction(new OfflinePlugin()),
  ]),
}

Then you'd invoke the webpack config with cross-env in your package.json scripts (or with nps):

{
  // your package.json stuff
  scripts: {
    "build:dev": "cross-env NODE_ENV=development webpack",
    "build:prod": "cross-env NODE_ENV=production webpack"
  }
}

Things get even better with webpack 2+ because you can write your webpack config as a function that accepts an env argument which you can set on the command line (which means you don't need cross-env πŸ‘).

const webpack = require('webpack')
const {resolve} = require('path')
const {getIfUtils} = require('webpack-config-utils')

module.exports = env => {
  const {ifDev} = getIfUtils(env)
  return {
    output: {
      // etc.
      pathinfo: ifDev(),
      path: resolve(__dirname, 'dist'),
    },
    // etc.
  }
}

API

See the API Documentation here. In addition to custom utilities from this package, it comes bundled with a few another helpful utility: webpack-combine-loaders (exposed as combineLoaders).

Articles

Contributors

Thanks goes to these people (emoji key):

Kent C. Dodds
Kent C. Dodds

πŸ’» πŸ“– πŸ’‘ πŸš‡ ⚠️
Breno Calazans
Breno Calazans

πŸ’‘
Tamara Temple
Tamara Temple

πŸ“–
Ben Halverson
Ben Halverson

πŸ“–
Huy Nguyen
Huy Nguyen

πŸ’» πŸ“– πŸ’‘ ⚠️
Ryan Johnson
Ryan Johnson

πŸ“ πŸ“–
Adam DiCarlo
Adam DiCarlo

πŸ“– πŸ”§
Jeremy Y
Jeremy Y

πŸ“–
Sean Yesmunt
Sean Yesmunt

πŸ“–

This project follows the all-contributors specification. Contributions of any kind welcome!

LICENSE

MIT

webpack-config-utils's People

Contributors

adamdicarlo avatar huy-nguyen avatar ashiknesin avatar benhalverson avatar bzalasky avatar klzns avatar cs-miller avatar jezzay avatar jdorfman avatar nmaves avatar rrag avatar ryandrewjohnson avatar tamouse avatar vernondegoede avatar allcontributors[bot] avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.