-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
218 lines (199 loc) · 5.58 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
const webpack = require('webpack');
const path = require('path');
const ReactRefreshPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const { merge } = require('webpack-merge');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const ArcGISPlugin = require('@arcgis/webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const MomentLocalesPlugin = require('moment-locales-webpack-plugin');
const isDevelopment = process.env.NODE_ENV !== 'production';
/**
*
* COMMON CONFIGURATION
*
*/
const commonConfig = {
// Set the proper webpack "mode" dependent on the NODE_ENV environment flag
mode: isDevelopment ? 'development' : 'production',
// Resolve all paths relative to the 'src' directory
//context: path.resolve(__dirname, 'src'),
// Set the "entry" point for webpack to inspect the dependency tree
entry: {
index: './src/index.tsx',
},
//Application output location
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].bundle.js',
chunkFilename: '[name].chunk.js',
publicPath: '',
},
// Where to look for the various modules, and what type
// of file extensions should we look for
resolve: {
modules: [path.resolve(__dirname, '/src'), 'node_modules/'],
extensions: ['.mjs', '.ts', '.tsx', '.js', '.jsx'],
},
// Rules for how to "parse" the files used throughout the app
// The "loaders" are node modules that allow to understand
// how to preprocess files
module: {
rules: [
// Rules for CSS files
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
// Rules for SASS/SCSS files
{
test: /\.s[ac]ss$/i,
use: [
// Creates `style` nodes from JS strings
'style-loader',
// Translates CSS into CommonJS
'css-loader',
// Compiles Sass to CSS
'sass-loader',
],
},
// Rules for Typescript files
{
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
{
test: /\.m?js/,
resolve: {
fullySpecified: false,
},
},
// Rules for static files, such as images
{
test: /\.(png|jpe?g|gif|svg)$/i,
use: [
{
loader: 'file-loader',
},
],
},
],
},
// Plugins are executed top down in the order they are listed.
// While loaders are used to transform certain types of modules,
// plugins can be leveraged to perform a wider range of tasks like
// bundle optimization, asset management and injection of environment
// variables.
plugins: [
// Analyze bundle
// new BundleAnalyzerPlugin(),
// Only keep specified locales
// This still creates locale chunks, but I can't find a way
// around this and it is assumed they'll only load if needed
new MomentLocalesPlugin({
localesToKeep: ['es-us'],
}),
// Clean contents of "dist" folder
new CleanWebpackPlugin(),
// Copies files/folders from/to bundled locations
new CopyPlugin({ patterns: [{ from: 'sampleData', to: 'sampleData' }] }),
new ArcGISPlugin({
// The sketch widget requires the 3d widget
// features: {
// '3d': false,
// },
locales: ['en-US'],
userDefinedExcludes: [
'@arcgis/core/layers/BingMapsLayer',
'@arcgis/core/layers/CSVLayer',
'@arcgis/core/layers/GeoRSSLayer',
'@arcgis/core/layers/ImageryLayer',
'@arcgis/core/layers/KMLLayer',
'@arcgis/core/layers/MapImageLayer',
'@arcgis/core/layers/OpenStreetMapLayer',
'@arcgis/core/layers/StreamLayer',
'@arcgis/core/layers/WMSLayer',
'@arcgis/core/layers/WMTSLayer',
'@arcgis/core/layers/WebTileLayer',
'@arcgis/core/layers/ImageryTileLayer',
],
}),
// Inject the bundles into the distribution index.html
new HtmlWebpackPlugin({
template: 'index.html',
}),
],
};
/**
*
* LOCAL DEVELOPMENT CONFIGURATION
*
*/
const developmentConfig = {
devtool: 'source-map',
optimization: {
moduleIds: 'named',
},
plugins: [
new ReactRefreshPlugin(),
// Enable HMR globally
new webpack.HotModuleReplacementPlugin(),
],
// Setup development server
devServer: {
hot: true,
open: false,
port: 8080,
historyApiFallback: true,
},
};
/**
*
* PRODUCTION CONFIGURATION
*
*/
const productionConfig = {
optimization: {
minimize: true,
splitChunks: {
chunks: 'async',
minSize: 20000,
minRemainingSize: 0,
minChunks: 1,
maxAsyncRequests: 30,
maxInitialRequests: 30,
enforceSizeThreshold: 50000,
cacheGroups: {
// esriVendor: {
// test: /[\\/]node_modules[\\/](@arcgis)[\\/]/,
// name: 'esri',
// },
vendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10,
chunks: 'initial', // Remove this line if you don't want a single common vendors chunk
name: 'vendors', // Remove this line if you don't want a single common vendors chunk
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true,
},
},
},
},
};
/**
*
* MERGE THE COMMON CONFIG INTO EITHER DEV OR PRODUCTION
*
*/
module.exports = env => {
if (isDevelopment) {
return merge(commonConfig, developmentConfig);
} else {
return merge(commonConfig, productionConfig);
}
};