-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.common.js
112 lines (111 loc) · 3.16 KB
/
webpack.common.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
const path = require('path')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const { NODE_ENV } = require('./env.js')
module.exports = (mode) => {
const devMode = mode === NODE_ENV.DEV
return {
entry: {
index: { import: [devMode ? './src/devIndex.tsx' : './src/index.tsx'] }
},
mode,
output: {
filename: '[name].[contenthash].js', // 动态生成 bundle 名称
path: path.resolve(__dirname, 'dist/client'),
clean: true,
publicPath: '/'
},
resolve: {
extensions: ['.tsx', '.ts', '...'],
alias: {
components: path.resolve(__dirname, 'src/components/'),
pages: path.resolve(__dirname, 'src/pages/'),
assets: path.resolve(__dirname, 'src/assets/'),
utils: path.resolve(__dirname, 'src/utils/'),
types: path.resolve(__dirname, 'src/types/'),
_constants: path.resolve(__dirname, 'src/constants'),
api: path.resolve(__dirname, 'src/api/'),
public: path.resolve(__dirname, 'src/public/')
}
},
externals: {
react: 'React', // key 对应引入包的名称 value 原包向window中注入的变量名称
'react-dom': 'ReactDOM',
'react-dom/client': 'ReactDOM'
},
optimization: {
runtimeChunk: 'single',
splitChunks: {
cacheGroups: {
// 'test-vendor': {
// test: /[\\/]node_modules[\\/]/,
// name: 'test-vendor',
// chunks(chunk){
// return chunk.name === 'test'
// }
// },
'index-vendor': {
test: /[\\/]node_modules[\\/]/,
name: 'index-vendor',
chunks(chunk) {
return chunk.name === 'index'
}
}
}
}
},
plugins: [
new MiniCssExtractPlugin({
filename: devMode ? '[name].css' : '[name].[contenthash].css',
chunkFilename: devMode ? '[id].css' : '[id].[contenthash].css'
})
],
module: {
rules: [
{
test: /\.(ts|tsx)$/i,
use: ['ts-loader']
},
// {
// test: /\.jsx$/i,
// use: {
// loader: 'babel-loader',
// options: {
// presets: [
// '@babel/preset-env',
// [
// ('@babel/preset-react',
// { runtime: 'automatic', importSource: '@emotion/react' })
// ]
// ]
// }
// }
// },
{
test: /\.css$/i, // 处理 css 文件,先安装依赖,再配置
use: [
{
loader: MiniCssExtractPlugin.loader, // 将 css 抽离到文件中,通过 link 标签将文件引入 html 中
options: {}
},
'css-loader'
]
},
{
test: /\.(|jpg|gif)$/i, // 处理图片,先安装依赖,再配置
type: 'asset/resource',
generator: {
// emit: false
}
},
{
test: /\.png$/i,
type: 'asset/inline'
},
{
test: /\.(jpeg|svg)/,
type: 'asset/source'
}
]
}
}
}