-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.ts
128 lines (104 loc) · 3.24 KB
/
rollup.config.ts
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
import fs from "node:fs";
import path from "node:path";
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import node from "@rollup/plugin-node-resolve";
import { pick } from "remeda";
import { defineConfig } from "rollup";
import dts from "rollup-plugin-dts";
import esbuild from "rollup-plugin-esbuild";
import type { LogHandlerWithDefault, Plugin } from "rollup";
const input = {
index: "src/index.ts",
prettier: "src/prettier.ts",
modules: "src/modules.ts",
};
const dist = path.resolve(import.meta.dirname, "dist");
const modules = path.resolve(dist, "modules.cjs");
const cjs = path.resolve(dist, "cjs");
const esm = path.resolve(dist, "esm");
const external = [/^eslint$|^eslint\/.*/, "prettier", /^typescript$|^typescript\/.*/];
const IGNORED_WARNING_CODES = ["UNRESOLVED_IMPORT", "CIRCULAR_DEPENDENCY"];
const onLog: LogHandlerWithDefault = (level, log, handler) => {
if (level === "warn" && IGNORED_WARNING_CODES.includes(log.code || "")) return;
handler(level, log);
};
const pluginClean = (...paths: string[]): Plugin => ({
name: "plugin:clean",
async buildStart() {
await Promise.all(
paths.map(path =>
fs.promises.rm(path, {
force: true,
recursive: true,
}),
),
);
},
});
export default defineConfig([
{
input: pick(input, ["index", "prettier"]),
output: {
dir: dist,
format: "esm",
},
external,
plugins: [dts({ respectExternal: true }), pluginClean(dist)],
onLog,
},
{
input: pick(input, ["index", "prettier"]),
output: [
{
dir: cjs,
format: "cjs",
entryFileNames: "[name].cjs",
},
{
dir: esm,
format: "esm",
entryFileNames: "[name].mjs",
},
],
external: [...external, /node_modules/],
plugins: [
commonjs(),
json(),
esbuild({ minify: false }),
node(),
pluginClean(cjs, esm),
{
name: "plugin:module",
resolveId: {
order: "pre",
handler(source) {
if (!source.endsWith("/modules")) return null;
return { id: "../modules.cjs", external: true };
},
},
},
],
},
{
input: pick(input, ["modules"]),
output: {
dir: dist,
format: "cjs",
entryFileNames: "[name].cjs",
},
external,
plugins: [
commonjs({
// 相关错误:
// Could not dynamically require "<project>/node_modules/react/index.js". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.
ignoreDynamicRequires: true,
}),
json(),
esbuild({ minify: true }),
node(),
pluginClean(modules),
],
onLog,
},
]);