-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
155 lines (147 loc) · 4.27 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const path = require('path');
const HtmlwebpackPlugin = require('html-webpack-plugin');
const merge = require('webpack-merge');
const webpack = require('webpack');
const CleanPlugin = require('clean-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const pkg = require('./package.json');
const TARGET = process.env.npm_lifecycle_event;
const PATHS = {
app: path.join(__dirname, 'app'),
build: path.join(__dirname, 'build'),
style: path.join(__dirname, 'app/main.css')
};
const ENV = {
host: process.env.HOST || 'localhost',
port: process.env.PORT || 8080
};
process.env.BABEL_ENV = TARGET;
const common = {
entry: {
app: PATHS.app
},
resolve: {
extensions: ['.js', '.jsx']
},
output: {
path: PATHS.build,
filename: '[name].js'
},
module: {
rules: [
{
test: /\.jsx?$/,
use: [{
loader: 'babel-loader',
options: {
cacheDirectory: true
}
}],
include: PATHS.app
}
]
},
plugins: [
new HtmlwebpackPlugin({
template: path.join(PATHS.app, 'index.html'),
path: PATHS.build,
appMountId: 'app',
filename: 'index.html',
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
]
};
if(TARGET === 'start' || !TARGET) {
module.exports = merge(common, {
entry: {
style: PATHS.style
},
devtool: 'eval-source-map',
devServer: {
historyApiFallback: true,
hot: true,
inline: 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: ENV.host,
port: ENV.port
},
module: {
rules: [
// Define development specific CSS setup
{
test: /\.css$/,
use: [{
loader: 'style-loader'
}, {
loader: 'css-loader'
}],
include: PATHS.app
}
]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.LoaderOptionsPlugin({
minimize: true
})
]
});
}
if(TARGET === 'build' || TARGET === 'stats') {
module.exports = merge(common, {
entry: {
vendor: Object.keys(pkg.dependencies).filter(function(v) {
// Exclude alt-utils as it won't work with this setup
// due to the way the package has been designed
// (no package.json main).
return v !== 'alt-utils';
}),
style: PATHS.style
},
output: {
path: PATHS.build,
filename: '[name].[chunkhash].js',
chunkFilename: '[chunkhash].js'
},
module: {
rules: [
// Extract CSS during build
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style',
use: 'css'
}),
include: PATHS.app
}
]
},
plugins: [
new CleanPlugin([PATHS.build]),
// Output extracted CSS to a file
new ExtractTextPlugin('styles.[chunkhash].css'),
// Extract vendor and manifest files
new webpack.optimize.CommonsChunkPlugin({
names: ['vendor', 'manifest']
}),
// Setting DefinePlugin affects React library size!
new webpack.DefinePlugin({
'process.env.NODE_ENV': '"production"'
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
},
sourceMap: true
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
]
});
}