-
Notifications
You must be signed in to change notification settings - Fork 779
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c0374c
commit 07db54c
Showing
31 changed files
with
764 additions
and
241 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@cloudflare/vite-plugin": minor | ||
--- | ||
|
||
Add support for Wasm (WebAssembly) modules. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
# Keep environment variables out of version control | ||
.env |
16 changes: 16 additions & 0 deletions
16
packages/vite-plugin-cloudflare/playground/prisma/__tests__/prisma.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { expect, test } from "vitest"; | ||
import { getJsonResponse } from "../../__test-utils__"; | ||
|
||
// Need to remove the `.wrangler` directory and run the following commands before the tests. | ||
// I'm not sure how to do this with our testing setup so have skipped the test for now. | ||
// const commands = [ | ||
// `pnpm wrangler d1 migrations apply prisma-demo-db --local`, | ||
// `pnpm wrangler d1 execute prisma-demo-db --command "INSERT INTO \"User\" (\"email\", \"name\") VALUES ('[email protected]', 'Jane Doe (Local)');" --local`, | ||
// ]; | ||
|
||
test.skip("runs D1 query using Prisma", async () => { | ||
const result = await getJsonResponse(); | ||
expect(result).toEqual([ | ||
{ id: 1, email: "[email protected]", name: "Jane Doe (Local)" }, | ||
]); | ||
}); |
9 changes: 9 additions & 0 deletions
9
packages/vite-plugin-cloudflare/playground/prisma/migrations/0001_create_user_table.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
-- CreateTable | ||
CREATE TABLE "User" ( | ||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, | ||
"email" TEXT NOT NULL, | ||
"name" TEXT | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); |
27 changes: 27 additions & 0 deletions
27
packages/vite-plugin-cloudflare/playground/prisma/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "@playground/prisma", | ||
"private": true, | ||
"type": "module", | ||
"scripts": { | ||
"prebuild": "pnpm generate", | ||
"build": "vite build --app", | ||
"check:types": "tsc --build", | ||
"predev": "pnpm generate", | ||
"dev": "vite dev", | ||
"generate": "pnpm prisma generate", | ||
"migrate-db": "pnpm wrangler d1 migrations apply prisma-demo-db --local", | ||
"preview": "vite preview", | ||
"seed-db": "pnpm wrangler d1 execute prisma-demo-db --command \"INSERT INTO \"User\" (\"email\", \"name\") VALUES ('[email protected]', 'Jane Doe (Local)');\" --local" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/vite-plugin": "workspace:*", | ||
"@cloudflare/workers-tsconfig": "workspace:*", | ||
"@cloudflare/workers-types": "^4.20250121.0", | ||
"@prisma/adapter-d1": "^6.3.0", | ||
"@prisma/client": "^6.3.0", | ||
"prisma": "^6.3.0", | ||
"typescript": "catalog:default", | ||
"vite": "catalog:vite-plugin", | ||
"wrangler": "catalog:vite-plugin" | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/vite-plugin-cloudflare/playground/prisma/prisma/schema.prisma
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// This is your Prisma schema file, | ||
// learn more about it in the docs: https://pris.ly/d/prisma-schema | ||
|
||
generator client { | ||
provider = "prisma-client-js" | ||
previewFeatures = ["driverAdapters"] | ||
// Default output directory does not work with Vite. See https://github.com/vitejs/vite/issues/19036#issuecomment-2558791944 | ||
output = "../node_modules/@prisma/client-generated" | ||
} | ||
|
||
datasource db { | ||
provider = "sqlite" | ||
url = env("DATABASE_URL") | ||
} | ||
|
||
model User { | ||
id Int @id @default(autoincrement()) | ||
email String @unique | ||
name String? | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/vite-plugin-cloudflare/playground/prisma/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { PrismaD1 } from "@prisma/adapter-d1"; | ||
import { PrismaClient } from "@prisma/client-generated"; | ||
|
||
interface Env { | ||
DB: D1Database; | ||
} | ||
|
||
export default { | ||
async fetch(request, env) { | ||
const adapter = new PrismaD1(env.DB); | ||
const prisma = new PrismaClient({ adapter }); | ||
const users = await prisma.user.findMany(); | ||
|
||
return Response.json(users); | ||
}, | ||
} satisfies ExportedHandler<Env>; |
7 changes: 7 additions & 0 deletions
7
packages/vite-plugin-cloudflare/playground/prisma/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"files": [], | ||
"references": [ | ||
{ "path": "./tsconfig.node.json" }, | ||
{ "path": "./tsconfig.worker.json" } | ||
] | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/vite-plugin-cloudflare/playground/prisma/tsconfig.node.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": ["@cloudflare/workers-tsconfig/base.json"], | ||
"include": ["vite.config.ts", "__tests__"] | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/vite-plugin-cloudflare/playground/prisma/tsconfig.worker.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": ["@cloudflare/workers-tsconfig/worker.json"], | ||
"include": ["src"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"$schema": "http://turbo.build/schema.json", | ||
"extends": ["//"], | ||
"tasks": { | ||
"build": { | ||
"outputs": ["dist/**"] | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/vite-plugin-cloudflare/playground/prisma/vite.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { cloudflare } from "@cloudflare/vite-plugin"; | ||
import { defineConfig } from "vite"; | ||
|
||
export default defineConfig({ | ||
plugins: [cloudflare()], | ||
}); |
9 changes: 9 additions & 0 deletions
9
packages/vite-plugin-cloudflare/playground/prisma/wrangler.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
name = "worker" | ||
main = "./src/index.ts" | ||
compatibility_date = "2024-12-30" | ||
compatibility_flags = ["nodejs_compat"] | ||
|
||
[[d1_databases]] | ||
binding = "DB" | ||
database_name = "prisma-demo-db" | ||
database_id = "local" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# WebAssembly Playground | ||
|
||
The `minimal.wasm` file in the `src` directory was generated from the following C code: | ||
|
||
```c | ||
#include <stdint.h> | ||
|
||
int32_t add(int32_t a, int32_t b) { | ||
return a + b; | ||
} | ||
``` |
7 changes: 7 additions & 0 deletions
7
packages/vite-plugin-cloudflare/playground/wasm/__tests__/wasm.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { expect, test } from "vitest"; | ||
import { getJsonResponse } from "../../__test-utils__"; | ||
|
||
test("imports and instantiates WebAssembly", async () => { | ||
const result = await getJsonResponse(); | ||
expect(result).toEqual({ result: 7 }); | ||
}); |
19 changes: 19 additions & 0 deletions
19
packages/vite-plugin-cloudflare/playground/wasm/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "@playground/wasm", | ||
"private": true, | ||
"type": "module", | ||
"scripts": { | ||
"build": "vite build --app", | ||
"check:types": "tsc --build", | ||
"dev": "vite dev", | ||
"preview": "vite preview" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/vite-plugin": "workspace:*", | ||
"@cloudflare/workers-tsconfig": "workspace:*", | ||
"@cloudflare/workers-types": "^4.20250121.0", | ||
"typescript": "catalog:default", | ||
"vite": "catalog:vite-plugin", | ||
"wrangler": "catalog:vite-plugin" | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/vite-plugin-cloudflare/playground/wasm/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import wasm from "./minimal.wasm"; | ||
|
||
export default { | ||
async fetch() { | ||
const instance = await WebAssembly.instantiate(wasm); | ||
const result = instance.exports.add(3, 4); | ||
|
||
return Response.json({ result }); | ||
}, | ||
} satisfies ExportedHandler; |
Binary file not shown.
7 changes: 7 additions & 0 deletions
7
packages/vite-plugin-cloudflare/playground/wasm/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"files": [], | ||
"references": [ | ||
{ "path": "./tsconfig.node.json" }, | ||
{ "path": "./tsconfig.worker.json" } | ||
] | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/vite-plugin-cloudflare/playground/wasm/tsconfig.node.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": ["@cloudflare/workers-tsconfig/base.json"], | ||
"include": ["vite.config.ts", "__tests__"] | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/vite-plugin-cloudflare/playground/wasm/tsconfig.worker.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": ["@cloudflare/workers-tsconfig/worker.json"], | ||
"include": ["src"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"$schema": "http://turbo.build/schema.json", | ||
"extends": ["//"], | ||
"tasks": { | ||
"build": { | ||
"outputs": ["dist/**"] | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/vite-plugin-cloudflare/playground/wasm/vite.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { cloudflare } from "@cloudflare/vite-plugin"; | ||
import { defineConfig } from "vite"; | ||
|
||
export default defineConfig({ | ||
plugins: [cloudflare({ persistState: false })], | ||
}); |
3 changes: 3 additions & 0 deletions
3
packages/vite-plugin-cloudflare/playground/wasm/wrangler.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
name = "worker" | ||
main = "./src/index.ts" | ||
compatibility_date = "2024-12-30" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
export const ROUTER_WORKER_NAME = "__router-worker__"; | ||
export const ASSET_WORKER_NAME = "__asset-worker__"; | ||
export const ASSET_WORKERS_COMPATIBILITY_DATE = "2024-10-04"; | ||
// TODO: add `Text` and `Data` types (resolves https://github.com/cloudflare/workers-sdk/issues/8022) | ||
export const MODULE_TYPES = ["CompiledWasm"] as const; | ||
export type ModuleType = (typeof MODULE_TYPES)[number]; |
Oops, something went wrong.