Project Files
build.mjs
#!/usr/bin/env node
import { execSync } from "node:child_process";
import {
rmSync,
writeFileSync,
existsSync,
mkdirSync,
copyFileSync,
statSync,
chmodSync,
} from "node:fs";
import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = fileURLToPath(new URL(".", import.meta.url));
function clean() {
rmSync(resolve(__dirname, "dist"), { recursive: true, force: true });
rmSync(resolve(__dirname, "dist-temp"), { recursive: true, force: true });
}
function makeExec(p) {
try {
const st = statSync(p);
// add user/group/world execute bits if missing
if ((st.mode & 0o111) === 0) chmodSync(p, 0o755);
} catch {}
}
try {
console.log("clean: removing dist...");
clean();
// Rollup-only build
console.log(
"[rollup] step 1: compile TypeScript with SWC to dist-temp/src …"
);
execSync("npx swc src -d dist-temp/src --config-file .swcrc", {
stdio: "inherit",
cwd: resolve(__dirname),
});
// Ensure entry files exist at expected locations; create shims if SWC nested under src/src
try {
// 1) Main plugin entry (index.js)
const idxA = resolve(__dirname, "dist-temp", "src", "index.js");
const idxB = resolve(__dirname, "dist-temp", "src", "src", "index.js");
if (!existsSync(idxA) && existsSync(idxB)) {
mkdirSync(resolve(__dirname, "dist-temp", "src"), { recursive: true });
const shebang = "#!/usr/bin/env node\n";
const shim = `${shebang}export * from "./src/index.js";\nimport "./src/index.js";\n`;
writeFileSync(idxA, shim, "utf8");
makeExec(idxA);
console.log(
"[rollup] created entry shim dist-temp/src/index.js → ./src/index.js"
);
}
// 2) Legacy plugin entry under plugin/index.js (kept for compatibility if referenced)
const pluginA = resolve(
__dirname,
"dist-temp",
"src",
"plugin",
"index.js"
);
const pluginB = resolve(
__dirname,
"dist-temp",
"src",
"src",
"plugin",
"index.js"
);
if (!existsSync(pluginA) && existsSync(pluginB)) {
mkdirSync(resolve(__dirname, "dist-temp", "src", "plugin"), { recursive: true });
const shebang = "#!/usr/bin/env node\n";
const shim = `${shebang}import '../src/plugin/index.js';\n`;
writeFileSync(pluginA, shim, "utf8");
makeExec(pluginA);
console.log(
"[rollup] created entry shim dist-temp/src/plugin/index.js → ../src/plugin/index.js"
);
}
} catch {}
// Copy core-bundle.mjs to dist-temp so Rollup can resolve it.
// SWC nests output under dist-temp/src/src/, so place the bundle there too.
{
const bundleSrc = resolve(__dirname, "src", "core-bundle.mjs");
const bundleDstA = resolve(__dirname, "dist-temp", "src", "src", "core-bundle.mjs");
const bundleDstB = resolve(__dirname, "dist-temp", "src", "core-bundle.mjs");
if (existsSync(bundleSrc)) {
for (const dst of [bundleDstA, bundleDstB]) {
mkdirSync(resolve(dst, ".."), { recursive: true });
copyFileSync(bundleSrc, dst);
}
console.log("[rollup] copied core-bundle.mjs to dist-temp");
}
}
console.log("[rollup] step 2: bundle with Rollup …");
execSync("npx rollup -c", { stdio: "inherit", cwd: resolve(__dirname) });
console.log("[rollup] step 3: cleanup dist-temp …");
rmSync(resolve(__dirname, "dist-temp"), { recursive: true, force: true });
// Optional: Create executable CLI shim for plugin bundle
const cliPath = resolve(__dirname, "dist", "cli.js");
const shebang = "#!/usr/bin/env node\n";
// CommonJS shim to load the bundled entry (dist/index.js is CJS)
const cliCode = `${shebang}require('./index.js');\n`;
writeFileSync(cliPath, cliCode, "utf8");
makeExec(cliPath);
console.log("build complete (Rollup only).");
} catch (e) {
console.error("error occurred during the build process:", e);
process.exit(e.status || 1);
}