Skip to content

Commit 9f46666

Browse files
committed
simplify build
1 parent b586bd9 commit 9f46666

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

build.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Glob, fileURLToPath, pathToFileURL } from "bun";
1+
import { Glob, fileURLToPath, pathToFileURL, type JavaScriptLoader } from "bun";
22
import { basename, join, relative } from "node:path";
33

44
function escapeRegExp(string: string) {
@@ -52,40 +52,39 @@ export async function build({
5252
name: "bun-react-ssr",
5353
target: "browser",
5454
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+
});
5562
build.onLoad(
5663
{
5764
filter: new RegExp(
5865
"^" + escapeRegExp(absPageDir) + "/.*" + "\\.ts[x]$"
5966
),
6067
},
6168
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+
);
6582
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",
7085
};
7186
}
7287
);
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-
);
8988
},
9089
},
9190
],

0 commit comments

Comments
 (0)