-
-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathrolldown.config.js
More file actions
110 lines (97 loc) · 2.84 KB
/
rolldown.config.js
File metadata and controls
110 lines (97 loc) · 2.84 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
import { builtinModules } from "node:module";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { defineConfig } from "rolldown";
import { dts } from "rolldown-plugin-dts";
const nodeBuiltins = new Set(builtinModules);
function nodeProtocolPlugin() {
return {
name: "node-protocol",
resolveId(source) {
if (nodeBuiltins.has(source)) {
return { id: `node:${source}`, external: true };
}
},
};
}
function defineVersionPlugin(version) {
const replacement = JSON.stringify(version);
return {
name: "define-version",
transform(code, id) {
if (code.includes("__HOCUSPOCUS_VERSION__")) {
return { code: code.replaceAll("__HOCUSPOCUS_VERSION__", replacement) };
}
},
};
}
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
function getWorkspacePackages() {
const packagePaths = fs.globSync("packages/*/package.json", { cwd: __dirname });
return packagePaths
.map((pkgPath) => {
const fullPath = path.join(__dirname, pkgPath);
const pkgJson = JSON.parse(fs.readFileSync(fullPath, "utf-8"));
return { location: path.dirname(fullPath), package: pkgJson };
})
.filter((pkg) => pkg.package.exports && !["@hocuspocus/docs", "@hocuspocus/demo"].includes(pkg.package.name));
}
function getExternalDependencies(pkgJson) {
return [
...Object.keys(pkgJson.dependencies || {}),
...Object.keys(pkgJson.peerDependencies || {}),
...Object.keys(pkgJson.devDependencies || {}),
];
}
const packages = getWorkspacePackages();
// Build aliases dynamically from all packages
const aliases = Object.fromEntries(
packages.map((pkg) => [pkg.package.name, path.join(pkg.location, "src")]),
);
const configs = packages.flatMap((pkg) => {
const basePath = pkg.location;
const input = path.join(basePath, "src/index.ts");
const { name, exports } = pkg.package;
const external = getExternalDependencies(pkg.package);
// Main bundle config (ESM + CJS)
const bundleConfig = defineConfig({
input,
external: [
...external,
// Also externalize Node built-ins
/^node:/,
],
plugins: [nodeProtocolPlugin(), defineVersionPlugin(pkg.package.version)],
output: [
{
file: path.join(basePath, exports.default.require),
format: "cjs",
sourcemap: true,
exports: "auto",
},
{
file: path.join(basePath, exports.default.import),
format: "esm",
sourcemap: true,
},
],
resolve: {
alias: aliases,
},
});
// DTS config for type declarations
// Don't use aliases here - we want to reference external package types, not inline them
const dtsConfig = defineConfig({
input,
external: [...external, /^node:/],
plugins: [nodeProtocolPlugin(), dts({ emitDtsOnly: true })],
output: {
dir: path.join(basePath, "dist"),
format: "esm",
},
});
return [bundleConfig, dtsConfig];
});
export default configs;