-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.babel.js
273 lines (238 loc) · 6.79 KB
/
webpack.config.babel.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* eslint-disable */
import autoprefixer from 'autoprefixer'
import precss from 'precss'
import ExtractTextPlugin from 'extract-text-webpack-plugin'
import Clean from 'clean-webpack-plugin'
import path from 'path'
import webpack from 'webpack'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import StringReplacePlugin from 'string-replace-webpack-plugin'
import Immutable from 'immutable'
const Im = Immutable.fromJS
const production = process.env.NODE_ENV === 'production'
const build = process.env.BUILD_ENV === 'build'
const staticPath = '/static/adminfrontend/'
class WebPackConfig {
getStaticKeys() {
let keys = Im(Object.keys(this.constructor))
const ParentClass = this.__proto__.__proto__.constructor
if (new ParentClass() instanceof WebPackConfig) {
keys = keys.concat(Im(new ParentClass().getStaticKeys()))
}
return keys
}
buildConfig() {
let config = {}
const keys = this.getStaticKeys()
Im(keys).forEach((key) => {
let value = this.constructor[key]
if (value instanceof Immutable.Map || value instanceof Immutable.List) {
config[key] = value.toJS()
}
else {
config[key] = value
}
})
return config
}
static node = Im({ net: 'empty', tls: 'empty', dns: 'empty' })
static target = 'web'
static entry = Im({
main: './src/index.js',
vendor: [
'babel-polyfill',
'immutable',
'react-dom',
'clipboard'
]
})
static output = Im(
{
path: path.join(__dirname, '.' + staticPath),
pathInfo: true,
publicPath: staticPath,
chunkFilename: '[name].js'
})
static eslint = Im({
configFile: './linting/prod.yaml'
})
static module = Im({
preLoaders: [
{ test: /\.jsx?$/,
loader: 'eslint-loader',
exclude: /node_modules|webpack/}
],
loaders: [
{ test: /\.(gif|jpg|png|woff|woff2|eot|ttf|svg)$/,
loader: 'file-loader' },
{ test: /\.jsx?$/, loader: 'babel',
exclude: /node_modules/,
include: path.join(__dirname, 'src')
},
{
test: /\.(styl)$/,
loader: ExtractTextPlugin.extract(
'style-loader',
'css-loader!postcss-loader!stylus-loader'
)
},
{
test: /\.(css)$/,
loader: "style-loader!css-loader"
}
],
noParse: /\.min\.js/
})
static resolve = Im({
extentions: ['', '.js', '.jsx', '.styl', '.css'],
modules_directories: ['./src', 'node_modules']
})
static plugins = Im([
new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.js', Infinity),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
function() {
this.plugin('done', function(stats) {
if (stats.compilation.errors &&
stats.compilation.errors.length &&
process.argv.indexOf('--bail') !== -1) {
for (let i = stats.compilation.errors.length - 1; i >= 0; i--) {
let error = stats.compilation.errors[i]
console.error(error.message)
}
process.exit(1);
}
})
}
])
static postcss = [autoprefixer()]
}
class DevBuild extends WebPackConfig {
static devServer = Im({
headers: { 'Access-Control-Allow-Origin': '*' },
contentBase: '.',
__webpack_public_path__: 'http://localhost:8888',
hot: true,
publicPath: 'http://localhost:8888/build/',
port: 8888,
host: 'localhost',
inline: true,
quiet: false,
noInfo: false,
https: true,
stats: {
assets: true,
colors: true,
version: false,
hash: false,
timings: true,
chunks: false,
chunkModules: false
},
historyApiFallback: true
})
static __webpack_public_path__ = 'http://localhost:8888'
static debug = true
static devtool = 'source-map'
static entry = WebPackConfig.entry.set('main', Im([
'webpack/hot/dev-server',
'./src/index.js'
]))
static output = Im({
path: path.join(__dirname, 'build'),
publicPath: 'http://localhost:8888/build/',
filename: '[name].js'
})
static eslint = Im({
configFile: './linting/dev.yaml'
})
/* eslint-disable max-len */
static module = WebPackConfig.module.setIn(['loaders', 1, 'loaders'],
Im(['react-hot', 'babel-loader'])).setIn(['loaders', 2, 'loader'],
'style-loader!css-loader!postcss-loader!stylus-loader')
/* eslint-enable */
static plugins = WebPackConfig.plugins.concat(Im(
[new webpack.HotModuleReplacementPlugin(), new webpack.NoErrorsPlugin(),
new webpack.DefinePlugin({
'process.env': {
STATIC_PATH: JSON.stringify('http://localhost:8888/build/'),
DEBUG: JSON.stringify(process.env.DEBUG),
NODE_ENV: JSON.stringify(process.env.NODE_ENV)
}
}),]
))
}
class ProductionBuild extends WebPackConfig {
static plugins = WebPackConfig.plugins.concat(Im([
new webpack.DefinePlugin({
'process.env': {
STATIC_PATH: JSON.stringify(staticPath),
DEBUG: JSON.stringify(process.env.DEBUG),
NODE_ENV: JSON.stringify(process.env.NODE_ENV)
}
}),
new webpack.optimize.AggressiveMergingPlugin(),
new Clean(['./static']),
new Clean(['./build']),
new ExtractTextPlugin('styles/app.css', {
allChunks: true
}),
new webpack.optimize.DedupePlugin(),
new StringReplacePlugin(),
new webpack.optimize.UglifyJsPlugin({
minimize: true,
mangle: true,
mangle: {
except: ['require', 'export', '$super']
},
compress: {
warnings: false,
sequences: true,
dead_code: true,
conditionals: true,
booleans: true,
unused: true,
if_return: true,
join_vars: true,
drop_console: true
}
}),
new HtmlWebpackPlugin({
filename: './build/index.html',
template: './src/index_prod.html',
minify: {
// TODO check when preparing for prod
}})
])
)
static module = WebPackConfig.module.set('loaders',
WebPackConfig.module.get('loaders').push(
Im(
{ test: /\.(js|styl)$/, loader:
StringReplacePlugin.replace({
replacements: [
{ pattern: /(['"])\/static\//g,
replacement: function(match, p1) {
return p1 + staticPath
}
} ] })
})
)
)
static bail = true
static debug = true
static profile = true
static output = WebPackConfig.output.set(
'pathInfo', false).set(
'filename', '[name].js').set(
'chunkFilename', '[id].js'
)
static devtool = '#source-map'
}
let config
if (production) {
config = new ProductionBuild().buildConfig()
} else {
config = new DevBuild().buildConfig()
}
module.exports = config