Created
February 8, 2017 09:01
-
-
Save guimochila/a220bf5253160d7f86a1fe68db7f4e98 to your computer and use it in GitHub Desktop.
Webpack configuration sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const webpack = require('webpack'); | |
const path = require('path'); | |
const ExtractTextPlugin = require('extract-text-webpack-plugin'); | |
const nodeEnv = process.env.NODE_ENV || 'development'; | |
const isProd = nodeEnv === 'production'; | |
const sourcePath = path.join(__dirname, './client'); | |
const staticsPath = path.join(__dirname, './static'); | |
const extractCSS = new ExtractTextPlugin('style.css'); | |
const plugins = [ | |
new webpack.optimize.CommonsChunkPlugin({ | |
name: 'vendor', | |
minChunks: Infinity, | |
filename: 'vendor.bundle.js' | |
}), | |
new webpack.DefinePlugin({ | |
'process.env': { NODE_ENV: JSON.stringify(nodeEnv) } | |
}), | |
]; | |
if (isProd) { | |
plugins.push( | |
new webpack.LoaderOptionsPlugin({ | |
minimize: true, | |
debug: false | |
}), | |
new webpack.optimize.UglifyJsPlugin({ | |
compress: { | |
warnings: false, | |
screw_ie8: true, | |
conditionals: true, | |
unused: true, | |
comparisons: true, | |
sequences: true, | |
dead_code: true, | |
evaluate: true, | |
if_return: true, | |
join_vars: true, | |
}, | |
output: { | |
comments: false | |
}, | |
}), | |
extractCSS | |
); | |
} | |
module.exports = { | |
devtool: isProd ? 'source-map' : 'eval', | |
context: sourcePath, | |
entry: { | |
js: [ | |
'index', | |
'pages/Home' | |
], | |
vendor: [ | |
'react', | |
'react-dom' | |
] | |
}, | |
output: { | |
path: staticsPath, | |
filename: 'bundle.js', | |
publicPath: '/', | |
}, | |
module: { | |
rules: [ | |
{ | |
test: /\.html$/, | |
use: 'file-loader', | |
query: { | |
name: '[name].[ext]' | |
} | |
}, | |
{ | |
test: /\.scss$/, | |
use: isProd ? | |
extractCSS.extract({ | |
fallbackLoader: 'style-loader', | |
loader: ['css-loader', 'sass-loader'], | |
}) : | |
['style-loader', 'css-loader', 'sass-loader'] | |
}, | |
{ | |
test: /\.(js|jsx)$/, | |
exclude: /node_modules/, | |
use: [ | |
{ | |
loader: 'babel-loader', | |
query: { | |
cacheDirectory: true | |
} | |
} | |
] | |
}, | |
{ | |
test: /\.(gif|png|jpg|jpeg\ttf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/, | |
use: 'file-loader' | |
} | |
], | |
}, | |
resolve: { | |
extensions: ['.js', '.jsx'], | |
modules: [ | |
sourcePath, | |
'node_modules' | |
] | |
}, | |
plugins: plugins, | |
devServer: { | |
contentBase: './client', | |
historyApiFallback: true, | |
port: 3000, | |
compress: isProd, | |
stats: { colors: true }, | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment