Skip to content

Instantly share code, notes, and snippets.

@cungen
Created March 4, 2016 06:24
Show Gist options
  • Save cungen/93770545f85e66627265 to your computer and use it in GitHub Desktop.
Save cungen/93770545f85e66627265 to your computer and use it in GitHub Desktop.
webpack config generator
var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
function makeWebpackConfig(options) {
var BUILD = !!options.BUILD;
var config = {};
/**
* Dev tool
*/
if (BUILD) {
config.devtool = 'source-map';
} else {
config.devtool = 'eval';
}
config.entry = {
app: './client/index',
vendors: ['react']
};
config.output = {
path: './dist',
filename: '[name].js',
publicPath: BUILD ? '/dist' : 'http://localhost:8080/dist',
chunkFilename: '[name].js'
};
// initialize module
config.module = {
preLoaders: [],
loaders: [{
test: /\.js$/,
loaders: ['react-hot', 'babel?presets[]=react,presets[]=es2015'],
exclude: /(node_modules|bower_components)/
}, {
test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)$/,
loader: 'file'
}, {
test: /\.html$/,
loader: 'raw'
}]
};
config.module.loaders.push({
test: /\.css$/,
loader: ExtractTextPlugin.extract('style', 'css')
});
config.plugins = [
new ExtractTextPlugin('[name].css', {
disable: !BUILD
})
];
config.plugins.push(
new HtmlWebpackPlugin({
template: './client/index.html',
inject: 'body',
minify: false
})
);
config.plugins.push(
new webpack.optimize.CommonsChunkPlugin({
filename: 'common.js',
name: 'common'
})
);
if (BUILD) {
config.plugins.push(
// Reference: http://webpack.github.io/docs/list-of-plugins.html#noerrorsplugin
// Only emit files when there are no errors
new webpack.NoErrorsPlugin(),
// Reference: http://webpack.github.io/docs/list-of-plugins.html#dedupeplugin
// Dedupe modules in the output
new webpack.optimize.DedupePlugin(),
// Reference: http://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin
// Minify all javascript, switch loaders to minimizing mode
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
})
)
}
config.devServer = {
contentBase: '.',
stats: {
modules: false,
cached: true,
colors: true,
chunk: false
}
};
return config;
}
module.exports = makeWebpackConfig;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment