-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.base.js
105 lines (102 loc) · 2.94 KB
/
webpack.base.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
/**
* webpack 基础配置
*/
const pathBuilder = require('path');
// webpack 压缩插件
const TerserPlugin = require('terser-webpack-plugin');
// ts 配置文件读取插件
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
const webpack = require('webpack');
module.exports = function (env, mode) {
const { output = './dist' } = env;
const outputParts = output.split('/');
return {
mode,
devtool: mode !== 'production' ? 'source-map' : undefined,
cache: true,
output: {
path: pathBuilder.resolve(...outputParts),
filename: '[name].[chunkhash].js'
},
optimization: {
usedExports: true,
minimize: true,
minimizer: [
new TerserPlugin({
extractComments: false
})
],
splitChunks: {
chunks: 'all',
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/](react|react-dom)[\\/]/,
name: 'commons',
reuseExistingChunk: true,
chunks: 'all'
}
}
}
},
resolve: {
extensions: ['.js', '.ts', '.tsx', '.json', 'txt'],
// 路径简化识别插件
plugins: [
// typescript 路径识别插件,该插件会通过 tsconfig 配置生成简化的别名
// 如 tsconfig 中 paths:{"@/*": ["src/*"]}
// 通过该插件,代码中可使用 import {...} from '@/components/....' 这种形式。
// 这种形式代表了 import {...} from '${root}/src/components/....'
new TsconfigPathsPlugin({ configFile: './tsconfig.json' })
]
},
// 模块系统配置
module: {
// 文件编译规则
rules: [
{
// 匹配文件名
test: /\.ts$|\.tsx$/,
// 排除匹配目录
exclude: /(node_modules|bower_components)/,
// 使用编译接驳器 babel-loader,
// babel-loader 会自动寻找 .babelrc,babel.config.js等文件,将配置信息merge成 babel 解析配置信息
use: [
{
loader: 'babel-loader',
options: {
cacheDirectory: true
}
}
]
},
{
// less处理
test: /\.less$/,
exclude: /(node_modules|bower_components)/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
modules: {
localIdentName: '[name]_[local]_[hash:base64:5]'
}
}
},
{
loader: 'less-loader'
}
]
}
]
},
plugins: [
// 定义环境变量,环境变量可在代码中使用,webpack会根据环境变量忽略不符合当前编译环境的代码
new webpack.DefinePlugin({
'process.env':{
NODE_ENV: JSON.stringify(mode === 'development' ? mode : 'production')
}
})
]
};
};