-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrollup.config.js
More file actions
177 lines (158 loc) · 6.03 KB
/
rollup.config.js
File metadata and controls
177 lines (158 loc) · 6.03 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
170
171
172
173
174
175
176
177
import path from "path";
import typescript from "@rollup/plugin-typescript";
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import fs from "fs";
import dts from "rollup-plugin-dts";
const packagesDir = "packages"; // Adjust if your workspaces are elsewhere
const distDir = "dist";
const workspaces = fs
.readdirSync(packagesDir)
.filter((pkg) => fs.existsSync(path.join(packagesDir, pkg, "index.ts")));
const packageJsons = workspaces.reduce((acc, pkg) => {
const packageJsonPath = path.join(packagesDir, pkg, "package.json");
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
acc[pkg] = packageJson;
return acc;
}, {});
// Build a dependency graph based on package.json dependencies
function buildDependencyGraph() {
const graph = {};
// Create a graph where each package points to its dependencies
for (const pkg of workspaces) {
graph[pkg] = Object.keys(packageJsons[pkg].dependencies || {});
}
return graph;
}
// Topological sort (using Kahn's algorithm) to determine the correct build order
function topologicalSort(graph) {
const sorted = [];
const visited = new Set();
const tempMark = new Set();
function visit(pkg) {
if (tempMark.has(pkg)) {
throw new Error(`Circular dependency detected: ${pkg}`);
}
if (!visited.has(pkg)) {
tempMark.add(pkg);
for (const dep of graph[pkg] || []) {
visit(dep);
}
tempMark.delete(pkg);
visited.add(pkg);
sorted.push(pkg);
}
}
for (const pkg of workspaces) {
visit(pkg);
}
return sorted.reverse(); // Reverse to get the correct order
}
const dependencyGraph = buildDependencyGraph();
const sortedWorkspaces = topologicalSort(dependencyGraph).filter((pkg) => workspaces.includes(pkg));
const prod = true
console.log(sortedWorkspaces)
const configs = [
...sortedWorkspaces.map((pkg) => ([
{
input: path.join(packagesDir, pkg, "index.ts"),
output: {
file: path.join(packagesDir, pkg, distDir, "index.d.ts"),
format: "es"
},
plugins: [dts()]
},
{
input: path.join(packagesDir, pkg, "index.ts"),
output: {
file: path.join(packagesDir, pkg, distDir, "index.d.mts"),
format: "es"
},
plugins: [dts()]
}
])),
...sortedWorkspaces.map((pkg) => ([
// Commonjs
{
input: path.join(packagesDir, pkg, "index.ts"),
output: {
dir: path.join(packagesDir, pkg, distDir),
format: "cjs",
sourcemap: true,
preserveModules: true,
preserveModulesRoot: path.join(packagesDir, pkg), // ✅ Fixes imports
entryFileNames: "[name].js", // ✅ Ensures the output files have `.mjs`
},
plugins: [resolve({
extensions: [".ts", ".tsx", ".js", ".json"],
}),
commonjs({ transformMixedEsModules: true }),
typescript({
compilerOptions: {
module: "ESNext",
moduleResolution: "node",
declaration: false, // Set true if you want .d.ts
esModuleInterop: false,
importHelpers: false,
},
}),
],
external: (id) => !id.startsWith(".") && !id.startsWith('packages') && !path.isAbsolute(id), // Exclude external dependencies
},
// ESM Output
{
input: path.join(packagesDir, pkg, "index.ts"),
output: {
dir: path.join(packagesDir, pkg, distDir),
format: "es", // Ensure ESM format (import/export)
sourcemap: true,
preserveModules: true,
preserveModulesRoot: path.join(packagesDir, pkg), // ✅ Fixes imports
entryFileNames: "[name].mjs", // ✅ Ensures the output files have `.mjs`
},
plugins: [
resolve({ extensions: [".ts", ".tsx", ".mjs", ".js", ".json"] }),
typescript({
compilerOptions: {
module: "ESNext", // Ensure ES module output
moduleResolution: "node",
declaration: false, // Set to true if you want .d.ts files
esModuleInterop: false, // Avoids unnecessary __importDefault wrappers
importHelpers: false, // Avoids tslib imports
},
}),
],
external: (id) => !id.startsWith(".") && !path.isAbsolute(id), // Keep external dependencies as ESM
},
])),
...sortedWorkspaces.filter(() => prod).filter(pkg => !['puls-compiler', 'create-puls'].includes(pkg)).map((pkg) => ([
// Browser export
{
input: path.join(packagesDir, pkg, "index.ts"),
output: {
file: path.join(packagesDir, pkg, distDir, "index.global.js"),
format: "iife",
name: pkg.replaceAll('-', ''),
sourcemap: true,
globals: {
// Explicitly define global dependencies if necessary
},
},
plugins: [
resolve({ browser: true, preferBuiltins: false }), // Ensure all dependencies are bundled
commonjs({ requireReturnsDefault: "auto" }), // Convert CJS to ESM properly
typescript({
compilerOptions: {
module: "ESNext",
moduleResolution: "node",
esModuleInterop: true, // Ensures compatibility with CJS modules
importHelpers: false, // Prevents tslib from being imported
noEmitHelpers: true, // Avoids extra helper imports
},
})
],
external: [], // 🚀 Ensures all dependencies are bundled inside
},
]))
];
export default configs.flat();