Skip to content

Instantly share code, notes, and snippets.

@mactive
Created November 15, 2016 02:23
Show Gist options
  • Save mactive/ff2066de7affe91cd3551fd471675458 to your computer and use it in GitHub Desktop.
Save mactive/ff2066de7affe91cd3551fd471675458 to your computer and use it in GitHub Desktop.
webpack config
/**
* React Static Boilerplate
* https://github.com/koistya/react-static-boilerplate
* Copyright (c) Konstantin Tarkus (@koistya) | MIT license
*/
import path from 'path';
import webpack from 'webpack';
import merge from 'lodash.merge';
const DEBUG = !process.argv.includes('production');
const VERBOSE = process.argv.includes('verbose');
const WATCH = global.watch;
const AUTOPREFIXER_BROWSERS = [
'Android 2.3',
'Android >= 4',
'Chrome >= 35',
'Firefox >= 31',
'Explorer >= 9',
'iOS >= 7',
'Opera >= 12',
'Safari >= 7.1',
];
const JS_LOADER = {
test: /\.jsx?$/,
include: [
path.resolve(__dirname, '../components'),
path.resolve(__dirname, '../core'),
path.resolve(__dirname, '../static/js'),
path.resolve(__dirname, '../pages'),
path.resolve(__dirname, '../app.js'),
path.resolve(__dirname, '../config.js'),
],
loader: 'babel-loader',
};
const outputPath = path.join(__dirname, '../build');
// Base configuration
const config = {
output: {
path: outputPath,
publicPath: '',
sourcePrefix: ' ',
},
cache: true,
debug: DEBUG,
stats: {
colors: true,
reasons: DEBUG,
hash: VERBOSE,
version: VERBOSE,
timings: true,
chunks: VERBOSE,
chunkModules: VERBOSE,
cached: VERBOSE,
cachedAssets: VERBOSE,
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.DefinePlugin({
'process.env.NODE_ENV': DEBUG ? '"development"' : '"production"',
'__DEV__': DEBUG,
}),
],
module: {
loaders: [
{
test: /[\\\/]app\.js$/,
loader: path.join(__dirname, './lib/routes-loader.js'),
}, {
test: /\.json$/,
loader: 'json-loader',
}, {
test: /\.txt$/,
loader: 'raw-loader',
}, {
test: /\.(png|jpg|jpeg|gif|svg|woff|woff2)$/,
loader: 'url-loader?limit=10000',
}, {
test: /\.(eot|ttf|wav|mp3)$/,
loader: 'file-loader',
}, {
test: /\.scss$/,
loaders: [
'isomorphic-style-loader',
'css-loader?modules&localIdentName=[name]_[local]_[hash:base64:3]',
'postcss-loader'
],
},
],
},
postcss: function plugins(bundler) {
return [
require('postcss-import')({addDependencyTo: bundler}),
require('precss')(),
require('autoprefixer')({
browsers: AUTOPREFIXER_BROWSERS,
}),
];
},
};
// Configuration for the client-side bundle
const appConfig = merge({}, config, {
entry: [
...(WATCH ? ['webpack-hot-middleware/client'] : []),
'./app.js',
],
output: {
filename: 'app.js',
buildPath: outputPath,
path: path.join(outputPath, '/static/js'),
publicPath: '/static/js/',
},
// http://webpack.github.io/docs/configuration.html#devtool
devtool: DEBUG ? 'cheap-module-eval-source-map' : false,
plugins: [
...config.plugins,
...(DEBUG ? [] : [
new webpack.optimize.LimitChunkCountPlugin({maxChunks: 1}),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: VERBOSE,
},
}),
]),
...(WATCH ? [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
] : []),
],
module: {
loaders: [
WATCH ? Object.assign({}, JS_LOADER, {
query: {
// Wraps all React components into arbitrary transforms
// https://github.com/gaearon/babel-plugin-react-transform
plugins: ['react-transform'],
extra: {
'react-transform': {
transforms: [
{
transform: 'react-transform-hmr',
imports: ['react'],
locals: ['module'],
}, {
transform: 'react-transform-catch-errors',
imports: ['react', 'redbox-react'],
},
],
},
},
},
}) : JS_LOADER,
...config.module.loaders,
],
},
});
// Configuration for server-side pre-rendering bundle
const pagesConfig = merge({}, config, {
entry: './app.js',
output: {
filename: 'app.node.js',
libraryTarget: 'commonjs2',
path: path.join(outputPath, '/static/js'),
publicPath: '/static/js/',
},
target: 'node',
node: {
console: false,
global: false,
process: false,
Buffer: false,
__filename: false,
__dirname: false,
},
externals: /^[a-z][a-z\.\-\/0-9]*$/i,
plugins: config.plugins.concat([
new webpack.optimize.LimitChunkCountPlugin({maxChunks: 1}),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: VERBOSE,
},
}),
]),
module: {
loaders: [
JS_LOADER,
...config.module.loaders,
],
},
});
export default [appConfig, pagesConfig];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment