-
Notifications
You must be signed in to change notification settings - Fork 4
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
Showing
12 changed files
with
286 additions
and
26 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
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" type="image/svg+xml" href="/vite.svg" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Vite + Vue</title> | ||
<!--app-head--> | ||
</head> | ||
<body> | ||
<div id="app"><!--app-html--></div> | ||
<script type="module" src="./src/entry-client.ts"></script> | ||
</body> | ||
</html> |
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,17 @@ | ||
{ | ||
"name": "vite-plugin-stylex-e2e-vue-ssr", | ||
"scripts": { | ||
"dev": "tsx ./server.ts" | ||
}, | ||
"dependencies": { | ||
"@vitejs/plugin-vue": "4.5.1", | ||
"express": "^4.18.2", | ||
"vite": "^5.0.6", | ||
"vite-plugin-stylex": "workspace:*", | ||
"vue": "^3.3.10" | ||
}, | ||
"devDependencies": { | ||
"@types/express": "^4.17.21", | ||
"tsx": "^4.6.2" | ||
} | ||
} |
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,36 @@ | ||
import fs from 'fs/promises' | ||
import path from 'path' | ||
import express from 'express' | ||
import { createChromeBrowser, genRandomPort } from '../../helper' | ||
|
||
export async function createSSRServer(configFile = path.join(__dirname, 'vite.config.mts')) { | ||
const app = express() | ||
const { createServer } = await import('vite') | ||
|
||
const viteServer = await createServer({ configFile, server: { middlewareMode: true }, appType: 'custom', base: '/', root: __dirname }) | ||
app.use(viteServer.middlewares) | ||
app.use('*', async (req, res) => { | ||
try { | ||
const url = req.originalUrl.replace('/', '') | ||
let template = await fs.readFile(path.join(__dirname, 'index.html'), 'utf8') | ||
template = await viteServer.transformIndexHtml(url, template) | ||
const { render } = await viteServer.ssrLoadModule('/src/entry-server.ts') | ||
const rendered = await render(url) | ||
const html = template | ||
.replace('<!--app-head-->', rendered.head ?? '') | ||
.replace('<!--app-html-->', rendered.html ?? '') | ||
res.status(200).set({ 'Content-Type': 'text/html' }).end(html) | ||
} catch (error) { | ||
viteServer.ssrFixStacktrace(error) | ||
res.status(500).end(error.stack) | ||
} | ||
}) | ||
const port = genRandomPort() | ||
app.listen(port) | ||
const { page } = await createChromeBrowser(port) | ||
return { app, page } | ||
} | ||
|
||
if (require.main === module) { | ||
createSSRServer() | ||
} |
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,33 @@ | ||
<template> | ||
<div> | ||
<div :class="className" role="button" @click="handleClick">Action</div> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import * as stylex from '@stylexjs/stylex' | ||
import { computed, defineComponent, ref } from 'vue' | ||
const styles = stylex.create({ | ||
text: { fontSize: '20px', cursor: 'pointer' }, | ||
blue: { | ||
color: 'blue' | ||
}, | ||
red: { | ||
color: 'red' | ||
} | ||
}) | ||
export default defineComponent({ | ||
setup() { | ||
const color = ref('red') | ||
const className = computed(() => stylex.props(styles.text, color.value === 'red' ? styles.red : styles.blue).className) | ||
const handleClick = () => { | ||
color.value = color.value === 'red' ? 'blue' : 'red' | ||
} | ||
return { className, handleClick } | ||
} | ||
}) | ||
</script> |
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 @@ | ||
import { createApp } from './main' | ||
|
||
const { app } = createApp() | ||
|
||
app.mount('#app') |
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,15 @@ | ||
import { renderToString } from 'vue/server-renderer' | ||
import { createApp } from './main' | ||
|
||
export async function render() { | ||
const { app } = createApp() | ||
|
||
// passing SSR context object which will be available via useSSRContext() | ||
// @vitejs/plugin-vue injects code into a component's setup() that registers | ||
// itself on ctx.modules. After the render, ctx.modules would contain all the | ||
// components that have been instantiated during this render call. | ||
const ctx = {} | ||
const html = await renderToString(app, ctx) | ||
|
||
return { html } | ||
} |
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 { createSSRApp } from 'vue' | ||
import App from './app.vue' | ||
|
||
// SSR requires a fresh app instance per request, therefore we export a function | ||
// that creates a fresh app instance. If using Vuex, we'd also be creating a | ||
// fresh store here. | ||
export function createApp() { | ||
const app = createSSRApp(App) | ||
return { app } | ||
} |
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 { defineConfig } from 'vite' | ||
import vue from '@vitejs/plugin-vue' | ||
import { stylexPlugin } from 'vite-plugin-stylex' | ||
|
||
export default defineConfig({ | ||
plugins: [vue(), stylexPlugin()] | ||
}) |
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
Oops, something went wrong.