-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.babel.ts
More file actions
168 lines (159 loc) · 4.62 KB
/
webpack.config.babel.ts
File metadata and controls
168 lines (159 loc) · 4.62 KB
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
/* eslint no-console: ["error", { allow: ["warn", "error", "info"] }] */
import { Configuration, WebpackPluginInstance } from 'webpack';
import { join, resolve } from 'path';
import ReactRefreshPlugin from '@pmmmwh/react-refresh-webpack-plugin';
// @ts-expect-error: Waiting for declaration file.
import AntdDayjsPlugin from 'antd-dayjs-webpack-plugin';
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
import CssMinimizerPlugin from 'css-minimizer-webpack-plugin';
// @ts-expect-error: Waiting for declaration file.
import ErrorOverlayPlugin from 'error-overlay-webpack-plugin';
import HtmlPlugin from 'html-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import { createServe } from 'serve-mock';
import TerserPlugin from 'terser-webpack-plugin';
import { theme } from './package.json';
const Try = <T>(fn: () => T): T | undefined => {
try {
return fn();
} catch {
return undefined;
}
};
const ssr = process.argv.includes('ssr');
const withoutMock = process.argv.includes('without-mock');
// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
const proxy = Try(() => require('./.proxy')?.default);
const makeConfig = (target: string) => {
const entryPath = resolve(__dirname, 'src');
const outputPath = resolve(__dirname, 'public');
const mode = process.env.NODE_ENV as Configuration['mode'];
const config: Configuration = {
name: target,
target,
mode,
entry: {
app: join(entryPath, 'index.tsx'),
},
output: {
path: outputPath,
publicPath: '/',
chunkFilename: ({ chunk }) => {
const name = chunk!.name!.replace(/-([a-z0-9])/g, '/$1');
if (name.split('/').length === 2) {
return `${name}/index.js`;
}
return `${name}.js`;
},
},
resolve: {
extensions: ['.js', '.ts', '.jsx', '.tsx'],
alias: {
'@': entryPath,
},
},
module: {
rules: [
{
test: /\.(j|t)sx?$/,
exclude: /node_modules(\/|\\)(?!react-intl|intl-messageformat|intl-messageformat-parser)/,
use: [
{
loader: 'babel-loader',
options: {
cacheDirectory: true,
caller: { target, ssr },
},
},
],
},
{
test: /\.less$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'less-loader',
options: {
lessOptions: {
modifyVars: theme,
javascriptEnabled: true,
},
},
},
],
},
{
test: /\.(png|jpe?g|gif|svg)$/,
type: 'asset/resource',
generator: {
filename: 'images/[name][ext]',
},
},
],
},
plugins: [
new AntdDayjsPlugin(),
new MiniCssExtractPlugin({
chunkFilename: 'css/[name].css',
}),
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: [
'**/*',
'!images',
'!images/**/*',
'!.gitignore',
'!update-browser.html',
'!*-stats.json',
],
}),
new HtmlPlugin({
template: join(entryPath, 'index.html'),
}),
],
devtool: false,
devServer: {
contentBase: outputPath,
open: true,
hot: true,
historyApiFallback: true,
proxy,
after: withoutMock
? undefined
: (app, server) =>
app.all(
'*',
createServe(resolve(__dirname, 'mocks'), {
onWatch: name => {
if (name === 'change') {
console.info('\x1b[34mℹ\x1b[0m \x1b[90m「mock」\x1b[0m: Compiling...');
server.sockWrite(server.sockets, 'content-changed');
console.info('\x1b[34mℹ\x1b[0m \x1b[90m「mock」\x1b[0m: Compiled successfully.');
}
},
}),
),
},
optimization: {
minimizer: [
new TerserPlugin(),
new CssMinimizerPlugin(),
] as unknown as WebpackPluginInstance[],
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/](node_modules|src[\\/](components|utils))[\\/]/,
chunks: 'all',
name: 'vendor',
},
},
},
},
};
if (mode === 'development') {
config.devtool = 'cheap-module-source-map';
config.plugins!.push(new ErrorOverlayPlugin(), new ReactRefreshPlugin());
}
return config;
};
export default [makeConfig('web')];