forked from nasa-gibs/worldview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
275 lines (268 loc) · 7.17 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
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
274
275
const path = require('path');
const webpack = require('webpack');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const WriteFilePlugin = require('write-file-webpack-plugin');
const postcssPresetEnv = require('postcss-preset-env');
const postcssNesting = require('postcss-nesting');
// production optimizations
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const cssnano = require('cssnano');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const MomentLocalesPlugin = require('moment-locales-webpack-plugin');
// environment dev flag
const devMode = process.env.NODE_ENV !== 'production';
const isDevServer = process.argv[1].indexOf('webpack-dev-server') !== -1;
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const pluginSystem = [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
hash: true,
title: 'Worldview',
filename: 'web/index.html',
inject: false,
}),
new MiniCssExtractPlugin({
filename: 'wv.css',
}),
new WriteFilePlugin(),
new MomentLocalesPlugin(),
];
/* Conditional Plugin Management */
// add hot module replacement
if (isDevServer) {
pluginSystem.push(
new webpack.HotModuleReplacementPlugin(), // use path to module for development performance
new webpack.NamedModulesPlugin(),
new ReactRefreshWebpackPlugin(),
);
}
// conditionally required and add plugin bundle analzyer
if (process.env.ANALYZE_MODE === 'true') {
pluginSystem.push(new BundleAnalyzerPlugin());
}
if (process.env.DEBUG !== undefined) {
pluginSystem.push(
new webpack.DefinePlugin({ DEBUG: JSON.stringify(process.env.DEBUG) }),
);
} else {
pluginSystem.push(
new webpack.DefinePlugin({ DEBUG: false }),
);
}
// handle testing entry point and output file name
const entryPoint = './web/js/main.js';
const outputFileName = 'wv.js';
/*
if (process.env.TESTING_MODE === 'true') {
entryPoint = './test/main.js';
outputFileName = 'wv-test-bundle.js';
}
*/
const babelLoaderExcludes = [
/\.test\.js$/,
/fixtures\.js$/,
/core-js/,
];
// Inlucde any modules that need to be transpiled by babel-loader
const transpileDependencies = [
'react-visibility-sensor',
];
if (devMode) {
// Don't transpile any dependencies in /node_modules except those found
// in transpileDependencies array
babelLoaderExcludes.push(
new RegExp(`node_modules(?!(/|\\\\)(${transpileDependencies.join('|')})).*`),
);
}
module.exports = {
resolve: {
alias: {
googleTagManager$: path.resolve(
__dirname,
'./web/js/components/util/google-tag-manager.js',
),
},
},
mode: devMode ? 'development' : 'production',
stats: {
// reduce output text on build - remove for more verbose
chunks: false,
modules: false,
children: false,
},
entry: entryPoint,
devtool: devMode ? 'cheap-module-source-map' : 'source-map',
devServer: {
contentBase: path.join(__dirname, '/web'),
compress: true,
hot: true,
watchContentBase: true, // watch index.html changes
port: 3000,
host: '0.0.0.0',
liveReload: false,
},
output: {
filename: outputFileName,
path: path.join(__dirname, '/web/build'),
pathinfo: false,
},
optimization: {
minimizer: [
new UglifyJsPlugin({
uglifyOptions: {
ecma: 5, // dependent on ie11 support
compress: true,
mangle: false,
topLevel: true,
safari10: true,
output: {
comments: false,
beautify: false,
},
},
cache: true,
parallel: true,
}),
new OptimizeCSSAssetsPlugin({
cssProcessor: cssnano,
cssProcessorOptions: {
preset: ['default', {
discardComments: {
removeAll: true,
},
map: {
inline: false,
},
}],
},
}),
],
},
plugins: pluginSystem,
module: {
rules: [
{
test: /\.js$/,
use: {
loader: 'babel-loader',
options: {
compact: false, // fixes https://stackoverflow.com/questions/29576341/what-does-the-code-generator-has-deoptimised-the-styling-of-some-file-as-it-e
cacheDirectory: devMode,
plugins: [isDevServer && require.resolve('react-refresh/babel')].filter(Boolean),
},
},
exclude: babelLoaderExcludes,
},
{
test: require.resolve('jquery'), // expose globally for jQuery plugins
use: [
{
loader: 'expose-loader',
options: 'jQuery',
},
{
loader: 'expose-loader',
options: '$',
},
],
},
{
test: /\.(sa|sc|c)ss$/,
use: [
{
loader: 'css-hot-loader',
},
{
loader: MiniCssExtractPlugin.loader,
options: {
sourceMap: true,
},
},
{
loader: 'css-loader',
options: {
importLoaders: 1,
sourceMap: true,
},
},
{
loader: 'postcss-loader', // Run post css actions
options: {
sourceMap: true,
ident: 'postcss',
plugins: () => [
postcssPresetEnv({
browserslist: [
'last 4 versions',
'not ie < 11',
'not edge < 17',
'not IE_Mob 11',
'not dead',
'> 2%',
],
}),
postcssNesting(),
],
},
},
{
loader: 'sass-loader',
options: {
sourceMap: true,
},
},
],
},
{
test: /\.(png|jpg|jpeg|gif|svg)$/,
exclude: /(fontawesome-webfont.svg)/,
use: {
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'images/',
},
},
},
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
use: {
// handle font-awesome fonts
loader: 'url-loader?limit=10000&mimetype=application/font-woff',
options: {
name: '[name].[ext]',
outputPath: 'fonts/',
},
},
},
{
test: /((fontawesome-webfont.svg)|(\.(ttf|eot)(\?v=[0-9]\.[0-9]\.[0-9])?))/,
use: {
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts/',
},
},
},
{
test: /\.html$/,
use: [
{
loader: 'html-loader',
options: {
minimize: !devMode,
removeEmptyAttributes: !devMode,
sortAttributes: !devMode,
sortClassName: !devMode,
},
},
],
},
],
},
node: { fs: 'empty' },
};