diff --git a/packages/vinxi-server-functions/server-handler.js b/packages/vinxi-server-functions/server-handler.js index e3d99150..9f7927c9 100644 --- a/packages/vinxi-server-functions/server-handler.js +++ b/packages/vinxi-server-functions/server-handler.js @@ -2,25 +2,6 @@ import invariant from "vinxi/lib/invariant"; import { eventHandler, toWebRequest } from "vinxi/server"; -async function loadModule(id) { - if (import.meta.env.DEV) { - const mod = await import( - /* @vite-ignore */ - import.meta.env.MANIFEST[import.meta.env.ROUTER_NAME].chunks[id].output - .path - ); - return mod; - } - - if (globalThis.$$chunks[id + ".js"]) { - return globalThis.$$chunks[id + ".js"]; - } - return await import( - /* @vite-ignore */ - import.meta.env.MANIFEST[import.meta.env.ROUTER_NAME].chunks[id].output.path - ); -} - export async function handleServerAction(event) { invariant(event.method === "POST", "Invalid method"); @@ -29,7 +10,7 @@ export async function handleServerAction(event) { invariant(typeof serverReference === "string", "Invalid server action"); // This is the client-side case const [filepath, name] = serverReference.split("#"); - const action = (await loadModule(filepath))[name]; + const action = (await import.meta.env.MANIFEST[import.meta.env.ROUTER_NAME].chunks[filepath].import())[name]; const text = await new Promise((resolve) => { const requestBody = []; event.node.req.on("data", (chunks) => { diff --git a/packages/vinxi/lib/manifest/prod-server-manifest.js b/packages/vinxi/lib/manifest/prod-server-manifest.js index 7b464819..9318314f 100644 --- a/packages/vinxi/lib/manifest/prod-server-manifest.js +++ b/packages/vinxi/lib/manifest/prod-server-manifest.js @@ -2,6 +2,7 @@ import invariant from "vinxi/lib/invariant"; import { handlerModule, join, virtualId } from "vinxi/lib/path"; import findAssetsInViteManifest from "./vite-manifest.js"; +import { pathToFileURL } from "node:url"; /** @typedef {import("../app.js").App & { config: { buildManifest: { [key:string]: any } }}} ProdApp */ @@ -66,7 +67,7 @@ export function createProdManifest(app) { if (globalThis.$$chunks[chunk + ".js"]) { return globalThis.$$chunks[chunk + ".js"]; } - return import(/* @vite-ignore */ chunkPath); + return import(/* @vite-ignore */ pathToFileURL(chunkPath).href); }, output: { path: chunkPath, diff --git a/packages/vinxi/types/manifest.d.ts b/packages/vinxi/types/manifest.d.ts index e567fcd2..71ca7454 100644 --- a/packages/vinxi/types/manifest.d.ts +++ b/packages/vinxi/types/manifest.d.ts @@ -20,7 +20,7 @@ export type Manifest = { [key: string]: { /** Assets needed by this entry point */ assets(): Promise; - + import(): Promise; output: { /** Path to built artifact for this entry point. */ path: string; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 62b80da5..b1bd8b33 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -137,7 +137,7 @@ importers: specifier: ^3.3.3 version: 3.3.5 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../../../packages/vinxi devDependencies: '@types/react': @@ -159,10 +159,10 @@ importers: specifier: 0.0.3 version: 0.0.3(react-dom@0.0.0-experimental-035a41c4e-20230704)(react@0.0.0-experimental-035a41c4e-20230704)(vite@4.5.0) '@vinxi/server-components': - specifier: 0.0.37 + specifier: 0.0.38 version: link:../../../../packages/vinxi-server-components '@vinxi/server-functions': - specifier: 0.0.37 + specifier: 0.0.38 version: link:../../../../packages/vinxi-server-functions '@vitejs/plugin-react': specifier: ^4.0.4 @@ -186,7 +186,7 @@ importers: specifier: ^1.9.0 version: 1.9.0 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../../../packages/vinxi devDependencies: '@types/react': @@ -220,7 +220,7 @@ importers: specifier: ^3.3.3 version: 3.3.5 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../../../packages/vinxi devDependencies: '@types/react': @@ -266,7 +266,7 @@ importers: specifier: ^3.3.3 version: 3.3.5 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../../../packages/vinxi vite-plugin-inspect: specifier: ^0.7.38 @@ -327,7 +327,7 @@ importers: specifier: ^3.3.3 version: 3.3.5 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../../../packages/vinxi devDependencies: '@types/react': @@ -376,7 +376,7 @@ importers: specifier: ^3.3.3 version: 3.3.5 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../../../packages/vinxi devDependencies: '@types/react': @@ -423,7 +423,7 @@ importers: specifier: ^3.3.3 version: 3.3.5 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../../../packages/vinxi examples/react/ssr/basic: @@ -450,7 +450,7 @@ importers: specifier: ^3.3.3 version: 3.3.5 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../../../packages/vinxi devDependencies: '@types/react': @@ -502,7 +502,7 @@ importers: specifier: ^1.7.4 version: 1.7.4 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../../../packages/vinxi ws: specifier: ^8.13.0 @@ -557,7 +557,7 @@ importers: specifier: ^3.3.3 version: 3.3.5 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../../../packages/vinxi devDependencies: '@types/react': @@ -594,7 +594,7 @@ importers: specifier: ^3.3.3 version: 3.3.5 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../../../packages/vinxi wouter: specifier: ^2.11.0 @@ -622,7 +622,7 @@ importers: specifier: ^3.3.3 version: 3.3.5 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../../../packages/vinxi vite: specifier: ^4.5.0 @@ -655,7 +655,7 @@ importers: specifier: ^3.3.3 version: 3.3.5 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../../../packages/vinxi vite-plugin-solid: specifier: ^2.7.0 @@ -691,7 +691,7 @@ importers: specifier: ^3.3.3 version: 3.3.5 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../../../packages/vinxi vite-plugin-solid: specifier: ^2.7.0 @@ -725,7 +725,7 @@ importers: specifier: ^3.3.3 version: 3.3.5 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../../../packages/vinxi devDependencies: axios: @@ -735,7 +735,7 @@ importers: examples/vanilla/empty: dependencies: vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../../packages/vinxi examples/vanilla/partyroom: @@ -753,7 +753,7 @@ importers: specifier: ^3.3.3 version: 3.3.5 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../../packages/vinxi examples/vanilla/spa: @@ -762,7 +762,7 @@ importers: specifier: ^1.5.10 version: 1.5.10 '@vinxi/server-functions': - specifier: 0.0.37 + specifier: 0.0.38 version: link:../../../packages/vinxi-server-functions autoprefixer: specifier: ^10.4.15 @@ -771,7 +771,7 @@ importers: specifier: ^3.3.3 version: 3.3.5 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../../packages/vinxi examples/vanilla/stack: @@ -780,7 +780,7 @@ importers: specifier: ^1.5.10 version: 1.5.10 '@vinxi/server-functions': - specifier: 0.0.37 + specifier: 0.0.38 version: link:../../../packages/vinxi-server-functions '@vitejs/plugin-react': specifier: ^4.0.4 @@ -792,7 +792,7 @@ importers: specifier: ^3.3.3 version: 3.3.5 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../../packages/vinxi devDependencies: '@types/resolve': @@ -823,7 +823,7 @@ importers: specifier: ^3.3.3 version: 3.3.5 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../../packages/vinxi frameworks/solid-start: @@ -845,7 +845,7 @@ importers: specifier: ^1.7.11 version: 1.8.5 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../packages/vinxi packages/doc: {} @@ -1062,7 +1062,7 @@ importers: specifier: ^2.5.5 version: 2.5.5 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../vinxi packages/vinxi-directives: @@ -1093,7 +1093,7 @@ importers: specifier: ^0.34.6 version: 0.34.6(vitest@0.28.5) vinxi: - specifier: ^0.0.45 + specifier: ^0.0.46 version: link:../vinxi vitest: specifier: ^0.28.5 @@ -1167,7 +1167,7 @@ importers: specifier: 0.0.0-experimental-035a41c4e-20230704 version: 0.0.0-experimental-035a41c4e-20230704 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../vinxi packages/vinxi-react-server: @@ -1195,7 +1195,7 @@ importers: specifier: 0.0.0-experimental-035a41c4e-20230704 version: 0.0.0-experimental-035a41c4e-20230704 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../vinxi packages/vinxi-router: @@ -1208,13 +1208,13 @@ importers: version: 4.2.1(typescript@5.1.6) devDependencies: vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../vinxi packages/vinxi-server-components: dependencies: '@vinxi/plugin-directives': - specifier: 0.0.37 + specifier: 0.0.38 version: link:../vinxi-directives acorn: specifier: ^8.10.0 @@ -1236,7 +1236,7 @@ importers: version: 0.23.4 devDependencies: vinxi: - specifier: ^0.0.45 + specifier: ^0.0.46 version: link:../vinxi vitest: specifier: ^0.28.5 @@ -1245,7 +1245,7 @@ importers: packages/vinxi-server-functions: dependencies: '@vinxi/plugin-directives': - specifier: 0.0.37 + specifier: 0.0.38 version: link:../vinxi-directives acorn: specifier: ^8.10.0 @@ -1267,7 +1267,7 @@ importers: version: 0.23.4 devDependencies: vinxi: - specifier: ^0.0.45 + specifier: ^0.0.46 version: link:../vinxi vitest: specifier: ^0.28.5 @@ -1282,7 +1282,7 @@ importers: specifier: ^1.7.11 version: 1.8.5 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../vinxi test: @@ -1370,7 +1370,7 @@ importers: specifier: 0.0.0-experimental-035a41c4e-20230704 version: 0.0.0-experimental-035a41c4e-20230704(react@0.0.0-experimental-035a41c4e-20230704) vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../../packages/vinxi devDependencies: '@types/react': @@ -1392,10 +1392,10 @@ importers: specifier: 0.0.3 version: 0.0.3(react-dom@0.0.0-experimental-035a41c4e-20230704)(react@0.0.0-experimental-035a41c4e-20230704)(vite@4.5.0) '@vinxi/server-components': - specifier: 0.0.37 + specifier: 0.0.38 version: link:../../../packages/vinxi-server-components '@vinxi/server-functions': - specifier: 0.0.37 + specifier: 0.0.38 version: link:../../../packages/vinxi-server-functions '@vitejs/plugin-react': specifier: ^4.0.4 @@ -1416,7 +1416,38 @@ importers: specifier: ^3.3.3 version: 3.3.5 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 + version: link:../../../packages/vinxi + devDependencies: + '@types/react': + specifier: ^18.2.21 + version: 18.2.33 + '@types/react-dom': + specifier: ^18.2.7 + version: 18.2.14 + + test/templates/react-srv-fn: + dependencies: + '@vinxi/react': + specifier: workspace:* + version: link:../../../packages/vinxi-react + '@vinxi/server-functions': + specifier: workspace:* + version: link:../../../packages/vinxi-server-functions + '@vitejs/plugin-react': + specifier: ^4.0.4 + version: 4.1.0(vite@4.5.0) + path-to-regexp: + specifier: ^6.2.1 + version: 6.2.1 + react: + specifier: 0.0.0-experimental-035a41c4e-20230704 + version: 0.0.0-experimental-035a41c4e-20230704 + react-dom: + specifier: 0.0.0-experimental-035a41c4e-20230704 + version: 0.0.0-experimental-035a41c4e-20230704(react@0.0.0-experimental-035a41c4e-20230704) + vinxi: + specifier: workspace:* version: link:../../../packages/vinxi devDependencies: '@types/react': @@ -1453,7 +1484,7 @@ importers: specifier: ^3.3.3 version: 3.3.5 vinxi: - specifier: 0.0.45 + specifier: 0.0.46 version: link:../../../packages/vinxi wouter: specifier: ^2.11.0 diff --git a/test/srv-fn.test.ts b/test/srv-fn.test.ts new file mode 100644 index 00000000..155010ab --- /dev/null +++ b/test/srv-fn.test.ts @@ -0,0 +1,77 @@ +import { expect, test, type Response } from "@playwright/test"; + +import type { AppFixture, Fixture } from "./helpers/create-fixture.js"; +import { createDevFixture, createFixture, js } from "./helpers/create-fixture.js"; +import { PlaywrightFixture } from "./helpers/playwright-fixture.js"; + +test.describe("srv-fn-dev", () => { + let fixture: Fixture; + let appFixture: AppFixture; + + test.beforeAll(async () => { + fixture = await createDevFixture({ + files: {}, + template: "react-srv-fn", + }); + + appFixture = await fixture.createServer(); + }); + + test.afterAll(async () => { + await appFixture.close(); + }); + + let logs: string[] = []; + + test.beforeEach(({ page }) => { + page.on("console", (msg) => { + logs.push(msg.text()); + }); + }); + + test("spa", async ({ page }) => { + let app = new PlaywrightFixture(appFixture, page); + await app.goto("/", true); + + let responses = app.collectResponses() + await app.clickElement("[data-test-id=button]"); + + expect(responses[0].status()).toEqual(200); + }); +}); + +test.describe("srv-fn-prod", () => { + let fixture: Fixture; + let appFixture: AppFixture; + + test.beforeAll(async () => { + fixture = await createFixture({ + files: {}, + template: "react-srv-fn", + }); + + appFixture = await fixture.createServer(); + }); + + test.afterAll(async () => { + await appFixture.close(); + }); + + let logs: string[] = []; + + test.beforeEach(({ page }) => { + page.on("console", (msg) => { + logs.push(msg.text()); + }); + }); + + test("spa", async ({ page }) => { + let app = new PlaywrightFixture(appFixture, page); + await app.goto("/", true); + + let responses = app.collectResponses(); + await app.clickElement("[data-test-id=button]"); + + expect(responses[0].status()).toEqual(200); + }); +}); \ No newline at end of file diff --git a/test/templates/react-srv-fn/CHANGELOG.md b/test/templates/react-srv-fn/CHANGELOG.md new file mode 100644 index 00000000..33f9a0ab --- /dev/null +++ b/test/templates/react-srv-fn/CHANGELOG.md @@ -0,0 +1,193 @@ +# app + +## null + +### Patch Changes + +- Updated dependencies [978b04a] + - vinxi@0.0.43 + +## null + +### Patch Changes + +- Updated dependencies [e8fdad4] +- Updated dependencies [291a5cf] + - vinxi@0.0.42 + +## null + +### Patch Changes + +- Updated dependencies [2e52d87] + - vinxi@0.0.41 + +## null + +### Patch Changes + +- Updated dependencies [0335776] + - vinxi@0.0.40 + +## null + +### Patch Changes + +- Updated dependencies [f237894] +- Updated dependencies [d620072] + - vinxi@0.0.39 + +## null + +### Patch Changes + +- Updated dependencies [1284791] + - vinxi@0.0.38 + +## null + +### Patch Changes + +- Updated dependencies [6ff575e] +- Updated dependencies [f2cbc3c] +- Updated dependencies [b437331] +- Updated dependencies [e87800e] + - vinxi@0.0.37 + +## null + +### Patch Changes + +- Updated dependencies [49e4e03] +- Updated dependencies [a009733] + - vinxi@0.0.36 + +## null + +### Patch Changes + +- vinxi@0.0.35 + +## null + +### Patch Changes + +- Updated dependencies [355daea] + - vinxi@0.0.34 + +## null + +### Patch Changes + +- Updated dependencies [1dec590] + - vinxi@0.0.33 + +## null + +### Patch Changes + +- Updated dependencies [982147a] + - vinxi@0.0.32 + +## null + +### Patch Changes + +- Updated dependencies [94f59aa] +- Updated dependencies [bc82d8e] +- Updated dependencies [1d8b542] +- Updated dependencies [5bf5e03] +- Updated dependencies [4c7fd35] +- Updated dependencies [0f4b3ee] +- Updated dependencies [ad62318] +- Updated dependencies [c223ab6] +- Updated dependencies [94f59aa] + - vinxi@0.0.31 + - @vinxi/react@0.0.11 + +## null + +### Patch Changes + +- Updated dependencies [fd06048] +- Updated dependencies [0f14555] +- Updated dependencies [82267c5] + - vinxi@0.0.30 + - @vinxi/react@0.0.10 + +## null + +### Patch Changes + +- Updated dependencies [8058084] + - vinxi@0.0.29 + +## null + +### Patch Changes + +- Updated dependencies [b934e84] +- Updated dependencies [17693dc] +- Updated dependencies [d6305b8] +- Updated dependencies [cb91c48] +- Updated dependencies [085116d] +- Updated dependencies [f1ee5b8] + - vinxi@0.0.28 + +## null + +### Patch Changes + +- Updated dependencies [7803042] + - vinxi@0.0.27 + +## null + +### Patch Changes + +- Updated dependencies [2b17e0d] + - vinxi@0.0.26 + +## null + +### Patch Changes + +- Updated dependencies [552d8ca] + - vinxi@0.0.25 + +## null + +### Patch Changes + +- Updated dependencies [47abc3c] + - vinxi@0.0.24 + +## null + +### Patch Changes + +- Updated dependencies [ec37a2e] + - @vinxi/react@0.0.9 + +## null + +### Patch Changes + +- Updated dependencies [76da329] + - @vinxi/react@0.0.8 + +## null + +### Patch Changes + +- Updated dependencies [46f3426] + - @vinxi/react@0.0.7 + - vinxi@0.0.23 + +## null + +### Patch Changes + +- Updated dependencies [5834c1a] + - @vinxi/react@0.0.6 + - vinxi@0.0.22 diff --git a/test/templates/react-srv-fn/README.md b/test/templates/react-srv-fn/README.md new file mode 100644 index 00000000..e69de29b diff --git a/test/templates/react-srv-fn/app.config.js b/test/templates/react-srv-fn/app.config.js new file mode 100644 index 00000000..ad91f9c7 --- /dev/null +++ b/test/templates/react-srv-fn/app.config.js @@ -0,0 +1,22 @@ +import reactRefresh from "@vitejs/plugin-react"; +import { serverFunctions } from "@vinxi/server-functions/plugin"; +import { createApp } from "vinxi"; + +export default createApp({ + routers: [ + { + name: "public", + mode: "static", + dir: "./public", + base: "/", + }, + { + name: "client", + mode: "spa", + handler: "./index.html", + target: "browser", + plugins: () => [serverFunctions.client(), reactRefresh()], + }, + serverFunctions.router() + ], +}); diff --git a/test/templates/react-srv-fn/app/client.tsx b/test/templates/react-srv-fn/app/client.tsx new file mode 100644 index 00000000..9eadeef4 --- /dev/null +++ b/test/templates/react-srv-fn/app/client.tsx @@ -0,0 +1,13 @@ +/// +import ReactDOM from "react-dom/client"; + +async function greetServer(name: string) { + "use server"; + console.log(`Hi, server. My name is ${name}.`); +} + +ReactDOM.createRoot(document.getElementById("root")).render( +
+ +
, +); \ No newline at end of file diff --git a/test/templates/react-srv-fn/index.html b/test/templates/react-srv-fn/index.html new file mode 100644 index 00000000..7a6d0b58 --- /dev/null +++ b/test/templates/react-srv-fn/index.html @@ -0,0 +1,13 @@ + + + + + + Document + + + +
+ + + \ No newline at end of file diff --git a/test/templates/react-srv-fn/package.json b/test/templates/react-srv-fn/package.json new file mode 100644 index 00000000..dd345ac2 --- /dev/null +++ b/test/templates/react-srv-fn/package.json @@ -0,0 +1,24 @@ +{ + "name": "test-react-srv-fn", + "type": "module", + "private": true, + "version": null, + "scripts": { + "dev": "vinxi dev", + "build": "vinxi build", + "start": "node .output/server/index.mjs" + }, + "dependencies": { + "@vinxi/react": "workspace:*", + "@vitejs/plugin-react": "^4.0.4", + "@vinxi/server-functions": "workspace:*", + "path-to-regexp": "^6.2.1", + "react": "0.0.0-experimental-035a41c4e-20230704", + "react-dom": "0.0.0-experimental-035a41c4e-20230704", + "vinxi": "workspace:*" + }, + "devDependencies": { + "@types/react": "^18.2.21", + "@types/react-dom": "^18.2.7" + } +} diff --git a/test/templates/react-srv-fn/public/favicon.ico b/test/templates/react-srv-fn/public/favicon.ico new file mode 100644 index 00000000..fb282da0 Binary files /dev/null and b/test/templates/react-srv-fn/public/favicon.ico differ diff --git a/test/templates/react-srv-fn/tsconfig.json b/test/templates/react-srv-fn/tsconfig.json new file mode 100644 index 00000000..1c09f8cb --- /dev/null +++ b/test/templates/react-srv-fn/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "allowSyntheticDefaultImports": true, + "esModuleInterop": true, + "target": "ESNext", + "module": "ESNext", + "moduleResolution": "Bundler", + "jsx": "react-jsx", + "baseUrl": "./", + "paths": { + "~/*": ["./src/*"] + } + } +}