Add webpack-configurator which enables passing an object to a
gatsby.config.js. This object has a few convenience functions for
managing loaders as well as plugins (and arbitrary keys) relating to
webpack configuration.
* Move webpack config to JS
* Allows consumers to modify loaders and plugins based on stage.
Here is a webpack config which uses the new extensibility to add
css-modules, classnames and postcss support to a static site.
```javascript
var ExtractTextPlugin = require("extract-text-webpack-plugin");
function configureDevelopment(config) {
config.removeLoader('css');
config.loader('css', function(cfg) {
cfg.test = /\.css$/;
cfg.loader = 'style!css?modules!postcss'
return cfg
})
config.merge({
postcss: [
require('postcss-import')(),
require('postcss-cssnext')({
browsers: 'last 2 versions'
})
]
})
}
function configureForJSBundle(config) {
config.removeLoader('css');
config.loader('css', function(cfg) {
cfg.test = /\.css$/;
cfg.loader = 'css/locals?modules!postcss'
return cfg
})
}
function configureForHTMLGeneration(config) {
config.removeLoader('css');
config.loader('css', function(cfg) {
cfg.test = /\.css$/;
cfg.loader = ExtractTextPlugin.extract('style', 'css?modules&minimize!postcss');
return cfg
})
config.plugin('extract-css',
ExtractTextPlugin,
["styles.css"]);
config.merge({
postcss: [
require('postcss-import')(),
require('postcss-cssnext')({
browsers: 'last 2 versions'
})
]
})
}
module.exports = function(config, env) {
switch(env) {
case 'production':
configureForJSBundle(config);
break;
case 'static':
configureForHTMLGeneration(config);
break;
default:
configureDevelopment(config);
break;
}
return config;
}
```
Given `pages/_template.jsx` which imports and uses the following
`pages/_template.css`:
```css
:root {
--black: black;
}
.header {
background: var(--black);
}
```
When running `gatsby build`, All css is extracted and turned into a
minified `styles.css`:
```css
._2D1e_Piibk-xdIwKzGpKuS{background:#000}
```
The bundle.js and static html files have a div that looks like:
```html
<div class="_2D1e_Piibk-xdIwKzGpKuS">
...
</div>
```
When running `gatsby develop`, the styles are included in the <head>,
unminified:
```html
<style type="text/css">._2D1e_Piibk-xdIwKzGpKuS {
background: black;
}</style>
```