-
-
Notifications
You must be signed in to change notification settings - Fork 805
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
78 lines (70 loc) · 2.37 KB
/
Copy pathrollup.config.mjs
File metadata and controls
78 lines (70 loc) · 2.37 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
import { nodeResolve } from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import fs from "node:fs";
import path from "node:path";
function getAllFiles(dir, fileList = []) {
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = path.join(dir, file);
if (fs.statSync(filePath).isDirectory()) {
getAllFiles(filePath, fileList);
} else if (filePath.endsWith(".js")) {
fileList.push(path.normalize(filePath));
}
}
return fileList;
}
export default {
input: getAllFiles("src"),
output: {
dir: "lib",
format: "cjs",
preserveModules: true,
preserveModulesRoot: "src",
exports: "auto",
interop: "auto",
},
plugins: [nodeResolve(), commonjs(), json()],
external: (id, parentId) => {
if (id.startsWith("node:")) {
return true;
}
// Resolve the path if possible
let resolvedPath;
if (id.startsWith("src/")) {
resolvedPath = path.resolve(process.cwd(), id);
} else if (path.isAbsolute(id)) {
resolvedPath = id;
} else if (id.startsWith(".")) {
resolvedPath = path.resolve(
parentId ? path.dirname(parentId) : ".",
id,
);
} else {
// Named imports (node_modules) are external
return true;
}
const srcPath = path.resolve(process.cwd(), "src");
if (resolvedPath.startsWith(srcPath)) {
// It's inside src/.
// If it's an entry point (no parentId), we must treat it as NOT external.
if (!parentId) {
return false;
}
// For other files, check if they exist in src/
const exists =
fs.existsSync(resolvedPath) ||
fs.existsSync(`${resolvedPath}.js`) ||
fs.existsSync(`${resolvedPath}.mjs`);
if (exists) {
return false;
} // Exists in src/, so transpile it
// Doesn't exist in src/, so it must be a relative import to a file
// that we haven't ported yet, but will exist in lib/.
return true;
}
// Everything else (outside src/) is external
return true;
},
};