-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.config.js
129 lines (114 loc) · 3.2 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
require('babel-polyfill'); //for Object.assign support
const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
//get NPM target
const TARGET = process.env.npm_lifecycle_event;
//make target available to babel
process.env.BABEL_ENV = TARGET;
//setup common paths
const PATHS = {
app: path.join(__dirname, 'app'),
build: path.join(__dirname, 'build'),
hotStatic: '/static/'
};
//flag to determine dev or prod build status
const isDevBuild = (TARGET === 'start' || !TARGET);
//decide whether to use the react-hot loader or not
const jsxLoaders = isDevBuild ? ['react-hot', 'babel?cacheDirectory=babel-cache'] : ['babel?cacheDirectory=babel-cache'];
const common = {
resolve: {
extensions: ['', '.js', '.jsx']
},
plugins: [
new ExtractTextPlugin('./appstyles.css', {
allChunks: true,
disable: isDevBuild
})
],
module: {
preLoaders: [
{
test: /\.jsx?$/,
loaders: ['eslint-loader'],
include: PATHS.app
}
],
loaders: [
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract('style-loader', ['css-loader', 'postcss-loader','sass-loader'])
},
{
// Set up jsx. This accepts js too thanks to RegExp
test: /\.jsx?$/,
// Enable caching for improved performance during development
loaders: jsxLoaders,
exclude: /(node_modules)/,
include: PATHS.app
},
{
test: /\.(png|jpg|gif|woff|woff2|eot|ttf)$/,
loader: 'url-loader?limit=8192'
},
{
test: /\.(mp4|ogg|svg)$/,
loader: 'file-loader'
}
]
}
};
// Default configuration. We will return this if
// Webpack is called outside of npm.
//DEVELOPMENT
if(isDevBuild) {
common.plugins.push(new webpack.HotModuleReplacementPlugin());
module.exports = Object.assign(common, {
entry: [
'webpack-dev-server/client?http://0.0.0.0:8080', // WebpackDevServer host and port
'webpack/hot/only-dev-server', // "only" prevents reload on syntax errors
PATHS.app
],
output: {
path: PATHS.build,
filename: 'bundle.js',
publicPath: '/static/'
},
devtool: 'eval-source-map',
devServer: {
contentBase: PATHS.build,
// Enable history API fallback so HTML5 History API based
// routing works.
historyApiFallback: true,
hot: true,
publicPath: PATHS.hotStatic,
inline: true,
progress: true,
// Display only errors to reduce the amount of output.
stats: 'errors-only',
// Parse host and port from env so this is easy to customize.
host: process.env.HOST,
port: process.env.PORT
}
});
}
//Running build target
//PRODUCTION
if(TARGET === 'build') {
//set production environment
common.plugins.push(new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
}));
//optimization plugins
common.plugins.push(new webpack.optimize.UglifyJsPlugin());
common.plugins.push(new webpack.optimize.DedupePlugin());
module.exports = Object.assign(common, {
entry: [
PATHS.app
],
output: {
path: PATHS.build,
filename: 'bundle.js'
}
});
}