发布npm之前增加打包工具构建压缩代码

master
LiuJiaNan 2025-12-26 11:50:27 +08:00
parent f2600b25e8
commit dc9a864546
2 changed files with 24 additions and 1 deletions

View File

@ -23,6 +23,9 @@
"build": "rollup -c",
"build:watch": "rollup -c -w",
"prepublishOnly": "npm run build",
"patch": "npm version patch",
"minor": "npm version minor",
"publish": "npm publish",
"postinstall": "echo 'Thanks for using our component library!'"
},
"dependencies": {
@ -48,6 +51,7 @@
"@rollup/plugin-commonjs": "^29.0.0",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^16.0.3",
"@rollup/plugin-terser": "^0.4.4",
"glob": "^13.0.0",
"rollup": "^4.54.0"
}

View File

@ -2,6 +2,7 @@ import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import babel from '@rollup/plugin-babel';
import terser from '@rollup/plugin-terser';
import { readFileSync, existsSync } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
@ -195,6 +196,7 @@ const copyTypesPlugin = () => ({
* 3. commonjs - 转换 CommonJS 模块
* 4. json - 解析 JSON 导入但被标记为 external不会实际转换
* 5. copyTypesPlugin - 复制类型和样式文件
* 6. terser - 代码压缩
*/
const plugins = [
// 模块解析插件 - 解析 node_modules 中的模块
@ -215,7 +217,24 @@ const plugins = [
json(),
// 自定义插件 - 复制类型和样式文件
copyTypesPlugin()
copyTypesPlugin(),
// 代码压缩插件
terser({
compress: {
drop_console: false, // 保留 console
drop_debugger: true, // 移除 debugger
pure_funcs: [], // 不移除任何函数调用
passes: 2 // 压缩次数
},
format: {
comments: false, // 移除注释
beautify: false // 不美化代码,保持压缩格式
},
ecma: 2015, // 目标 ECMAScript 版本
module: true, // 保留 ES 模块格式
toplevel: false // 不压缩顶层作用域
})
];
/**