-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrollup.config.js
More file actions
169 lines (161 loc) · 5.75 KB
/
Copy pathrollup.config.js
File metadata and controls
169 lines (161 loc) · 5.75 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import typescript from "@rollup/plugin-typescript";
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import dts from "rollup-plugin-dts";
import { readFileSync, writeFileSync, mkdirSync } from "fs";
import { dirname } from "path";
const pkg = JSON.parse(
readFileSync(new URL("./package.json", import.meta.url), "utf8")
);
// Plugin to copy package.json files for proper module resolution
function copyPackageJson(type, dir) {
return {
name: "copy-package-json",
generateBundle() {
const packageJsonContent = JSON.stringify({ type }, null, 2);
const outputPath = `${dir}/package.json`;
try {
mkdirSync(dirname(outputPath), { recursive: true });
writeFileSync(outputPath, packageJsonContent);
console.log(`✅ Created ${outputPath} with type: ${type}`);
} catch (error) {
console.warn(
`⚠️ Failed to create ${outputPath}:`,
error.message
);
}
},
};
}
export default [
// ESM build - Modular output
{
input: "src/index.ts",
output: {
dir: "dist/esm",
format: "es",
sourcemap: true,
exports: "named",
preserveModules: true, // Keep modular structure
preserveModulesRoot: "src", // Preserve src structure
},
plugins: [
resolve({
preferBuiltins: true, // Prefer Node.js built-ins
browser: false, // Target Node.js
exportConditions: ["node"], // Use Node.js exports
}),
commonjs(),
json(),
typescript({
tsconfig: "./tsconfig.json",
declaration: false, // We'll generate declarations separately
declarationMap: false, // Disable declaration maps
outDir: undefined, // Let Rollup handle output directory
exclude: [
"**/node_modules/**/*",
"**/*.test.ts",
"**/*.spec.ts",
"src/__tests__/**/*",
],
}),
copyPackageJson("module", "dist/esm"),
],
external: (id) => {
// Make ALL dependencies external (don't bundle them)
if (id.includes("node_modules")) return true;
if (
Object.keys(pkg.dependencies || {}).some(
(dep) => id === dep || id.startsWith(dep + "/")
)
)
return true;
if (
Object.keys(pkg.peerDependencies || {}).some(
(dep) => id === dep || id.startsWith(dep + "/")
)
)
return true;
// Node.js built-ins
const builtins = [
"crypto", "fs", "path", "os", "http", "https", "events",
"stream", "buffer", "util", "url", "querystring", "zlib",
"child_process", "cluster", "dgram", "dns", "net", "tls",
"readline", "repl", "vm", "worker_threads", "perf_hooks",
];
if (builtins.includes(id)) return true;
return false;
},
},
// CommonJS build - Modular output
{
input: "src/index.ts",
output: {
dir: "dist/cjs",
format: "cjs",
sourcemap: true,
exports: "auto",
esModule: false,
preserveModules: true, // Keep modular structure
preserveModulesRoot: "src", // Preserve src structure
},
plugins: [
resolve({
preferBuiltins: true, // Prefer Node.js built-ins
browser: false, // Target Node.js
exportConditions: ["node"], // Use Node.js exports
}),
commonjs(),
json(),
typescript({
tsconfig: "./tsconfig.json",
declaration: false, // Prevent duplicate declarations
declarationMap: false, // Disable declaration maps
outDir: undefined, // Let Rollup handle output directory
exclude: [
"**/node_modules/**/*",
"**/*.test.ts",
"**/*.spec.ts",
"src/__tests__/**/*",
],
}),
copyPackageJson("commonjs", "dist/cjs"),
],
external: (id) => {
// Make ALL dependencies external (don't bundle them)
if (id.includes("node_modules")) return true;
if (
Object.keys(pkg.dependencies || {}).some(
(dep) => id === dep || id.startsWith(dep + "/")
)
)
return true;
if (
Object.keys(pkg.peerDependencies || {}).some(
(dep) => id === dep || id.startsWith(dep + "/")
)
)
return true;
// Node.js built-ins
const builtins = [
"crypto", "fs", "path", "os", "http", "https", "events",
"stream", "buffer", "util", "url", "querystring", "zlib",
"child_process", "cluster", "dgram", "dns", "net", "tls",
"readline", "repl", "vm", "worker_threads", "perf_hooks",
];
if (builtins.includes(id)) return true;
return false;
},
},
// TypeScript declarations
{
input: "src/index.ts",
output: {
file: "dist/schema.d.ts",
format: "es",
},
plugins: [dts()],
external: [],
},
];