-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
130 lines (123 loc) · 4.06 KB
/
Copy pathrollup.config.mjs
File metadata and controls
130 lines (123 loc) · 4.06 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
125
126
127
128
129
130
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import terser from "@rollup/plugin-terser";
import typescript from "@rollup/plugin-typescript";
import fs from "fs";
import path from "path";
import dts from "rollup-plugin-dts";
/**
* 获取当前工作目录下的 package.json 内容
* @returns {any} 解析后的 package.json 对象
* @throws 如果找不到 package.json 则抛出异常
*/
const getCurrentPackageJson = () => {
const packagePath = path.resolve(process.cwd(), "package.json");
if (fs.existsSync(packagePath)) {
return JSON.parse(fs.readFileSync(packagePath, "utf-8"));
}
throw new Error(`package.json not found in ${process.cwd()}`);
};
/**
* 生成共享的 Rollup 配置
* @param options.input 入口文件路径,默认为 src/index.ts
* @param options.external 需要排除打包的依赖(external)
* @param options.plugins 额外的 Rollup 插件
* @returns {RollupOptions[]} Rollup 配置数组(包含 ESM 构建和类型声明构建)
*/
export default function createRollupConfig(options = {}) {
// 读取 package.json
const pkg = getCurrentPackageJson();
// 入口文件,默认为 src/index.ts
const input = options.input || "src/index.ts";
// 需要 external 的依赖
const external = options.external || [];
// 额外插件
const additionalPlugins = options.plugins || [];
// 基础插件配置
const basePlugins = [
json(), // 支持导入 json 文件
nodeResolve({
preferBuiltins: false, // 不优先使用 Node.js 内置模块
browser: false, // 不启用浏览器兼容
}),
commonjs(), // 支持 CommonJS 模块
typescript({
tsconfig: "./tsconfig.json",
declaration: false, // 类型声明由单独的配置生成
declarationMap: false,
sourceMap: true,
}),
...additionalPlugins, // 自定义插件
];
const configs = [];
// ESM 构建配置
configs.push({
input,
output: [
{
dir: "dist", // 使用 dir 而不是 file,因为使用了 preserveModules
format: "esm",
sourcemap: true,
preserveModules: true, // 保持模块结构,支持按需加载
preserveModulesRoot: "src", // 保持 src 目录结构
entryFileNames: "[name].js", // 输出文件名规则
exports: "named",
},
{
file: "dist/index.esm.min.js", // 压缩版 ESM 文件
format: "esm",
sourcemap: true,
plugins: [terser()], // 代码压缩
exports: "named",
},
],
plugins: basePlugins,
// external 配置,排除 dependencies、peerDependencies 及自定义 external
external: (id) => {
// 排除 peerDependencies
if (pkg.peerDependencies && Object.keys(pkg.peerDependencies).some(dep => id === dep || id.startsWith(dep + '/'))) {
return true;
}
// 排除 dependencies
if (pkg.dependencies && Object.keys(pkg.dependencies).some(dep => id === dep || id.startsWith(dep + '/'))) {
return true;
}
// 排除自定义 external
if (external.some(ext => id === ext || id.startsWith(ext + '/'))) {
return true;
}
return false;
},
});
// 类型声明文件构建配置
configs.push({
input,
output: {
dir: "dist", // 使用 dir 而不是 file
format: "esm",
},
plugins: [
dts({
respectExternal: true, // 遵循 external 配置
}),
],
// external 配置,逻辑同上
external: (id) => {
// 排除 peerDependencies
if (pkg.peerDependencies && Object.keys(pkg.peerDependencies).some(dep => id === dep || id.startsWith(dep + '/'))) {
return true;
}
// 排除 dependencies
if (pkg.dependencies && Object.keys(pkg.dependencies).some(dep => id === dep || id.startsWith(dep + '/'))) {
return true;
}
// 排除自定义 external
if (external.some(ext => id === ext || id.startsWith(ext + '/'))) {
return true;
}
return false;
},
});
return configs;
}