-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwebpack.config.babel.js
252 lines (228 loc) · 6.05 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
const fs = require('fs')
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const BrowserSyncPlugin = require('browser-sync-webpack-plugin')
const StatsWriterPlugin = require('webpack-stats-plugin').StatsWriterPlugin
const VisualizerPlugin = require('webpack-visualizer-plugin')
const DuplicatePackageCheckerPlugin = require('duplicate-package-checker-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CompressionPlugin = require('compression-webpack-plugin')
const PostcssAssetsPlugin = require('postcss-assets-webpack-plugin')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const autoprefixer = require('autoprefixer')
const mqpacker = require('css-mqpacker')
const cssnano = require('cssnano')
const LAUNCH_COMMAND = process.env.npm_lifecycle_event
const isProduction = LAUNCH_COMMAND === 'build'
process.env.BABEL_ENV = LAUNCH_COMMAND
const WWW_HOST = process.env.WWW_HOST || 'local.uxscoreboard'
const WWW_PORT = process.env.WWW_PORT || 8888
const API_HOST = process.env.API_HOST || 'local.api.uxscoreboard'
const API_PORT = process.env.API_PORT || 9999
const WWW_PROXY = `https://${WWW_HOST}:${WWW_PORT}`
const API_PROXY = `https://${API_HOST}:${API_PORT}`
const PATHS = {
app: path.join(__dirname, './src'),
dist: path.join(__dirname, './dist')
}
const globalVariables = new webpack.DefinePlugin({
__DEV__: !isProduction
})
const htmlWebpackPlugin = new HtmlWebpackPlugin({
template: PATHS.app + '/index.html',
filename: 'index.html'
})
const browserSyncPlugin = new BrowserSyncPlugin(
{
host: WWW_HOST,
port: WWW_PORT,
proxy: WWW_PROXY,
open: false,
ui: { port: WWW_PORT, weinre: { port: API_PORT } }
},
{ reload: false }
)
const postcssPlugin = new webpack.LoaderOptionsPlugin({
options: {
context: PATHS.app,
postcss: [autoprefixer({ remove: false, browsers: ['last 2 versions'] })]
}
})
const postcssAssetsPlugin = new PostcssAssetsPlugin({
test: /\.css$/,
log: false,
plugins: [mqpacker({ sort: true }), cssnano]
})
const statsWriterPlugin = new StatsWriterPlugin({
filename: './assets/build/stats/webpack_stats.json',
fields: null,
stats: { chunkModules: true }
})
const visualizerPlugin = new VisualizerPlugin({
filename: './assets/build/stats/webpack_stats.html'
})
const duplicatePackageCheckerPlugin = new DuplicatePackageCheckerPlugin({
verbose: true
})
const miniCssExtractPlugin = new MiniCssExtractPlugin({
filename: './assets/build/styles.[contenthash:12].bundle.css'
})
const compressionPlugin = new CompressionPlugin({
filename: '[path].gz[query]',
algorithm: 'gzip',
test: /\.(js|css)$/,
threshold: 1024,
minRatio: 0.8
})
const uglifyJsPlugin = new UglifyJsPlugin({
sourceMap: true,
uglifyOptions: {
ecma: 8,
comments: false,
mangle: true,
minimize: true,
beautify: false,
compress: { warnings: false }
}
})
const productionPlugin = new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
})
const sharedPlugins = [
globalVariables,
htmlWebpackPlugin,
miniCssExtractPlugin,
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
]
const sharedCssLoaders = [
{
loader: 'css-loader',
options: {
sourceMap: true,
modules: true,
minimize: true,
localIdentName: '[name]_[local]___[hash:base64:5]',
importLoaders: 1
}
},
{
loader: 'postcss-loader',
options: isProduction
? {
ident: 'postcss',
plugins: () => [require('autoprefixer')]
}
: {}
},
{ loader: 'sass-loader', options: { implementation: require('sass') } },
{
loader: 'sass-resources-loader',
options: {
resources: [path.resolve(PATHS.app, './styles/_variables.scss')]
}
}
]
const base = {
output: {
path: PATHS.dist,
filename: isProduction ? 'assets/build/app.[hash:12].js' : 'app.js',
chunkFilename: isProduction
? 'assets/build/[name].[chunkhash:12].js'
: '[name].js',
publicPath: '/'
},
module: {
rules: [
{
test: /\.(tsx?)|(js)$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
isProduction
? {
test: /\.(scss)|(css)$/,
use: [MiniCssExtractPlugin.loader, ...sharedCssLoaders]
}
: {
test: /\.(scss)|(css)$/,
use: [{ loader: 'style-loader' }, ...sharedCssLoaders]
}
]
},
resolve: {
modules: [PATHS.app, 'node_modules'],
extensions: ['.ts', '.tsx', '.js']
},
node: {
console: true,
fs: 'empty',
net: 'empty',
tls: 'empty'
},
optimization: {
splitChunks: {
chunks: 'all'
}
}
}
const devConfig = {
entry: [
'react-hot-loader/patch',
`webpack-dev-server/client?${WWW_PROXY}`,
'webpack/hot/only-dev-server',
PATHS.app
],
devtool: 'cheap-module-inline-source-map',
mode: 'development',
devServer: {
contentBase: PATHS.dist,
publicPath: '/',
hot: true,
inline: true,
compress: true,
historyApiFallback: true,
host: WWW_HOST,
port: WWW_PORT,
https: {
cert: fs.readFileSync(`./.ssl/${WWW_HOST}.cert`),
key: fs.readFileSync(`./.ssl/${WWW_HOST}.key`)
},
disableHostCheck: true,
proxy: {
'/api/**': {
target: API_PROXY,
secure: false
}
}
},
plugins: [
...sharedPlugins,
...(process.env.BROWSER_SYNC ? browserSyncPlugin : []),
duplicatePackageCheckerPlugin,
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin()
]
}
const prodConfig = {
entry: [PATHS.app],
devtool: 'cheap-module-source-map',
mode: 'production',
plugins: [
...sharedPlugins,
productionPlugin,
compressionPlugin,
uglifyJsPlugin,
postcssPlugin,
postcssAssetsPlugin,
...(process.env.WEBPACK_STATS ? [statsWriterPlugin, visualizerPlugin] : []),
new webpack.optimize.AggressiveMergingPlugin(),
new webpack.optimize.OccurrenceOrderPlugin()
]
}
module.exports = {
...base,
...(isProduction ? prodConfig : devConfig)
}