Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support wasm module asset #548

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 2 additions & 18 deletions packages/react-server-next/src/vite/adapters/cloudflare/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { existsSync } from "node:fs";
import { cp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import type { PrerenderManifest } from "@hiogawa/react-server/plugin";
import { bundleEntry } from "../shared";

export async function build({ outDir }: { outDir: string }) {
const buildDir = join(process.cwd(), outDir);
Expand Down Expand Up @@ -50,27 +51,10 @@ export async function build({ outDir }: { outDir: string }) {

// worker
// https://developers.cloudflare.com/pages/functions/advanced-mode/
const esbuild = await import("esbuild");
const result = await esbuild.build({
await bundleEntry(buildDir, {
entryPoints: [join(buildDir, "server/index.js")],
outfile: join(adapterOutDir, "_worker.js"),
bundle: true,
minify: true,
metafile: true,
format: "esm",
platform: "browser",
external: ["node:async_hooks"],
define: {
"process.env.NODE_ENV": `"production"`,
},
logOverride: {
"ignored-bare-import": "silent",
},
});
await writeFile(
join(buildDir, "esbuild-metafile.json"),
JSON.stringify(result.metafile),
);
}

async function getPrerenderPaths(buildDir: string) {
Expand Down
32 changes: 32 additions & 0 deletions packages/react-server-next/src/vite/adapters/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import type { BuildOptions } from "esbuild";

// cf.
// https://github.com/sveltejs/kit/blob/144fb75c8280695d8aeb3228904bebb262e171a7/packages/adapter-cloudflare/index.js

export async function bundleEntry(buildDir: string, options: BuildOptions) {
const esbuild = await import("esbuild");
const result = await esbuild.build({
bundle: true,
minify: true,
metafile: true,
format: "esm",
platform: "browser",
external: ["node:async_hooks"],
define: {
"process.env.NODE_ENV": `"production"`,
},
logOverride: {
"ignored-bare-import": "silent",
},
loader: {
".wasm": "copy",
},
...options,
});
await writeFile(
join(buildDir, "esbuild-metafile.json"),
JSON.stringify(result.metafile),
);
}
21 changes: 3 additions & 18 deletions packages/react-server-next/src/vite/adapters/vercel/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { existsSync } from "node:fs";
import { cp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import type { PrerenderManifest } from "@hiogawa/react-server/plugin";
import { bundleEntry } from "../shared";

const configJson = {
version: 3,
Expand Down Expand Up @@ -93,27 +94,11 @@ export async function build({
);

// bundle function
const esbuild = await import("esbuild");
const result = await esbuild.build({
await bundleEntry(buildDir, {
entryPoints: [join(buildDir, "server/index.js")],
outfile: join(adapterOutDir, "functions/index.func/index.mjs"),
metafile: true,
bundle: true,
minify: true,
format: "esm",
outfile: join(adapterOutDir, "functions/index.func/index.js"),
platform: runtime === "node" ? "node" : "browser",
external: ["node:async_hooks"],
define: {
"process.env.NODE_ENV": `"production"`,
},
logOverride: {
"ignored-bare-import": "silent",
},
});
await writeFile(
join(buildDir, "esbuild-metafile.json"),
JSON.stringify(result.metafile),
);
}

async function getPrerenderManifest(buildDir: string) {
Expand Down
1 change: 1 addition & 0 deletions packages/react-server/examples/wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
minimal template for testing
12 changes: 12 additions & 0 deletions packages/react-server/examples/wasm/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type React from "react";

export default function Layout(props: React.PropsWithChildren) {
return (
<html>
<body>
<div>[Layout]</div>
{props.children}
</body>
</html>
);
}
3 changes: 3 additions & 0 deletions packages/react-server/examples/wasm/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <div>[Page]</div>;
}
13 changes: 13 additions & 0 deletions packages/react-server/examples/wasm/app/wasm/route.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// import someWasm from "./test.wasm";
// TODO:
// - dev: replace with
// export default WebAssembly.compile(...)
// - build: need esbuild-like copy loader for all three steps
// server build
// ssr build
// adapter bundle

export function GET(request: Request) {
request;
return new Response("hello");
}
23 changes: 23 additions & 0 deletions packages/react-server/examples/wasm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "@hiogawa/react-server-example-wasm",
"version": "0.0.0",
"private": true,
"type": "module",
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"@hiogawa/react-server": "workspace:*",
"next": "link:../../../react-server-next",
"react": "rc",
"react-dom": "rc",
"react-server-dom-webpack": "rc"
},
"devDependencies": {
"@types/react": "latest",
"@types/react-dom": "latest",
"vite": "latest"
}
}
17 changes: 17 additions & 0 deletions packages/react-server/examples/wasm/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"include": ["**/*.ts", "**/*.tsx"],
"compilerOptions": {
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"skipLibCheck": true,
"verbatimModuleSyntax": true,
"noEmit": true,
"moduleResolution": "Bundler",
"module": "ESNext",
"target": "ESNext",
"lib": ["ESNext", "DOM"],
"types": ["next"],
"jsx": "react-jsx"
}
}
6 changes: 6 additions & 0 deletions packages/react-server/examples/wasm/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import next from "next/vite";
import { defineConfig } from "vite";

export default defineConfig({
plugins: [next()],
});
28 changes: 28 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.