|
| 1 | +import { BunPlugin, fileURLToPath, pathToFileURL } from "bun"; |
| 2 | +import { readdir, unlink } from "node:fs/promises"; |
| 3 | +import { basename, join } from "node:path"; |
| 4 | + |
| 5 | +function escapeRegExp(string: string) { |
| 6 | + return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string |
| 7 | +} |
| 8 | + |
| 9 | +// Some bug in bun cause use FileSystemRouter with bun.build got Error: Unexpected |
| 10 | +async function* walkDir(path: string): AsyncGenerator<string> { |
| 11 | + const dirents = await readdir(path, { withFileTypes: true }); |
| 12 | + for (const dirent of dirents) { |
| 13 | + const finalPath = join(path, dirent.name); |
| 14 | + if (dirent.isDirectory()) { |
| 15 | + yield* walkDir(finalPath); |
| 16 | + } else { |
| 17 | + yield finalPath; |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +export async function build({ |
| 23 | + baseDir, |
| 24 | + buildDir = ".build", |
| 25 | + pageDir = "pages", |
| 26 | + hydrate, |
| 27 | + sourcemap, |
| 28 | + minify = Bun.env.NODE_ENV === "production", |
| 29 | + define, |
| 30 | + plugins, |
| 31 | +}: { |
| 32 | + baseDir: string; |
| 33 | + buildDir?: string; |
| 34 | + pageDir?: string; |
| 35 | + hydrate: string; |
| 36 | + sourcemap?: "external" | "none" | "inline"; |
| 37 | + minify?: boolean; |
| 38 | + define?: Record<string, string>; |
| 39 | + plugins?: BunPlugin[]; |
| 40 | +}) { |
| 41 | + const entrypoints = [join(baseDir, hydrate)]; |
| 42 | + const absPageDir = join(baseDir, pageDir); |
| 43 | + for await (const path of walkDir(absPageDir)) { |
| 44 | + entrypoints.push(path); |
| 45 | + } |
| 46 | + const result = await Bun.build({ |
| 47 | + entrypoints, |
| 48 | + sourcemap, |
| 49 | + target: "browser", |
| 50 | + outdir: join(baseDir, buildDir), |
| 51 | + splitting: true, |
| 52 | + minify, |
| 53 | + define: { |
| 54 | + "process.env.NODE_ENV": JSON.stringify(Bun.env.NODE_ENV || "development"), |
| 55 | + ...define, |
| 56 | + }, |
| 57 | + plugins: [ |
| 58 | + ...(plugins ?? []), |
| 59 | + { |
| 60 | + name: "bun-react-ssr", |
| 61 | + target: "browser", |
| 62 | + setup(build) { |
| 63 | + build.onLoad( |
| 64 | + { |
| 65 | + filter: new RegExp( |
| 66 | + "^" + escapeRegExp(absPageDir) + "/.*" + "\\.ts[x]$" |
| 67 | + ), |
| 68 | + }, |
| 69 | + async ({ path, loader }) => { |
| 70 | + const search = new URLSearchParams(); |
| 71 | + search.append("client", "1"); |
| 72 | + search.append("loader", loader); |
| 73 | + return { |
| 74 | + contents: |
| 75 | + "export { default } from " + |
| 76 | + JSON.stringify("./" + basename(path) + "?client"), |
| 77 | + loader: "ts", |
| 78 | + }; |
| 79 | + } |
| 80 | + ); |
| 81 | + build.onResolve( |
| 82 | + { filter: /\.ts[x]\?client$/ }, |
| 83 | + async ({ importer, path }) => { |
| 84 | + const url = pathToFileURL(importer); |
| 85 | + return { |
| 86 | + path: fileURLToPath(new URL(path, url)), |
| 87 | + namespace: "client", |
| 88 | + }; |
| 89 | + } |
| 90 | + ); |
| 91 | + build.onLoad( |
| 92 | + { namespace: "client", filter: /\.ts[x]$/ }, |
| 93 | + async ({ path, loader }) => { |
| 94 | + return { contents: await Bun.file(path).text(), loader }; |
| 95 | + } |
| 96 | + ); |
| 97 | + }, |
| 98 | + }, |
| 99 | + ], |
| 100 | + }); |
| 101 | + if (result.success) { |
| 102 | + for await (const path of walkDir(join(baseDir, buildDir))) { |
| 103 | + if (result.outputs.every((x) => x.path !== path)) { |
| 104 | + await unlink(path); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + return result; |
| 109 | +} |
0 commit comments