|
1 | | -import { Glob, fileURLToPath, pathToFileURL } from "bun"; |
| 1 | +import { Glob, fileURLToPath, pathToFileURL, type JavaScriptLoader } from "bun"; |
2 | 2 | import { basename, join, relative } from "node:path"; |
3 | 3 |
|
4 | 4 | function escapeRegExp(string: string) { |
@@ -52,40 +52,39 @@ export async function build({ |
52 | 52 | name: "bun-react-ssr", |
53 | 53 | target: "browser", |
54 | 54 | setup(build) { |
| 55 | + // workaround for https://github.com/oven-sh/bun/issues/12892 |
| 56 | + const trimmer = new Bun.Transpiler({ |
| 57 | + deadCodeElimination: true, |
| 58 | + treeShaking: true, |
| 59 | + exports: { eliminate: ["getServerSideProps"] }, |
| 60 | + trimUnusedImports: true, |
| 61 | + }); |
55 | 62 | build.onLoad( |
56 | 63 | { |
57 | 64 | filter: new RegExp( |
58 | 65 | "^" + escapeRegExp(absPageDir) + "/.*" + "\\.ts[x]$" |
59 | 66 | ), |
60 | 67 | }, |
61 | 68 | async ({ path, loader }) => { |
62 | | - const search = new URLSearchParams(); |
63 | | - search.append("client", "1"); |
64 | | - search.append("loader", loader); |
| 69 | + const contents = await Bun.file(path).text(); |
| 70 | + const tsloader = new Bun.Transpiler({ |
| 71 | + loader: loader as JavaScriptLoader, |
| 72 | + }); |
| 73 | + if ( |
| 74 | + !tsloader.scan(contents).exports.includes("getServerSideProps") |
| 75 | + ) { |
| 76 | + return { contents, loader }; |
| 77 | + } |
| 78 | + const js = await tsloader.transform( |
| 79 | + await Bun.file(path).text(), |
| 80 | + loader as JavaScriptLoader |
| 81 | + ); |
65 | 82 | return { |
66 | | - contents: |
67 | | - "export { default } from " + |
68 | | - JSON.stringify("./" + basename(path) + "?client"), |
69 | | - loader: "ts", |
| 83 | + contents: await trimmer.transform(js, "js"), |
| 84 | + loader: "js", |
70 | 85 | }; |
71 | 86 | } |
72 | 87 | ); |
73 | | - build.onResolve( |
74 | | - { filter: /\.ts[x]\?client$/ }, |
75 | | - async ({ importer, path }) => { |
76 | | - const url = pathToFileURL(importer); |
77 | | - return { |
78 | | - path: fileURLToPath(new URL(path, url)), |
79 | | - namespace: "client", |
80 | | - }; |
81 | | - } |
82 | | - ); |
83 | | - build.onLoad( |
84 | | - { namespace: "client", filter: /\.ts[x]$/ }, |
85 | | - async ({ path, loader }) => { |
86 | | - return { contents: await Bun.file(path).text(), loader }; |
87 | | - } |
88 | | - ); |
89 | 88 | }, |
90 | 89 | }, |
91 | 90 | ], |
|
0 commit comments