server/build.ts

65 lines
1.8 KiB
TypeScript
Raw Permalink Normal View History

2024-04-07 10:56:15 +02:00
import { $ } from "bun";
import chalk from "chalk";
import ora from "ora";
import { routes } from "~routes";
2024-03-12 19:27:50 +01:00
const buildSpinner = ora("Building").start();
2024-04-09 13:14:53 +02:00
await $`rm -rf dist && mkdir dist`;
2023-12-09 03:59:02 +01:00
await Bun.build({
2024-04-07 07:30:49 +02:00
entrypoints: [
"index.ts",
"cli/index.ts",
2024-04-07 07:30:49 +02:00
// Force Bun to include endpoints
...Object.values(routes),
2024-04-07 07:30:49 +02:00
],
outdir: "dist",
2024-04-07 07:30:49 +02:00
target: "bun",
splitting: true,
2024-04-14 13:37:32 +02:00
minify: false,
external: ["bullmq", "unzipit"],
2024-04-07 07:30:49 +02:00
}).then((output) => {
if (!output.success) {
console.log(output.logs);
process.exit(1);
2024-04-07 07:30:49 +02:00
}
});
buildSpinner.text = "Transforming";
2024-04-07 10:56:15 +02:00
// Fix for wrong Bun file resolution, replaces node_modules with ./node_modules inside all dynamic imports
// I apologize for this
await $`sed -i 's|import("node_modules/|import("./node_modules/|g' dist/*.js`;
await $`sed -i 's|import"node_modules/|import"./node_modules/|g' dist/**/*.js`;
2024-05-03 02:25:20 +02:00
// Replace /temp/node_modules with ./node_modules
await $`sed -i 's|/temp/node_modules|./node_modules|g' dist/**/*.js`;
2024-04-07 10:56:15 +02:00
2024-04-14 03:26:19 +02:00
// Copy Drizzle migrations to dist
await $`cp -r drizzle dist/drizzle`;
2024-04-07 15:19:21 +02:00
// Copy Sharp to dist
2024-04-09 14:51:03 +02:00
await $`mkdir -p dist/node_modules/@img`;
2024-04-09 15:00:48 +02:00
await $`cp -r node_modules/@img/sharp-libvips-linux-* dist/node_modules/@img`;
await $`cp -r node_modules/@img/sharp-linux-* dist/node_modules/@img`;
2024-04-07 15:19:21 +02:00
// Copy unzipit and uzip-module to dist
await $`cp -r node_modules/unzipit dist/node_modules/unzipit`;
await $`cp -r node_modules/uzip-module dist/node_modules/uzip-module`;
2024-03-13 09:10:32 +01:00
// Copy the Bee Movie script from pages
2024-04-09 13:55:03 +02:00
await $`cp beemovie.txt dist/beemovie.txt`;
2024-03-13 09:10:32 +01:00
// Copy package.json
await $`cp package.json dist/package.json`;
// Copy cli/theme.json
await $`cp cli/theme.json dist/cli/theme.json`;
buildSpinner.stop();
console.log(
`${chalk.green("✓")} Built project. You can now run it with ${chalk.green(
"bun run dist/index.js",
)}`,
);