-
Notifications
You must be signed in to change notification settings - Fork 11
/
webpack.config.ts
224 lines (216 loc) · 5.84 KB
/
webpack.config.ts
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
import path from 'path';
import {
Configuration,
ProvidePlugin,
IgnorePlugin,
DefinePlugin,
} from 'webpack';
import * as webpackDevServer from 'webpack-dev-server';
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const InlineChunkHtmlPlugin = require('react-dev-utils/InlineChunkHtmlPlugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
let commitHash: string = '';
// All the public files needed for a build
// to work in the real world
const baseFilesToCopy = [
'public/favicon-*.png',
'public/android-chrome-*.png',
'public/apple-*.png',
].map((x: string) => ({ from: x, to: '[name][ext]' }));
try {
commitHash = require('child_process')
.execSync('git rev-parse --short HEAD')
.toString()
.trim();
} catch (error) {
commitHash = '';
}
const config = (env): Configuration => {
const isProduction = process.env.NODE_ENV === 'production';
const rootPath = env.rootPath || '/';
console.log(`ROOT_PATH: ${rootPath}`);
return {
mode: isProduction ? 'production' : 'development',
target: ['browserslist'],
output: {
assetModuleFilename: 'images/[hash][ext][query]',
chunkFilename: '[name]-[chunkhash].js',
filename: '[name]-[fullhash].js',
path: path.resolve(__dirname, `build${rootPath}`),
publicPath: rootPath,
},
module: {
rules: [
{
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
// Expose images
{
test: /\.(png|webm|jpe?g|gif)$/i,
type: 'asset/resource',
},
// Properly load our SVG files into react components
{
test: /\.svg$/,
use: [
{
loader: '@svgr/webpack',
options: {
prettier: false,
svgo: false,
svgoConfig: {
plugins: [{ removeViewBox: false }],
},
titleProp: true,
ref: true,
},
},
{
loader: 'file-loader',
options: {
name: 'images/[name].[hash].[ext]',
},
},
],
issuer: {
and: [/\.(js|ts)x?$/],
},
},
],
},
plugins: [
// Which html file to use for the main page
new HtmlWebpackPlugin({
template: './public/index.html',
favicon: './public/favicon.ico',
inject: true,
publicPath: rootPath,
rootPath,
}),
new InlineChunkHtmlPlugin(HtmlWebpackPlugin, [/runtime-.+[.]js/]),
// Ensure the app has the globals it needs
new ProvidePlugin({
process: 'process/browser',
Buffer: ['buffer', 'Buffer'],
}),
// Type checking
new ForkTsCheckerWebpackPlugin({
typescript: {
build: true,
},
}),
new CopyPlugin({
patterns: [
{
from: 'public/images/',
to: 'images/',
},
{
from: 'public/build.manifest.json',
to: 'manifest.json',
transform: content => {
return content.toString().replace('$ROOT_PATH', rootPath);
},
},
].concat(baseFilesToCopy),
}),
// Moment adds a lot of locale files we don't need
new IgnorePlugin({
resourceRegExp: /^\.\/locale$/,
contextRegExp: /moment$/,
}),
new DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
__COMMIT__: JSON.stringify(commitHash),
}),
// Only use HMR on dev environment
!isProduction &&
new ReactRefreshWebpackPlugin({
overlay: false,
}),
].filter(Boolean),
resolve: {
alias: {
'bn.js': path.join(__dirname, 'node_modules/bn.js/lib/bn.js'),
},
extensions: ['.tsx', '.ts', '.js'],
// Node plugins we can't use in the browser
fallback: {
https: false,
http: false,
os: false,
stream: false,
path: false,
fs: false,
},
},
devServer: {
compress: true,
static: {
directory: path.join(__dirname, 'public'),
},
port: 3000,
historyApiFallback: true,
client: {
overlay: {
errors: true,
warnings: false,
},
},
},
cache: {
type: 'filesystem',
version: JSON.stringify(env),
store: 'pack',
buildDependencies: {
defaultWebpack: ['webpack/lib/'],
config: [__filename],
},
},
optimization: {
concatenateModules: true,
minimize: isProduction,
minimizer: [
// This is only used in production mode
new TerserPlugin({
terserOptions: {
parse: {
ecma: 8,
},
compress: {
ecma: 5,
warnings: false,
comparisons: false,
inline: 2,
},
mangle: {
safari10: true,
},
// Added for profiling in devtools
keep_classnames: isProduction,
keep_fnames: isProduction,
output: {
ecma: 5,
comments: false,
ascii_only: true,
},
},
}),
new CssMinimizerPlugin(),
],
},
// Which source maps to use
devtool: !isProduction ? 'inline-source-map' : undefined,
};
};
export default config;