Skip to content

Commit

Permalink
feat: add support for experimental Vite runtime (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyco130 authored Feb 14, 2024
1 parent bd7cda9 commit 2bfd377
Show file tree
Hide file tree
Showing 25 changed files with 526 additions and 78 deletions.
11 changes: 9 additions & 2 deletions ci/ci.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { kill } from "node:process";
const TEST_HOST = "http://localhost:3000";

const browser = await puppeteer.launch({
headless: "new",
headless: true,
defaultViewport: { width: 1200, height: 800 },
});

Expand Down Expand Up @@ -44,12 +44,16 @@ const [major, minor] = process.version
const cases: Array<{
framework: string;
file: string;
env: "production" | "development" | "with-loader";
env: "production" | "development" | "with-loader" | "with-vite-runtime";
}> = [
...baseCases.map((x) => ({ ...x, env: "production" as const })),
...baseCases.map((x) => ({ ...x, env: "development" as const })),
];

cases.push(
...baseCases.map((x) => ({ ...x, env: "with-vite-runtime" as const })),
);

const loaderAvailable =
(major > 16 || (major === 16 && minor >= 12)) && major < 20;

Expand Down Expand Up @@ -84,6 +88,9 @@ describe.each(cases)("$framework - $env ", ({ framework, env, file }) => {
(process.env.NODE_OPTIONS ?? "") +
" -r vavite/suppress-loader-warnings --loader vavite/node-loader",
}),
...(env === "with-vite-runtime" && {
USE_VITE_RUNTIME: "true",
}),
},
});

Expand Down
2 changes: 1 addition & 1 deletion examples/express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@types/node": "^20.11.16",
"typescript": "^5.3.3",
"vavite": "4.0.3",
"vite": "^5.0.12"
"vite": "^5.1.2"
},
"dependencies": {
"express": "^4.18.2"
Expand Down
2 changes: 2 additions & 0 deletions examples/express/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import express, { RequestHandler } from "express";
import viteDevServer from "vavite/vite-dev-server";

console.log("Running the server");

const app = express();

// This is an optional optimization to load routes lazily so that
Expand Down
2 changes: 1 addition & 1 deletion examples/fastify-vite-plugin-ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
"typescript": "^5.3.3",
"vavite": "4.0.3",
"vike": "^0.4.160",
"vite": "^5.0.12"
"vite": "^5.1.2"
}
}
2 changes: 1 addition & 1 deletion examples/fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@types/node": "^20.11.16",
"typescript": "^5.3.3",
"vavite": "4.0.3",
"vite": "^5.0.12"
"vite": "^5.1.2"
},
"dependencies": {
"fastify": "^4.26.0"
Expand Down
2 changes: 1 addition & 1 deletion examples/hapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@types/node": "^20.11.16",
"typescript": "^5.3.3",
"vavite": "4.0.3",
"vite": "^5.0.12"
"vite": "^5.1.2"
},
"dependencies": {
"@hapi/hapi": "^21.3.3"
Expand Down
2 changes: 1 addition & 1 deletion examples/koa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@types/node": "^20.11.16",
"typescript": "^5.3.3",
"vavite": "4.0.3",
"vite": "^5.0.12"
"vite": "^5.1.2"
},
"dependencies": {
"@koa/router": "^12.0.1",
Expand Down
2 changes: 1 addition & 1 deletion examples/nestjs-vite-plugin-ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"rollup-plugin-swc3": "^0.11.0",
"typescript": "^5.3.3",
"vavite": "4.0.3",
"vite": "^5.0.12",
"vite": "^5.1.2",
"vite-tsconfig-paths": "^4.3.1"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/nestjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"rollup-plugin-swc3": "^0.11.0",
"typescript": "^5.3.3",
"vavite": "4.0.3",
"vite": "^5.0.12"
"vite": "^5.1.2"
},
"dependencies": {
"@nestjs/common": "^10.3.2",
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-standalone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"@types/node": "^20.11.16",
"typescript": "^5.3.3",
"vavite": "4.0.3",
"vite": "^5.0.12"
"vite": "^5.1.2"
}
}
2 changes: 1 addition & 1 deletion examples/socket-io/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@types/node": "^20.11.16",
"typescript": "^5.3.3",
"vavite": "4.0.3",
"vite": "^5.0.12"
"vite": "^5.1.2"
},
"dependencies": {
"express": "^4.18.2",
Expand Down
2 changes: 1 addition & 1 deletion examples/ssr-react-express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@vitejs/plugin-react": "^4.2.1",
"typescript": "^5.3.3",
"vavite": "4.0.3",
"vite": "^5.0.12"
"vite": "^5.1.2"
},
"dependencies": {
"express": "^4.18.2",
Expand Down
2 changes: 1 addition & 1 deletion examples/ssr-vue-express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@vitejs/plugin-vue": "^5.0.3",
"typescript": "^5.3.3",
"vavite": "4.0.3",
"vite": "^5.0.12"
"vite": "^5.1.2"
},
"dependencies": {
"express": "^4.18.2",
Expand Down
2 changes: 1 addition & 1 deletion examples/vite-plugin-ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
"typescript": "^5.3.3",
"vavite": "4.0.3",
"vike": "^0.4.160",
"vite": "^5.0.12"
"vite": "^5.1.2"
}
}
2 changes: 1 addition & 1 deletion packages/connect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"sirv": "^2.0.4",
"tsup": "^8.0.1",
"typescript": "^5.3.3",
"vite": "^5.0.12"
"vite": "^5.1.2"
},
"dependencies": {
"@types/node": "^20.11.16"
Expand Down
26 changes: 25 additions & 1 deletion packages/connect/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export interface VaviteConnectOptions {
* @default true
*/
bundleSirv?: boolean;

/** Whether to use the vite runtime to execute the server entry.
* @experimental
* @default false (can be overriden by the USE_VITE_RUNTIME environment variable to "true")
*/
useViteRuntime?: boolean;
}

export function vaviteConnect(options: VaviteConnectOptions = {}): Plugin[] {
Expand All @@ -48,8 +54,11 @@ export function vaviteConnect(options: VaviteConnectOptions = {}): Plugin[] {
standalone = true,
clientAssetsDir = null,
bundleSirv = true,
useViteRuntime = process.env.USE_VITE_RUNTIME === "true",
} = options;

let viteRuntime: import("vite/runtime").ViteRuntime | undefined;

return [
{
name: "@vavite/connect:resolve",
Expand Down Expand Up @@ -117,7 +126,22 @@ export function vaviteConnect(options: VaviteConnectOptions = {}): Plugin[] {
req.url = req.originalUrl || req.url;

try {
const module = await server.ssrLoadModule(handlerEntry);
let runner:
| typeof server.ssrLoadModule
| import("vite/runtime").ViteRuntime["executeEntrypoint"] =
server.ssrLoadModule.bind(server);

if (useViteRuntime) {
if (!viteRuntime) {
const { createViteRuntime } = await import("vite");
viteRuntime = await createViteRuntime(server);
}

runner = viteRuntime.executeEntrypoint.bind(viteRuntime);
}

const module = await runner(handlerEntry);

await module.default(req, res, (error: unknown) => {
if (error) {
next(error);
Expand Down
2 changes: 1 addition & 1 deletion packages/expose-vite-dev-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@
"eslint": "^8.56.0",
"tsup": "^8.0.1",
"typescript": "^5.3.3",
"vite": "^5.0.12"
"vite": "^5.1.2"
}
}
2 changes: 1 addition & 1 deletion packages/multibuild-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"eslint": "^8.56.0",
"tsup": "^8.0.1",
"typescript": "^5.3.3",
"vite": "^5.0.12"
"vite": "^5.1.2"
},
"dependencies": {
"@types/node": "^20.11.16",
Expand Down
2 changes: 1 addition & 1 deletion packages/multibuild/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"eslint": "^8.56.0",
"tsup": "^8.0.1",
"typescript": "^5.3.3",
"vite": "^5.0.12"
"vite": "^5.1.2"
},
"dependencies": {
"@types/node": "^20.11.16",
Expand Down
2 changes: 1 addition & 1 deletion packages/node-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@
"sirv": "^2.0.4",
"tsup": "^8.0.1",
"typescript": "^5.3.3",
"vite": "^5.0.12"
"vite": "^5.1.2"
}
}
2 changes: 1 addition & 1 deletion packages/reloader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@
"eslint": "^8.56.0",
"tsup": "^8.0.1",
"typescript": "^5.3.3",
"vite": "^5.0.12"
"vite": "^5.1.2"
}
}
24 changes: 23 additions & 1 deletion packages/reloader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,24 @@ export interface VaviteReloaderOptions {
* @default false
*/
serveClientAssetsInDev?: boolean;

/** Whether to use the vite runtime to execute the server entry.
* @experimental
* @default false (can be overriden by the USE_VITE_RUNTIME environment variable to "true")
*/
useViteRuntime?: boolean;
}

export function reloader({
entry = "/server",
reloadOn = "any-change",
serveClientAssetsInDev = false,
useViteRuntime = process.env.USE_VITE_RUNTIME === "true",
}: VaviteReloaderOptions = {}): Plugin {
let resolvedEntry: string;
let entryDeps: Set<string>;
let globalSymbol: string;
let viteRuntime: import("vite/runtime").ViteRuntime | undefined;

function getModuleContents() {
return `export default ${globalSymbol}`;
Expand Down Expand Up @@ -69,7 +77,21 @@ export function reloader({
}

resolvedEntry = resolved.id;
await viteServer.ssrLoadModule(resolvedEntry);

let runner:
| typeof viteServer.ssrLoadModule
| import("vite/runtime").ViteRuntime["executeEntrypoint"] =
viteServer.ssrLoadModule.bind(viteServer);

if (useViteRuntime) {
if (!viteRuntime) {
const { createViteRuntime } = await import("vite");
viteRuntime = await createViteRuntime(viteServer);
}
runner = viteRuntime.executeEntrypoint.bind(viteRuntime);
}

await runner(resolvedEntry);

if (reloadOn === "any-change") return;

Expand Down
2 changes: 1 addition & 1 deletion packages/vavite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"sirv": "^2.0.4",
"tsup": "^8.0.1",
"typescript": "^5.3.3",
"vite": "^5.0.12"
"vite": "^5.1.2"
},
"dependencies": {
"@vavite/connect": "workspace:*",
Expand Down
8 changes: 8 additions & 0 deletions packages/vavite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export interface VaviteOptions {
* @default "any-change"
*/
reloadOn?: "any-change" | "static-deps-change";
/** Whether to use the vite runtime to execute the server entry.
* @experimental
* @default false (can be overriden by the USE_VITE_RUNTIME environment variable to "true")
*/
useViteRuntime?: boolean;
}

export function vavite(options: VaviteOptions): PluginOption {
Expand All @@ -60,6 +65,7 @@ export function vavite(options: VaviteOptions): PluginOption {
clientAssetsDir,
bundleSirv,
reloadOn,
useViteRuntime,
} = options;

if (!serverEntry && !handlerEntry) {
Expand Down Expand Up @@ -116,6 +122,7 @@ export function vavite(options: VaviteOptions): PluginOption {
standalone,
clientAssetsDir,
bundleSirv,
useViteRuntime,
}),
);
} else {
Expand All @@ -124,6 +131,7 @@ export function vavite(options: VaviteOptions): PluginOption {
entry: serverEntry,
serveClientAssetsInDev,
reloadOn,
useViteRuntime,
}),
);
}
Expand Down
Loading

0 comments on commit 2bfd377

Please sign in to comment.