Skip to content

Commit

Permalink
chore: setup vue example (#35)
Browse files Browse the repository at this point in the history
* chore: setup vue example

* chore: readme

* chore: tweak

* chore: no jsx

* chore: tsc

* test: e2e

* chore: fix preview

* test: tweak

* chore: readme

* chore: ssr stream

* chore: dev workerd

* chore: fix deps

* chore: readme
  • Loading branch information
hi-ogawa authored Apr 17, 2024
1 parent c4d707f commit 8a00769
Show file tree
Hide file tree
Showing 22 changed files with 491 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ jobs:
- run: pnpm -C examples/react-server test-e2e
- run: pnpm -C examples/react-server build
- run: pnpm -C examples/react-server test-e2e-preview
- run: pnpm -C examples/vue-ssr test-e2e
- run: pnpm -C examples/vue-ssr build
- run: pnpm -C examples/vue-ssr test-e2e-preview
- run: pnpm -C examples/vue-ssr test-e2e-workerd
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ pnpm -C examples/react-ssr dev
pnpm -C examples/react-ssr-workerd dev
pnpm -C examples/react-server dev
pnpm -C examples/workerd-cli cli
pnpm -C examples/vue-ssr dev-workerd
```
3 changes: 3 additions & 0 deletions examples/vue-ssr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# vue-ssr

https://vite-environment-examples-vue-ssr.hiro18181.workers.dev/
10 changes: 10 additions & 0 deletions examples/vue-ssr/e2e/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { test, expect } from "@playwright/test";

test("basic", async ({ page }) => {
const res = await page.goto("/");
expect(await res?.text()).toContain("hydrated: false");
await expect(page.locator("#root")).toContainText("hydrated: true");
await expect(page.locator("#root")).toContainText("Count: 0");
await page.getByRole("button", { name: "+" }).click();
await expect(page.locator("#root")).toContainText("Count: 1");
});
14 changes: 14 additions & 0 deletions examples/vue-ssr/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>vue-ssr</title>
<meta
name="viewport"
content="width=device-width, height=device-height, initial-scale=1.0"
/>
</head>
<body>
<script src="/src/entry-client" type="module"></script>
</body>
</html>
4 changes: 4 additions & 0 deletions examples/vue-ssr/misc/cloudflare-workers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# cloudflare-workers

copied from
https://github.com/hi-ogawa/vite-plugins/tree/992368d0c2f23dbb6c2d8c67a7ce0546d610a671/packages/react-server/examples/basic/misc/cloudflare-workers
23 changes: 23 additions & 0 deletions examples/vue-ssr/misc/cloudflare-workers/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash
set -eu -o pipefail

cd "$(dirname "${BASH_SOURCE[0]}")"

# clean
rm -rf dist
mkdir -p dist/server dist/client

# static
cp -r ../../dist/client/. dist/client
rm -rf dist/client/index.html

# server (bundle by ourselve instead of relying on wrangler)
npx esbuild ../../dist/server/index.js \
--outfile=dist/server/index.js \
--metafile=dist/esbuild-metafile.json \
--define:process.env.NODE_ENV='"production"' \
--log-override:ignored-bare-import=silent \
--bundle \
--minify \
--format=esm \
--platform=browser
6 changes: 6 additions & 0 deletions examples/vue-ssr/misc/cloudflare-workers/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name = "vite-environment-examples-vue-ssr"

main = "dist/server/index.js"
assets = "dist/client"
workers_dev = true
compatibility_date = "2024-01-01"
30 changes: 30 additions & 0 deletions examples/vue-ssr/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@hiogawa/vite-environment-examples-react-ssr",
"private": true,
"type": "module",
"scripts": {
"dev": "vite",
"dev-workerd": "vite --config vite.config.workerd.ts",
"build": "vite build --all",
"preview": "vite preview",
"tsc": "vue-tsc -b",
"tsc-dev": "vue-tsc -b --watch --preserveWatchOutput",
"test-e2e": "playwright test",
"test-e2e-preview": "E2E_PREVIEW=1 playwright test",
"test-e2e-workerd": "E2E_WORKERD=1 playwright test",
"cf-build": "SERVER_ENTRY=/src/adapters/workerd.ts pnpm build && bash misc/cloudflare-workers/build.sh",
"cf-preview": "cd misc/cloudflare-workers && wrangler dev",
"cf-release": "cd misc/cloudflare-workers && wrangler deploy"
},
"dependencies": {
"vue": "^3.4.23"
},
"devDependencies": {
"@hiogawa/vite-plugin-workerd": "workspace:*",
"@vitejs/plugin-vue": "^5.0.4",
"vue-tsc": "^2.0.13"
},
"volta": {
"extends": "../../package.json"
}
}
28 changes: 28 additions & 0 deletions examples/vue-ssr/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { defineConfig, devices } from "@playwright/test";

const port = Number(process.env["E2E_PORT"] || 6174);
const command = process.env["E2E_PREVIEW"]
? `pnpm preview --port ${port} --strict-port`
: process.env["E2E_WORKERD"]
? `pnpm dev-workerd --port ${port} --strict-port`
: `pnpm dev --port ${port} --strict-port`;

export default defineConfig({
testDir: "e2e",
use: {
trace: "on-first-retry",
},
projects: [
{
name: "chromium",
use: devices["Desktop Chrome"],
},
],
webServer: {
command,
port,
},
forbidOnly: !!process.env["CI"],
retries: process.env["CI"] ? 2 : 0,
reporter: "list",
});
Binary file added examples/vue-ssr/public/favicon.ico
Binary file not shown.
4 changes: 4 additions & 0 deletions examples/vue-ssr/src/adapters/node.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { createMiddleware } from "@hattip/adapter-node/native-fetch";
import { handler } from "../entry-server";

export default createMiddleware((ctx) => handler(ctx.request));
5 changes: 5 additions & 0 deletions examples/vue-ssr/src/adapters/workerd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { handler } from "../entry-server";

export default {
fetch: handler,
};
9 changes: 9 additions & 0 deletions examples/vue-ssr/src/entry-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createSSRApp } from "vue";
import Page from "./routes/page.vue";

async function main() {
const app = createSSRApp(Page);
app.mount("#root");
}

main();
25 changes: 25 additions & 0 deletions examples/vue-ssr/src/entry-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { renderToWebStream } from "vue/server-renderer";
import { createSSRApp } from "vue";
import Page from "./routes/page.vue";

export async function handler(_req: Request) {
const app = createSSRApp(Page);
const ssrStream = renderToWebStream(app);
const html = (await import("virtual:index-html")).default;
const htmlStream = ssrStream.pipeThrough(injectSsr(html));
return new Response(htmlStream, { headers: { "content-type": "text/html" } });
}

function injectSsr(html: string) {
let [pre, post] = html.split("<body>") as [string, string];
pre = pre + `<body><div id="root">`;
post = `</div>` + post;
return new TransformStream<Uint8Array, Uint8Array>({
start(controller) {
controller.enqueue(new TextEncoder().encode(pre));
},
flush(controller) {
controller.enqueue(new TextEncoder().encode(post));
},
});
}
17 changes: 17 additions & 0 deletions examples/vue-ssr/src/routes/page.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script setup lang="ts">
import { ref, onMounted } from "vue";
const count = ref(0);
const hydrated = ref(false);
onMounted(() => {
hydrated.value = true;
});
</script>

<template>
<div>hydrated: {{ hydrated }}</div>
<div>Count: {{ count }}</div>
<button type="button" @click="count--">-1</button>
<button type="button" @click="count++">+1</button>
</template>
4 changes: 4 additions & 0 deletions examples/vue-ssr/src/types/virtual.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "virtual:index-html" {
const src: string;
export default src;
}
5 changes: 5 additions & 0 deletions examples/vue-ssr/src/types/vue.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module "*.vue" {
import type { DefineComponent } from "vue";
const component: DefineComponent<{}, {}, any>;
export default component;
}
14 changes: 14 additions & 0 deletions examples/vue-ssr/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "@tsconfig/strictest/tsconfig.json",
"include": ["src", "vite.config.ts", "e2e", "playwright.config.ts"],
"compilerOptions": {
"exactOptionalPropertyTypes": false,
"verbatimModuleSyntax": true,
"noEmit": true,
"moduleResolution": "Bundler",
"module": "ESNext",
"target": "ESNext",
"lib": ["ESNext", "DOM"],
"types": ["vite/client"]
}
}
41 changes: 41 additions & 0 deletions examples/vue-ssr/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { defineConfig } from "vite";
import {
vitePluginSsrMiddleware,
vitePluginVirtualIndexHtml,
} from "../react-ssr/vite.config";
import vue from "@vitejs/plugin-vue";
import { resolve } from "path";

export default defineConfig((_env) => ({
clearScreen: false,
appType: "custom",
plugins: [
vue(),
vitePluginSsrMiddleware({
entry: process.env["SERVER_ENTRY"] || "/src/adapters/node",
preview: resolve("./dist/server/index.js"),
}),
vitePluginVirtualIndexHtml(),
],
environments: {
client: {
build: {
minify: false,
sourcemap: true,
outDir: "dist/client",
},
},
ssr: {
build: {
outDir: "dist/server",
},
},
},

builder: {
async buildEnvironments(builder, build) {
await build(builder.environments["client"]!);
await build(builder.environments["ssr"]!);
},
},
}));
29 changes: 29 additions & 0 deletions examples/vue-ssr/vite.config.workerd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import { vitePluginWorkerd } from "@hiogawa/vite-plugin-workerd";
import { vitePluginVirtualIndexHtml } from "../react-ssr/vite.config";
import { Log } from "miniflare";

export default defineConfig((_env) => ({
clearScreen: false,
appType: "custom",
plugins: [
vue(),
vitePluginWorkerd({
entry: "/src/adapters/workerd.ts",
miniflare: {
log: new Log(),
compatibilityDate: "2024-01-01",
},
}),
vitePluginVirtualIndexHtml(),
],
environments: {
workerd: {
webCompatible: true,
resolve: {
noExternal: true,
},
},
},
}));
Loading

0 comments on commit 8a00769

Please sign in to comment.