mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-15 20:17:22 +00:00
fix tsup broken watch
This commit is contained in:
57
app/build.ts
57
app/build.ts
@@ -2,6 +2,8 @@ import { $ } from "bun";
|
|||||||
import * as tsup from "tsup";
|
import * as tsup from "tsup";
|
||||||
import pkg from "./package.json" with { type: "json" };
|
import pkg from "./package.json" with { type: "json" };
|
||||||
import c from "picocolors";
|
import c from "picocolors";
|
||||||
|
import { watch as fsWatch } from "node:fs";
|
||||||
|
import { join } from "node:path";
|
||||||
|
|
||||||
const args = process.argv.slice(2);
|
const args = process.argv.slice(2);
|
||||||
const watch = args.includes("--watch");
|
const watch = args.includes("--watch");
|
||||||
@@ -83,7 +85,8 @@ async function buildApi() {
|
|||||||
await tsup.build({
|
await tsup.build({
|
||||||
minify,
|
minify,
|
||||||
sourcemap,
|
sourcemap,
|
||||||
watch,
|
// don't use tsup's broken watch, we'll handle it ourselves
|
||||||
|
watch: false,
|
||||||
define,
|
define,
|
||||||
entry: [
|
entry: [
|
||||||
"src/index.ts",
|
"src/index.ts",
|
||||||
@@ -121,7 +124,7 @@ async function buildUi() {
|
|||||||
const base = {
|
const base = {
|
||||||
minify,
|
minify,
|
||||||
sourcemap,
|
sourcemap,
|
||||||
watch,
|
watch: false,
|
||||||
define,
|
define,
|
||||||
external: [
|
external: [
|
||||||
...external,
|
...external,
|
||||||
@@ -180,7 +183,7 @@ async function buildUiElements() {
|
|||||||
await tsup.build({
|
await tsup.build({
|
||||||
minify,
|
minify,
|
||||||
sourcemap,
|
sourcemap,
|
||||||
watch,
|
watch: false,
|
||||||
define,
|
define,
|
||||||
entry: ["src/ui/elements/index.ts"],
|
entry: ["src/ui/elements/index.ts"],
|
||||||
outDir: "dist/ui/elements",
|
outDir: "dist/ui/elements",
|
||||||
@@ -225,7 +228,7 @@ function baseConfig(adapter: string, overrides: Partial<tsup.Options> = {}): tsu
|
|||||||
return {
|
return {
|
||||||
minify,
|
minify,
|
||||||
sourcemap,
|
sourcemap,
|
||||||
watch,
|
watch: false,
|
||||||
entry: [`src/adapter/${adapter}/index.ts`],
|
entry: [`src/adapter/${adapter}/index.ts`],
|
||||||
format: ["esm"],
|
format: ["esm"],
|
||||||
platform: "neutral",
|
platform: "neutral",
|
||||||
@@ -335,4 +338,48 @@ async function buildAdapters() {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
await Promise.all([buildApi(), buildUi(), buildUiElements(), buildAdapters()]);
|
async function buildAll() {
|
||||||
|
await Promise.all([buildApi(), buildUi(), buildUiElements(), buildAdapters()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// initial build
|
||||||
|
await buildAll();
|
||||||
|
|
||||||
|
// custom watcher since tsup's watch is broken in 8.3.5+
|
||||||
|
if (watch) {
|
||||||
|
oldConsole.log(c.cyan("[Watch]"), "watching for changes in src/...");
|
||||||
|
|
||||||
|
let debounceTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
let isBuilding = false;
|
||||||
|
|
||||||
|
const rebuild = async () => {
|
||||||
|
if (isBuilding) return;
|
||||||
|
isBuilding = true;
|
||||||
|
oldConsole.log(c.cyan("[Watch]"), "rebuilding...");
|
||||||
|
try {
|
||||||
|
await buildAll();
|
||||||
|
oldConsole.log(c.cyan("[Watch]"), c.green("done"));
|
||||||
|
} catch (e) {
|
||||||
|
oldConsole.warn(c.cyan("[Watch]"), c.red("build failed"), e);
|
||||||
|
}
|
||||||
|
isBuilding = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
const debouncedRebuild = () => {
|
||||||
|
if (debounceTimer) clearTimeout(debounceTimer);
|
||||||
|
debounceTimer = setTimeout(rebuild, 100);
|
||||||
|
};
|
||||||
|
|
||||||
|
// watch src directory recursively
|
||||||
|
fsWatch(join(import.meta.dir, "src"), { recursive: true }, (event, filename) => {
|
||||||
|
if (!filename) return;
|
||||||
|
// ignore non-source files
|
||||||
|
if (!filename.endsWith(".ts") && !filename.endsWith(".tsx") && !filename.endsWith(".css"))
|
||||||
|
return;
|
||||||
|
oldConsole.log(c.cyan("[Watch]"), c.dim(`${event}: ${filename}`));
|
||||||
|
debouncedRebuild();
|
||||||
|
});
|
||||||
|
|
||||||
|
// keep process alive
|
||||||
|
await new Promise(() => {});
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user