ericclemmons / terse-webpack

Simplified, fluent Webpack API with presets. Describe *what* your app needs, not *how*.
MIT License
215 stars 15 forks source link

@terse/webpack

Join the chat at https://gitter.im/ericclemmons/terse-webpack

travis build version downloads MIT License

Webpack simplified in a fluent API with presets & an escape hatch so you're not locked in.


Why?

Why Not?

How?

This project attempts to solve these problems via:

Upcoming features:

Example

// webpack.config.js
module.exports = require("@terse/webpack").api()
  .entry("./src/client.js")
  .loader("babel", ".js", {
    exclude: /node_modules/,
    query: { cacheDirectory: true },
  })
  .modules("./lib")
  .output("build/client")
  .target("web")
  .when("development", function(api) {
    return api
      .entry({
        client: [
          "webpack-hot-middleware/client?reload=true&timeout=2000",
          "./src/client.js",
        ],
      })
      .plugin("npm-install-webpack-plugin")
      .plugin("webpack.HotModuleReplacementPlugin")
      .plugin("webpack.NoErrorsPlugin")
      .sourcemap("source-map")
    ;
  })
  .getConfig()
;
  1. Generates client.js in the build directory from ./src/client.js.
  2. Parses .js files (excluding node_modules) with Babel.
  3. Searches node_modules and ./lib for non-relative files.
  4. Outputs assets to ./build/client.
  5. Target platform is web (vs. node).
  6. When NODE_ENV is development: a. Override client.js to support HMR. b. Add npm-install-webpack-plugin to auto-install missing dependencies. c. Add HMR support. d. Enable source-map-style source maps (e.g. .map files).

Dependencies

I recommend using this against the latest Node + NPM v3.

Installation

npm install @terse/webpack

Usage

View the example.

Replace the contents of webpack.config.js (and others) with:

  module.exports = require("@terse/webpack").api()
    ...
    .getConfig()
  ;

It's crucial to call .getConfig() to return the Webpack configuration.

With the upcoming .presets, it'll be as simple as:

module.exports = require("@terse.webpack").api()
  .presets("autoinstall", "hot", "react", "offline")
  .target("web")
  .getConfig()
;

API

License

MIT License 2016 © Eric Clemmons