Skip to content

Instantly share code, notes, and snippets.

@wkf
Created June 13, 2016 18:12
Show Gist options
  • Save wkf/d81f03a6409baf2a9cafe0abc25b7640 to your computer and use it in GitHub Desktop.
Save wkf/d81f03a6409baf2a9cafe0abc25b7640 to your computer and use it in GitHub Desktop.
const path = require('path');
const merge = require('webpack-merge');
const webpack = require('webpack');
const cssnext = require('postcss-cssnext');
const cssreporter = require('postcss-reporter');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const StaticSiteGeneratorPlugin = require('static-site-generator-webpack-plugin');
const PATHS = {
bundle: path.join(__dirname, 'src', 'bundle', 'index'),
static: path.join(__dirname, 'src', 'static', 'index'),
public: path.join(__dirname, 'public')
};
const common = {
output: {
path: PATHS.public,
filename: '[name].js',
library: '[name]',
libraryTarget: 'umd',
umdNamedDefine: true
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel',
exclude: /node_modules/,
query: {
presets: ["es2015"],
plugins: ["transform-object-rest-spread"]
}
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract('style-loader', 'css-loader!postcss-loader')
},
{test: /\.json$/, loader: 'json'},
{test: /\.html$/, loader: 'html'},
{test: /\.md$/, loader: 'html!markdown'}
]
},
postcss: function() {
return [cssnext, cssreporter];
},
plugins:[
new ExtractTextPlugin('main.css')
]
};
const static = merge(common, {
entry: {
static: PATHS.static
},
target: 'node',
plugins: [
new StaticSiteGeneratorPlugin('static', ['/'])
]
});
const bundle = merge(common, {
entry: {
bundle: PATHS.bundle
}
});
const configs = {
start: [static, bundle],
watch: [static, merge(bundle, {
entry: {
bundle: [PATHS.bundle, 'webpack-dev-server/client?http://localhost:8081']
},
// HACK:
// the socket.io client library references these two libs...
// ...and webpack doesn't like it, as we're targeting the browser
resolve: {
aliases: {
fs: false,
net: false
}
}
// XXX:
// webpack-dev-server doesn't pick up config when returning an array...
// ...so we have to put it in the npm watch script directly
// ,
// devServer: {
// host: 'localhost',
// port: 8081,
// contentBase: PATHS.public
// }
})],
build: [static, merge(bundle, {
output: {
filename: 'bundle.js'
},
plugins: [
new webpack.optimize.UglifyJsPlugin()
]
})]
};
module.exports = configs[process.env.npm_lifecycle_event] || configs.watch;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment