-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.babel.js
173 lines (158 loc) · 4.88 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
/*
*
* .d8888b. .d888 d8b
* d88P Y88b d88P' Y8P
* 888 888 888
* 888 .d88b. 88888b. 888888 888 .d88b.
* 888 d88''88b 888 '88b 888 888 d88P'88b
* 888 888 888 888 888 888 888 888 888 888
* Y88b d88P Y88..88P 888 888 888 888 Y88b 888
* 'Y8888P' 'Y88P' 888 888 888 888 'Y88888
* 888
* Y8b d88P
* 'Y88P'
*
*/
import path from 'path'
import chalk from 'chalk'
import webpack from 'webpack'
import MinifyPlugin from 'babel-minify-webpack-plugin'
import HtmlWebpackPlugin from 'html-webpack-plugin'
const LAUNCH_COMMAND = process.env.npm_lifecycle_event
const isProd = LAUNCH_COMMAND === 'prod'
const isLocal = LAUNCH_COMMAND === 'local'
const PATHS = {
src : path.join(__dirname, 'src'),
build : path.join(__dirname, 'build'),
nModules : path.join(__dirname, 'node_modules'),
utils : path.join(__dirname, 'src/utils'),
config : path.join(__dirname, 'src/config'),
components : path.join(__dirname, 'src/components'),
demos : path.join(__dirname, 'src/components/demos'),
testUtils : path.join(__dirname, '__tests__/utils'),
}
// Plugins Configuration Starts
const moduleConcatenationPlugin = new webpack.optimize.ModuleConcatenationPlugin()
const prodPlugin = new webpack.DefinePlugin({
'process.env' : {
NODE_ENV : JSON.stringify('production')
}
})
// const commonsVendorChunk = new webpack.optimize.CommonsChunkPlugin({
// name : 'vendor',
// minChunks : Infinity,
// filename : 'vendor.commons.js'
// })
const envInfo = new webpack.DefinePlugin({
'env' : {
isProd,
isDev : LAUNCH_COMMAND === 'start',
command : JSON.stringify(LAUNCH_COMMAND)
}
})
const minifyPlugin = new MinifyPlugin({}, {
test : /\.(js|jsx)$/,
include : PATHS.src,
exclude : [/bundle\.js|coverage|node_modules/]
})
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
template : `${PATHS.src}/index.html`,
filename : 'index.html',
inject : 'body'
})
// Plugins Configuration Ends
process.env.BABEL_ENV = LAUNCH_COMMAND
process.env.LINT_ENV = LAUNCH_COMMAND
process.env.isProd = isProd
if (isLocal){
console.log(chalk.blue('******************************************'))
console.log(chalk.blue('Runnin in Local Mode!'))
console.log(chalk.blue('******************************************'))
} else {
console.log(chalk.green('---------------------------------------------------------------'))
console.log(chalk.green(`-------- Running Application In ${isProd ? 'Production' : 'Development'} Environment -------`))
console.log(chalk.green('---------------------------------------------------------------'))
}
const base = {
entry : path.join(__dirname, 'index.js'),
output : {
path : PATHS.build,
library : 'reactStateForm',
libraryTarget : 'umd',
umdNamedDefine : true,
filename : 'react-state-form.js'
},
externals : {
react : {
commonjs : 'react',
commonjs2 : 'react',
amd : 'React',
root : 'React'
}
},
module : {
rules : [
{
enforce : 'pre',
test : /\.(js|jsx)$/,
use : 'eslint-loader',
include : PATHS.src,
exclude : [/bundle\.js|coverage/]
},
{
test : /\.(js|jsx)$/,
exclude : [/node_modules/],
loader : 'babel-loader'
}
]
},
resolve : {
alias : {
$SRC : PATHS.src,
$BUILD : PATHS.build,
$UTILS : PATHS.utils,
$TEST_UTILS : PATHS.testUtils,
$CONFIG : PATHS.config,
$COMPONENTS : PATHS.components,
$DEMOS : PATHS.demos,
react : path.join(PATHS.nModules, 'react')
},
enforceExtension : false,
extensions : ['.js', '.jsx'],
modules : [path.resolve('.'), 'node_modules']
},
context : PATHS.src
}
if (isLocal) {
base.entry = [
'@babel/polyfill',
path.join(__dirname, 'src/index.jsx')
]
base.output = {
path : PATHS.build,
filename : 'bundle.js',
publicPath : '/',
}
delete base.externals
}
const localPlugins = [HtmlWebpackPluginConfig]
const commonPlugins = [moduleConcatenationPlugin, envInfo]
const devConfig = {
devtool : 'cheap-module-eval-source-map',
devServer : {
compress : true,
historyApiFallback : true,
clientLogLevel : 'info',
open : true,
overlay : {
warnings : isProd,
errors : true
}
},
plugins : isLocal ? commonPlugins.concat(localPlugins) : commonPlugins
}
const prodConf = {
devtool : false,
plugins : commonPlugins.concat([prodPlugin, minifyPlugin])
}
export default Object.assign({}, base, isProd ? prodConf : devConfig)