-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.babel.js
216 lines (206 loc) · 4.97 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
import path from 'path';
import ReactRefreshWebpackPlugin from '@pmmmwh/react-refresh-webpack-plugin';
import CopyPlugin from 'copy-webpack-plugin';
import CssMinimizerPlugin from 'css-minimizer-webpack-plugin';
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import RemovePlugin from 'remove-files-webpack-plugin';
import TerserJSPlugin from 'terser-webpack-plugin';
// const
const mode = process.env.NODE_ENV ?? 'production';
const isProd = mode === 'production';
const isDev = !isProd;
const port = 3000;
// utils
const arrayFilterEmpty = (array) => array.filter((x) => !!x);
const fileName = (ext) => `[name].[contenthash].${ext}`;
console.log(process.env)
const react = process.env.REACT_APP_LANDING_URL
console.log(react)
// plugins
const removePlugin = new RemovePlugin({
before: {
include: [path.resolve(__dirname, 'build')],
},
});
const copyPlugin = new CopyPlugin({
patterns: [
{
from: path.resolve(__dirname, 'public/favicon.png'),
to: path.resolve(__dirname, 'build'),
},
],
});
const forkTsCheckerWebpackPlugin = new ForkTsCheckerWebpackPlugin({
async: isDev,
typescript: {
configFile: path.resolve(__dirname, 'tsconfig.json'),
},
eslint: {enabled: true, files: './**/*.{ts,tsx,js,jsx}'},
});
const htmlWebpackPlugin = new HtmlWebpackPlugin({
filename: 'index.html',
inject: true,
template: path.resolve(__dirname, 'public/index.html'),
minify: {
removeComments: isProd,
collapseWhitespace: isProd,
},
});
const miniCssExtractPlugin = new MiniCssExtractPlugin({
filename: fileName('css'),
chunkFilename: fileName('css'),
});
const terserPlugin = new TerserJSPlugin({});
const cssMinimizerPlugin = new CssMinimizerPlugin();
const reactRefreshPlugin = new ReactRefreshWebpackPlugin();
//babel-loader
const babelLoader = {
loader: 'babel-loader',
options: {
configFile: path.resolve(__dirname, '.babelrc.js'),
},
};
//rules
const cssRule = {
test: /\.css$/,
use: [
{
loader: isDev ? 'style-loader' : MiniCssExtractPlugin.loader,
options: {
esModule: false,
},
},
'css-loader',
'postcss-loader',
],
};
const svgReactComponentRule = {
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
issuer: /\.[jt]sx$/,
use: [
babelLoader,
{
loader: '@svgr/webpack',
options: {
babel: false,
icon: true,
},
},
],
};
/**
* Using file-loader for handling svg files
* @see https://webpack.js.org/guides/asset-modules/
*/
const svgRule = {
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
issuer: {not: [/\.[jt]sx$/]},
type: 'asset/inline',
};
/**
* @see https://webpack.js.org/guides/typescript/#loader
*/
const typescriptRule = {
test: /\.tsx?$/,
loader: 'ts-loader',
options: {
transpileOnly: true,
},
exclude: /node_modules/,
};
/**
* @see https://webpack.js.org/loaders/babel-loader
*/
const javascriptRule = {
test: /\.(js|jsx)$/,
use: [babelLoader],
exclude: /node_modules/,
};
/**
* @see https://webpack.js.org/guides/asset-modules/
*/
const imagesRule = {
test: /\.(?:ico|gif|png|jpg|jpeg)$/i,
type: 'asset/resource',
};
/**
* @see https://webpack.js.org/guides/asset-modules/
*/
const fontsRule = {
test: /\.(woff(2)?|eot|ttf|otf|)$/,
type: 'asset/resource',
};
export default {
mode: isProd ? 'production' : 'development',
target: isProd ? ['web', 'es5'] : 'web',
context: path.resolve(__dirname, 'src'),
entry: isProd ? ['./index.tsx'] : [path.resolve(__dirname, './cleanOnHMR.js'), './index.tsx'],
output: {
publicPath: '/',
filename: fileName('js'),
path: path.resolve(__dirname, 'build'),
},
module: {
rules: [
javascriptRule,
typescriptRule,
imagesRule,
fontsRule,
cssRule,
svgReactComponentRule,
svgRule,
],
},
plugins: arrayFilterEmpty([
htmlWebpackPlugin,
forkTsCheckerWebpackPlugin,
copyPlugin,
removePlugin,
isDev && reactRefreshPlugin,
isProd && miniCssExtractPlugin,
]),
resolve: {
alias: {
'@images': path.resolve(__dirname, 'src/images'),
'@styles': path.resolve(__dirname, 'src/styles'),
'@components': path.resolve(__dirname, 'src/components'),
},
extensions: ['.tsx', '.ts', '.js', '.jsx'],
},
optimization: {
minimize: isProd,
minimizer: [terserPlugin, cssMinimizerPlugin],
runtimeChunk: {
name: 'runtime',
},
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
chunks: 'initial',
},
},
},
},
performance: {
hints: false,
maxEntrypointSize: 512000,
maxAssetSize: 512000,
},
devtool: isDev ? 'cheap-module-source-map' : false,
devServer: {
client: {
overlay: false,
},
headers: {'Access-Control-Allow-Origin': '*'},
historyApiFallback: true,
hot: true,
port,
static: {
publicPath: '/',
},
},
};