-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.mjs
113 lines (109 loc) · 2.7 KB
/
rollup.config.mjs
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
import { defineConfig } from "rollup";
import swc from "@rollup/plugin-swc";
import nodeResolve from "@rollup/plugin-node-resolve";
import { sync } from "glob";
import { dirname, extname, relative, resolve } from "path";
import { fileURLToPath } from "url";
import alias from "@rollup/plugin-alias";
import dts from "rollup-plugin-dts";
import terser from "@rollup/plugin-terser";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const paths = [];
const input = Object.fromEntries(
sync("src/**/*", { absolute: true })
.map((file) => {
paths.push(file);
if (file.endsWith(".spec.ts")) return;
return [
// 这里将删除 `src/` 以及每个文件的扩展名。
// 因此,例如 src/nested/foo.js 会变成 nested/foo
relative("src", file.slice(0, file.length - extname(file).length)),
// 这里可以将相对路径扩展为绝对路径,例如
// src/nested/foo 会变成 /project/src/nested/foo.js
file,
];
})
.filter((x) => x),
);
/** @type {import("rollup").InputPluginOption} */
const commonPlugins = [
alias({
entries: [{ find: "@", replacement: resolve(__dirname, "src") }],
}),
nodeResolve({
extensions: [".js", ".ts", ".mjs", ".mts", ".cjs", ".cts"],
}),
swc({
swc: {
jsc: { target: "es6" },
swcrc: true,
},
}),
];
export default defineConfig([
// 生成ESM和CJS格式
{
input,
external(id) {
if (paths.includes(id)) return false;
if (id.startsWith("@/")) return false;
return true;
},
plugins: commonPlugins,
output: [
// 生成ESM格式
{
format: "esm",
dir: "lib/esm",
sourcemap: "inline",
entryFileNames: "[name].mjs",
preserveModules: true,
preserveModulesRoot: "src",
},
// 生成CJS格式
{
format: "cjs",
dir: "lib/cjs",
sourcemap: "inline",
entryFileNames: "[name].js",
preserveModules: true,
preserveModulesRoot: "src",
},
],
},
// 生成类型定义文件
{
input,
plugins: [dts()],
output: {
dir: "lib/types",
preserveModules: true,
preserveModulesRoot: "src",
},
},
// 生成压缩版IIFE
{
input,
plugins: [...commonPlugins, terser()],
output: {
format: "iife",
dir: "lib",
entryFileNames: "my-library.min.js",
name: "MyLibrary",
preserveModulesRoot: "src",
},
},
// 生成正常IIFE
{
input,
plugins: commonPlugins,
output: {
format: "iife",
dir: "lib",
entryFileNames: "my-library.js",
name: "MyLibrary",
preserveModulesRoot: "src",
},
},
]);