Skip to content

Commit

Permalink
enable SPA support
Browse files Browse the repository at this point in the history
  • Loading branch information
nksaraf committed Jul 3, 2023
1 parent c70bd50 commit 472e693
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 9 deletions.
1 change: 1 addition & 0 deletions examples/spa/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vercel
27 changes: 27 additions & 0 deletions examples/spa/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import reactRefresh from "@vitejs/plugin-react";
import { createApp } from "vinxi";

export default createApp({
routers: [
{
name: "public",
mode: "static",
build: {
outDir: "./.build/client",
},
dir: "./public",
base: "/",
},
{
name: "client",
mode: "spa",
handler: "./index.html",
build: {
target: "browser",
outDir: "./.build/api",
plugins: () => [reactRefresh()],
},
base: "/",
},
],
});
4 changes: 4 additions & 0 deletions examples/spa/app/client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// <reference types="vinxi/client" />
import "./style.css";

alert("Hello world!");
7 changes: 7 additions & 0 deletions examples/spa/app/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

* {
color: red;
}
13 changes: 13 additions & 0 deletions examples/spa/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="icon" href="/favicon.ico" />
</head>
<body>
Hello world
<script src="./app/client.tsx" type="module"></script>
</body>
</html>
22 changes: 22 additions & 0 deletions examples/spa/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"type": "module",
"scripts": {
"dev": "vinxi dev",
"build": "vinxi build",
"start": "vinxi start"
},
"dependencies": {
"@picocss/pico": "^1.5.7",
"@vinxi/react": "workspace:^",
"@vitejs/plugin-react": "^4.0.1",
"autoprefixer": "^10.4.14",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"tailwindcss": "^3.3.2",
"vinxi": "workspace:^"
},
"devDependencies": {
"@types/react": "^18.2.14",
"@types/react-dom": "^18.2.6"
}
}
6 changes: 6 additions & 0 deletions examples/spa/postcss.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
Binary file added examples/spa/public/favicon.ico
Binary file not shown.
8 changes: 8 additions & 0 deletions examples/spa/tailwind.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./app/**/*.tsx", "./app/**/*.ts", "./app/**/*.js"],
theme: {
extend: {},
},
plugins: [],
};
46 changes: 40 additions & 6 deletions packages/vinxi/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ export async function createBuild(app, buildConfig) {
fileURLToPath(new URL("./prod-manifest.js", import.meta.url)),
],
handlers: [
...app.config.routers
.filter((router) => router.mode === "handler")
.map((router) => {
...app.config.routers.map((router) => {
if (router.mode === "handler") {
const bundlerManifest = JSON.parse(
readFileSync(
join(router.build.outDir, router.base, "manifest.json"),
Expand All @@ -51,8 +50,14 @@ export async function createBuild(app, buildConfig) {
bundlerManifest[relative(app.config.root, router.handler)].file,
),
};
}),
],
} else if (router.mode === "spa") {
return {
route: router.base.length === 1 ? "/**" : `${router.base}/**`,
handler: "#vinxi/spa",
};
}
}),
].filter(Boolean),
rollupConfig: {
plugins: [visualizer()],
},
Expand All @@ -64,7 +69,20 @@ export async function createBuild(app, buildConfig) {
baseURL: router.base,
passthrough: true,
})),
{ dir: ".build/api/_build", baseURL: "/_build", fallthrough: true },
...app.config.routers
.filter((router) => router.mode === "build")
.map((router) => ({
dir: join(router.build.outDir, router.base),
baseURL: router.base,
passthrough: true,
})),
...app.config.routers
.filter((router) => router.mode === "spa")
.map((router) => ({
dir: join(router.build.outDir, router.base),
baseURL: router.base,
passthrough: true,
})),
],
scanDirs: [],
appConfigFiles: [],
Expand Down Expand Up @@ -99,6 +117,22 @@ export async function createBuild(app, buildConfig) {
globalThis.app = prodApp
}
`,
"#vinxi/spa": () => {
const router = app.config.routers.find(
(router) => router.mode === "spa",
);
const indexHtml = readFileSync(
join(router.build.outDir, router.base, "index.html"),
"utf-8",
);
return `
import { eventHandler } from 'h3'
const html = ${JSON.stringify(indexHtml)}
export default eventHandler(event => {
return html
})
`;
},
},
});

Expand Down
14 changes: 11 additions & 3 deletions packages/vinxi/lib/dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createNitro } from "nitropack";

import { createDevManifest } from "./manifest/dev-server-manifest.js";
import { createDevServer as createDevNitroServer } from "./nitro-dev.js";
import { config } from "./plugins/config.js";
import { css } from "./plugins/css.js";
import { manifest } from "./plugins/manifest.js";
import { routes } from "./plugins/routes.js";
Expand Down Expand Up @@ -43,20 +44,25 @@ async function createViteServer(config) {
return vite.createServer(config);
}

const devPlugin = {
const targetDevPlugin = {
browser: () => [css()],
node: () => [],
};

const routerModeDevPlugin = {
spa: () => [config({ appType: "spa" })],
handler: () => [config({ appType: "custom" })],
};

async function createViteSSREventHandler(router, serveConfig) {
const viteDevServer = await createViteServer({
base: router.base,
appType: "custom",
plugins: [
routes(),
devEntries(),
manifest(),
devPlugin[router.build.target]?.(),
...(targetDevPlugin[router.build.target]?.() ?? []),
...(routerModeDevPlugin[router.mode]?.() ?? []),
...(router.build?.plugins?.() || []),
],
router,
Expand All @@ -78,6 +84,8 @@ async function createViteSSREventHandler(router, serveConfig) {
);
return handler(event);
});
} else if (router.mode === "spa") {
return defineEventHandler(fromNodeMiddleware(viteDevServer.middlewares));
} else {
return defineEventHandler(fromNodeMiddleware(viteDevServer.middlewares));
}
Expand Down
8 changes: 8 additions & 0 deletions packages/vinxi/lib/plugins/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function config(conf) {
return {
name: "vinxi:config",
config() {
return { ...conf };
},
};
}

0 comments on commit 472e693

Please sign in to comment.