-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathwebpack.config.cjs
More file actions
124 lines (117 loc) · 3.89 KB
/
webpack.config.cjs
File metadata and controls
124 lines (117 loc) · 3.89 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
// import {createRequire} from 'module';
// import {fileURLToPath} from "url";
// import path from "path";
// const require = createRequire(import.meta.url);
// const module = {};
// const __dirname = path.dirname(fileURLToPath(import.meta.url));
const path = require('path');
const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
mode: 'production',
// entry: './src/drpy-core.js',
// output: {
// path: path.resolve(__dirname, 'dist'),
// filename: 'drpy-core.min.js',
// library: {
// type: 'module'
// },
// environment: {
// module: true
// }
// },
// 1. 配置多个入口文件
entry: {
'drpy-core': './src/drpy-core.js', // 完整版入口
'drpy-core-lite': './src/drpy-core-lite.js', // lite版入口
'drpy-core-fast': './src/drpy-core-fast.js', // lite版入口
},
output: {
// 2. 使用 [name] 占位符生成对应输出文件
path: path.resolve(__dirname, 'dist'),
filename: '[name].min.js', // 输出格式:main.bundle.js, admin.bundle.js 等
clean: true, // 构建前清理输出目录
library: {
type: 'module'
},
environment: {
module: true
}
},
performance: {
maxEntrypointSize: 2048000, // 默认250kb,单位字节
maxAssetSize: 2048000
},
experiments: {
outputModule: true
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
['@babel/preset-env', {
targets: {
browsers: ['last 2 versions', 'not dead', '> 0.2%']
},
modules: false,
}]
],
plugins: [
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-optional-chaining'
]
}
}
},
{
test: /\.min\.js$/, // 处理第三方.min.js文件
resolve: {
fullySpecified: false
},
use: ['babel-loader?cacheDirectory=true']
},
{
// 匹配需要作为全局脚本加载的文件
test: /(gb18030|crypto-js|jsencrypt|node-rsa|pako|json5|jsonpathplus|jinja|polywasm|encoding|EncoderDecoder|xxhash-wasm|buffer)\.min\.js$/,
use: ['script-loader']
}
]
},
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
parallel: true,
extractComments: false,
terserOptions: {
ecma: 2015,
compress: {
drop_console: false,
passes: 2,
// 关键:保留全局变量引用
keep_fnames: true,
keep_classnames: true
},
mangle: {
// reserved: ['模板','CryptoJS','gbkTool','jinja'], // 保留中文标识符
properties: false // 关键:不混淆全局变量名称
},
format: {
comments: false
}
}
})
]
},
resolve: {
extensions: ['.js'],
alias: {
// 如果模板.js文件路径需要别名
'模板': path.resolve(__dirname, 'src/模板.js')
}
},
};