-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
95 lines (86 loc) · 2.49 KB
/
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
const path = require('path')
const fs = require('fs')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const env = process.env.NODE_ENV || 'development'
const directory = process.cwd()
const modulesDirectories = [
`${directory}/node_modules`,
`${__dirname}/node_modules`,
'node_modules'
]
const resolveBabelPackages = pkgs => {
return pkgs.map(pkg => {
const modulePath = fs.existsSync(path.join(__dirname, 'node_modules', pkg)) ? 'node_modules' : '../../node_modules'
return path.resolve(__dirname, modulePath, pkg)
})
}
const config = {
entry: ['webpack-hot-middleware/client', './src'],
output: {
path: path.join(directory, 'public'),
filename: '[name].js',
publicPath: '/'
},
resolveLoader: {modulesDirectories},
resolve: {modulesDirectories},
devtool: 'cheap-module-eval-source-map',
module: {
loaders: [
{
test: /\.jsx?$/, loader: 'babel',
exclude: /(node_modules|bower_components)/,
query: {
presets: resolveBabelPackages(['babel-preset-react', 'babel-preset-es2015', 'babel-preset-stage-0']),
plugins: resolveBabelPackages(['babel-plugin-transform-decorators-legacy']),
env: {
development: {
presets: resolveBabelPackages(['babel-preset-react-hmre'])
}
}
}
},
]
},
plugins: [
new webpack.DefinePlugin({
'process.env': { NODE_ENV: JSON.stringify(env) }
}),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.NoErrorsPlugin(),
new HtmlWebpackPlugin({
template: `${__dirname}/index.html`,
hash: true,
inject: 'body'
})
]
}
// production
if (env == 'production') {
config.devtool = 'source-map'
config.plugins.push(
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin({
compressor: { warnings: false },
output: { comments: false }
})
)
} else {
config.plugins.push(new webpack.HotModuleReplacementPlugin())
}
// merge in user-configs
// accepts {} or function(config)
const userConfig = config => {
const configPath = path.resolve(directory, './webpack.config.js')
if (fs.existsSync(configPath)) {
const userWebpackConfig = require(configPath)
if (typeof userWebpackConfig === 'function') {
return userWebpackConfig(config)
} else {
return Object.assign(config, userWebpackConfig)
}
}
return config
}
const finalConfig = userConfig(config)
module.exports = finalConfig