Skip to content

Commit

Permalink
refactor: createFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Oct 6, 2024
1 parent 3391247 commit d2df817
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
6 changes: 5 additions & 1 deletion examples/child-process/src/routes/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export default async function Page(props: { url: URL }) {
}

async function Sleep(props: { ms: number }) {
await Bun.sleep(props.ms);
if (typeof Bun !== "undefined") {
await Bun.sleep(props.ms);
} else {
await new Promise((r) => setTimeout(r, props.ms));
}
return <div>Done!</div>;
}
60 changes: 31 additions & 29 deletions examples/child-process/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import http from "node:http";
import { join } from "node:path";
import { webToNodeHandler } from "@hiogawa/utils-node";
import react from "@vitejs/plugin-react";
import { DevEnvironment, defineConfig, isRunnableDevEnvironment } from "vite";
import {
DevEnvironment,
type DevEnvironmentOptions,
defineConfig,
isRunnableDevEnvironment,
} from "vite";

export default defineConfig((_env) => ({
clearScreen: false,
Expand Down Expand Up @@ -44,34 +49,10 @@ export default defineConfig((_env) => ({
externalConditions: ["react-server"],
},
dev: {
createEnvironment(name, config, _context) {
let command: string[];
if (process.env["CHILD_PROCESS_TYPE"] === "node") {
command = [
"node",
"--conditions",
"react-server",
join(import.meta.dirname, "./src/lib/vite/runtime/node.js"),
];
} else {
command = [
"bun",
"run",
"--conditions",
"react-server",
join(import.meta.dirname, "./src/lib/vite/runtime/bun.js"),
];
}
return new ChildProcessFetchDevEnvironment(
{ command },
name,
config,
{
// TODO
hot: false,
},
);
},
createEnvironment: ChildProcessFetchDevEnvironment.createFactory({
runtime: (process.env["CHILD_PROCESS_RUNTIME"] ?? "bun") as any,
conditions: ["react-server"],
}),
},
},
},
Expand All @@ -87,6 +68,27 @@ export class ChildProcessFetchDevEnvironment extends DevEnvironment {
public childUrl!: string;
public childUrlPromise!: PromiseWithResolvers<string>;

static createFactory(options: {
runtime: "node" | "bun";
conditions?: string[];
}): NonNullable<DevEnvironmentOptions["createEnvironment"]> {
return (name, config) => {
const command = [
options.runtime === "node" ? ["node"] : [],
options.runtime === "bun" ? ["bun", "run"] : [],
options.conditions ? ["--conditions", ...options.conditions] : [],
join(
import.meta.dirname,
`./src/lib/vite/runtime/${options.runtime}.js`,
),
].flat();
return new ChildProcessFetchDevEnvironment({ command }, name, config, {
// TODO
hot: false,
});
};
}

constructor(
public extraOptions: { command: string[] },
...args: ConstructorParameters<typeof DevEnvironment>
Expand Down

0 comments on commit d2df817

Please sign in to comment.