-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
162 lines (159 loc) · 4.47 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
const path = require('path');
const HtmlWebpackPlugin = require('Html-webpack-plugin');
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const TerserPlugin = require("terser-webpack-plugin");
// 此插件可依据用户指定的语言和功能自动引入相应的依赖文件。此外还需要添加处理 .ttf 后缀字体文件的 loader 配置。
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin')
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const WebpackBar = require('webpackbar');
const CopyWebpackPlugin = require('copy-webpack-plugin');
// const isDev = process.env.NODE_ENV !== 'production';
// Host
const host = process.env.HOST || '0.0.0.0';
const outputPath = path.join(__dirname, 'dist');
module.exports = (_, argv) => {
console.log(_, argv)
const mode = argv.mode;
const isDev = mode === 'development';
return {
target: 'web',
entry: {
index: './src/index.js',
},
output: {
globalObject: 'self',
path: outputPath,
filename: '[name].bundle.js',
},
resolve: {
extensions: ['.js', '.ts', '.jsx', '.tsx']
},
devServer: {
// Enable compression
hot: true,
compress: true,
host,
port: 3000,
devMiddleware: {
publicPath: '/'
},
headers: {
'Access-Control-Allow-Origin': '*'
},
static: {
publicPath: '/'
}
},
plugins: [
new MonacoWebpackPlugin({
languages: ['javascript', 'typescript'],
features: ['!contextmenu', '!linkedEditing', '!quickHelp', '!suggest']
}),
new HtmlWebpackPlugin({
template: './index.html'
}),
!isDev && new CopyWebpackPlugin({
patterns: [{
from: path.resolve(__dirname, 'public'),
to: path.resolve(__dirname, outputPath)
}]
}),
isDev && new ReactRefreshWebpackPlugin(),
new TerserPlugin({
extractComments: false
}),
new BundleAnalyzerPlugin(),
new WebpackBar()
].filter(Boolean),
module: {
rules: [
{
test: /\.ttf$/,
type: 'asset/resource',
generator: {
filename: '[name][ext]'
}
},
{
test: /utools\.d\.ts$/,
type: 'asset/source',
},
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
['@babel/preset-env', { targets: { chrome: 91 } }],
'@babel/preset-react'],
plugins: [
['@babel/plugin-proposal-decorators', { legacy: true }],
'@babel/plugin-proposal-class-properties',
[
'import',
{
libraryName: '@mui/material',
libraryDirectory: '',
camel2DashComponentName: false
},
'material'
],
[
'import',
{
libraryName: '@mui/icons-material',
libraryDirectory: 'esm',
camel2DashComponentName: false
},
'icons'
],
// this code will evaluate to "false" when
// "isDevelopment" is "false"
// otherwise it will return the plugin
isDev && require.resolve('react-refresh/babel')
// this line removes falsy values from the array
].filter(Boolean)
}
}
},
{
test: /\.(s[a|c]ss|css)$/,
use: [
{
loader: 'style-loader'
},
{
loader: 'css-loader',
options: { url: false }
},
{
loader: 'sass-loader'
}
]
}
]
},
performance: {
hints: false,
},
optimization: {
splitChunks: {
chunks: 'all',
minSize: 20000,
maxAsyncRequests: 30,
maxInitialRequests: 30,
enforceSizeThreshold: 50000,
cacheGroups: {
monacoEditor: {
chunks: 'async',
name: 'chunk-monaco-editor',
priority: 22,
test: /[\/]node_modules[\/]monaco-editor[\/]/,
reuseExistingChunk: true,
},
}
}
}
}
}