-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbundle.js
More file actions
40 lines (36 loc) · 1.1 KB
/
Copy pathbundle.js
File metadata and controls
40 lines (36 loc) · 1.1 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
#!/usr/bin/env node
import { spawn } from "node:child_process";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { readdirSync, rmSync } from "node:fs";
const rootDir = path.dirname(fileURLToPath(import.meta.url));
const viteBin = path.join(rootDir, "node_modules", ".bin", "vite");
const KEEP_IN_DIST = new Set(["index.js"]);
async function bundle(bundleRoot) {
return new Promise((resolve, reject) => {
console.log(`\nBundling ${bundleRoot}...`);
const proc = spawn(
viteBin,
["build", "--config", "vite.bundle.config.ts"],
{
env: { ...process.env, BUNDLE_ROOT: bundleRoot },
stdio: "inherit",
cwd: rootDir,
},
);
proc.on("close", (code) =>
code === 0
? resolve()
: reject(new Error(`Failed to bundle ${bundleRoot}`)),
);
});
}
function pruneDist(distDir) {
for (const name of readdirSync(distDir)) {
if (KEEP_IN_DIST.has(name)) continue;
const full = path.join(distDir, name);
rmSync(full, { recursive: true });
}
}
await bundle(rootDir);
pruneDist(path.join(rootDir, "dist"));