-
Notifications
You must be signed in to change notification settings - Fork 5
/
webpack.config.js
229 lines (215 loc) · 8.38 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
require('dotenv').config({ path: './.env' });
const path = require('path');
const package = require('./package.json');
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const TerserPlugin = require('terser-webpack-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const { DefinePlugin } = require('webpack');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const config = require('./src/config.json');
module.exports = (env, options)=> {
const devMode = options.mode === 'development' ? true : false;
process.env.NODE_ENV = options.mode;
// name of the explorer app to start/build:
const app = env['app']
if(!app){
throw new Error(
'A valid `app` name is not found in environment variables, '+
'try `npm run start-landsat`.\n'
)
}
const appConfig = config.apps[app]
if(!appConfig){
throw new Error(
`config data for "${app}" is not found, `+
'please update `./src/config.json` to make sure it includes config data for this app'
)
}
const {
entrypoint,
title,
description,
pathname,
} = appConfig;
if(!entrypoint){
throw new Error(
`entrypoint for "${app}" is not found, `+
'please update `./src/config.json` to make sure it includes entrypoint of the app to start'
)
}
console.log(`starting ${app}\n`);
return {
mode: options.mode,
devServer: {
server: 'https',
host: process.env.WEBPACK_DEV_SERVER_HOSTNAME || 'localhost',
allowedHosts: "all",
port: 8080
},
entry: entrypoint,
output: {
path: path.resolve(__dirname, `./dist/${app}`),
filename: '[name].[contenthash].js',
chunkFilename: '[name].[contenthash].js',
clean: true,
assetModuleFilename: `[name][contenthash][ext][query]`
},
devtool: devMode ? 'source-map' : false,
resolve: {
extensions: ['.js', '.jsx', '.json', '.ts', '.tsx'],
alias: {
'@shared': path.resolve(__dirname, 'src/shared/'),
'@landsat-explorer': path.resolve(__dirname, 'src/landsat-explorer/'),
'@landcover-explorer': path.resolve(__dirname, 'src/landcover-explorer/'),
'@typing': path.resolve(__dirname, 'src/types/'),
},
},
module: {
rules: [
{
test: /\.(ts|tsx)$/,
loader: 'babel-loader'
},
{
test: /\.css$/i,
// include: path.resolve(__dirname, 'src'),
use: [
devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
sourceMap: true
}
},
{
loader: 'postcss-loader'
}
],
},
// {
// test: /\.(woff|woff2|ttf|eot)$/,
// loader: "file-loader",
// options: {
// name: '[name].[contenthash].[ext]',
// }
// },
{
test: /\.(woff|woff2|ttf|eot)$/,
type: 'asset/resource',
},
// {
// test: /\.(png|jpg|gif|svg)$/,
// loader: "file-loader",
// options: {
// name: '[name].[contenthash].[ext]',
// }
// },
{
test: /\.(png|jpg|gif|svg)$/,
type: 'asset/resource',
},
]
},
plugins: [
// need to use ForkTsCheckerWebpackPlugin because Babel loader ignores the compilation errors for Typescript
new ForkTsCheckerWebpackPlugin(),
new DefinePlugin({
/**
* node running environment
*/
NODE_ENV: JSON.stringify(process.env.NODE_ENV),
/**
* name of the imagery explorer app to start/build
*/
WEBPACK_DEFINED_APP_NAME: JSON.stringify(app),
}),
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: devMode ? '[name].css' : '[name].[contenthash].css',
chunkFilename: devMode ? '[name].css' : '[name].[contenthash].css',
}),
// copy static files from public folder to build directory
new CopyPlugin({
patterns: [
{
from: "public/**/*",
globOptions: {
ignore: ["**/index.html"],
},
}
],
}),
new HtmlWebpackPlugin({
template: './public/index.html',
filename: 'index.html',
title,
favicon: './public/esri-favicon-light-32.png',
meta: {
title,
description,
author: package.author,
keywords: Array.isArray(package.keywords)
? package.keywords.join(',')
: undefined,
'og:title': title,
'og:description': description,
'og:url': `https://livingatlas.arcgis.com${pathname}/`,
'og:image': `https://livingatlas.arcgis.com${pathname}/public/thumbnails/${app}.jpg`,
'last-modified': new Date().toString()
},
minify: {
html5 : true,
collapseWhitespace : true,
minifyCSS : true,
minifyJS : true,
minifyURLs : false,
removeComments : true,
removeEmptyAttributes : true,
removeOptionalTags : true,
removeRedundantAttributes : true,
removeScriptTypeAttributes : true,
removeStyleLinkTypeAttributese : true,
useShortDoctype : true
}
}),
// !devMode ? new CleanWebpackPlugin() : false,
!devMode ? new BundleAnalyzerPlugin() : false
].filter(Boolean),
optimization: {
// splitChunks: {
// cacheGroups: {
// // vendor chunk
// vendor: {
// // sync + async chunks
// chunks: 'all',
// name: 'vendor',
// // import file path containing node_modules
// test: /node_modules/
// }
// }
// },
minimizer: [
new TerserPlugin({
extractComments: true,
terserOptions: {
compress: {
drop_console: true,
}
}
}),
new CssMinimizerPlugin()
],
/**
* Encountered the `Uncaught ReferenceError: x is not defined` error during the production build.
* One suggestion that we found is disabling the `optimization.innerGraph` option is the best way to prevent this issue.
*
* @see https://img.ly/docs/pesdk/web/faq/webpack_reference_error/
*/
innerGraph: false,
},
}
};