|
1 |
| -import { Glob, fileURLToPath, pathToFileURL, type JavaScriptLoader } from "bun"; |
| 1 | +import { Glob, fileURLToPath, pathToFileURL } from "bun"; |
2 | 2 | import { basename, join, relative } from "node:path";
|
3 | 3 |
|
4 | 4 | function escapeRegExp(string: string) {
|
@@ -52,40 +52,40 @@ 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 |
| - }); |
62 | 55 | build.onLoad(
|
63 | 56 | {
|
64 | 57 | filter: new RegExp(
|
65 | 58 | "^" + escapeRegExp(absPageDir) + "/.*" + "\\.ts[x]$"
|
66 | 59 | ),
|
67 | 60 | },
|
68 | 61 | async ({ path, loader }) => {
|
69 |
| - const contents = await Bun.file(path).text(); |
70 |
| - const tsloader = new Bun.Transpiler({ |
71 |
| - loader: loader as JavaScriptLoader, |
72 |
| - autoImportJSX: true, |
73 |
| - }); |
74 |
| - if ( |
75 |
| - !tsloader.scan(contents).exports.includes("getServerSideProps") |
76 |
| - ) { |
77 |
| - return { contents, loader }; |
78 |
| - } |
79 |
| - const js = await tsloader.transform( |
80 |
| - await Bun.file(path).text(), |
81 |
| - loader as JavaScriptLoader |
82 |
| - ); |
| 62 | + const search = new URLSearchParams(); |
| 63 | + search.append("client", "1"); |
| 64 | + search.append("loader", loader); |
83 | 65 | return {
|
84 |
| - contents: await trimmer.transform(js, "js"), |
85 |
| - loader: "js", |
| 66 | + contents: |
| 67 | + "export { default } from " + |
| 68 | + JSON.stringify("./" + basename(path) + "?client"), |
| 69 | + loader: "ts", |
86 | 70 | };
|
87 | 71 | }
|
88 | 72 | );
|
| 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 | 89 | },
|
90 | 90 | },
|
91 | 91 | ],
|
|
0 commit comments