-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathsetup-dual-build.js
More file actions
67 lines (50 loc) · 1.73 KB
/
setup-dual-build.js
File metadata and controls
67 lines (50 loc) · 1.73 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
const fs = require("fs");
const path = require("path");
const PACKAGES_DIR = path.join(__dirname, "packages");
const ROOT_PACKAGE_JSON = path.join(__dirname, "package.json");
function updatePackageJson(pkgPath) {
const pkgJsonPath = path.join(pkgPath, "package.json");
if (!fs.existsSync(pkgJsonPath)) return;
const pkg = JSON.parse(fs.readFileSync(pkgJsonPath, "utf-8"));
// Clean up any existing build script
if (pkg.scripts && pkg.scripts.build) {
delete pkg.scripts.build;
}
// Ensure scripts object exists
pkg.scripts = pkg.scripts || {};
// Output fields
pkg.main = "dist/cjs/index.js";
pkg.module = "dist/esm/index.js";
pkg.types = "dist/esm/index.d.ts";
// Exports field
pkg.exports = {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
}
};
// Save updated package.json
fs.writeFileSync(pkgJsonPath, JSON.stringify(pkg, null, 2));
console.log(`✅ Updated ${pkg.name}`);
}
function updateRootBuildScript() {
if (!fs.existsSync(ROOT_PACKAGE_JSON)) return;
const rootPkg = JSON.parse(fs.readFileSync(ROOT_PACKAGE_JSON, "utf-8"));
rootPkg.scripts = rootPkg.scripts || {};
rootPkg.scripts.build = "pnpm -r exec tsup";
fs.writeFileSync(ROOT_PACKAGE_JSON, JSON.stringify(rootPkg, null, 2));
console.log("📦 Root package.json build script updated");
}
function main() {
const packages = fs.readdirSync(PACKAGES_DIR);
packages.forEach((pkgName) => {
const pkgPath = path.join(PACKAGES_DIR, pkgName);
const stats = fs.statSync(pkgPath);
if (stats.isDirectory()) {
updatePackageJson(pkgPath);
}
});
updateRootBuildScript();
console.log("\n🚀 Dual-format ESM+CJS support is now configured using global tsup config!");
}
main();