Skip to content

Commit

Permalink
move examples around
Browse files Browse the repository at this point in the history
  • Loading branch information
nksaraf committed Jul 25, 2023
1 parent aeec305 commit fb28cf9
Show file tree
Hide file tree
Showing 36 changed files with 105 additions and 25 deletions.
File renamed without changes.
58 changes: 48 additions & 10 deletions examples/react/rsc/client/app.js → examples/react/rsc/spa/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import serverComponent from "@vinxi/react-server-dom-vite/plugin";
import reactRefresh from "@vitejs/plugin-react";
import { readFileSync } from "fs";
import { join } from "path";
import { createApp } from "vinxi";
import { virtual } from "vinxi/lib/plugins/virtual";

Expand Down Expand Up @@ -92,7 +94,7 @@ function serverComponents() {

/**
*
* @returns {import('vite').Plugin}
* @returns {import('vinxi').PluginOption}
*/
function clientComponents() {
let isBuild;
Expand All @@ -103,25 +105,53 @@ function clientComponents() {
isBuild = env.command === "build";
// @ts-ignore
const router = config.router;
// @ts-ignore
const app = config.app;

if (isBuild) {
const rscRouter = app.getRouter("rsc");

const reactServerManifest = JSON.parse(
readFileSync(".build/rsc/_rsc/react-server-manifest.json", "utf-8"),
readFileSync(
join(
rscRouter.build.outDir,
rscRouter.base,
"react-server-manifest.json",
),
"utf-8",
),
);

input = {
entry: getEntries(router)[0],
entry: router.handler,
...Object.fromEntries(
reactServerManifest.client.map((key) => {
return [hash(key), key];
}),
),
};

console.log(input);

return {
ssr: {
external: ["react", "react-dom", "@vinxi/react-server-dom-vite"],
},
optimizeDeps: {
include: [
"@vinxi/react-server-dom-vite",
"react-server-dom-vite",
"react",
"react-dom",
],
}, // optimizeDeps: {
// include: [
// "@vinxi/react-server-dom-vite/client.browser",
// "@vinxi/react-server-dom-vite/runtime",
// "react",
// "react-dom",
// ],
// },
build: {
rollupOptions: {
// preserve the export names of the server actions in chunks
Expand Down Expand Up @@ -154,24 +184,29 @@ function clientComponents() {
return {
optimizeDeps: {
include: [
"@vinxi/react-server-dom-vite/client",
"@vinxi/react-server-dom-vite/runtime",
"@vinxi/react-server-dom-vite",
"react-server-dom-vite",
"react",
"react-dom",
],
},
ssr: {
external: ["react", "react-dom", "@vinxi/react-server-dom-vite"],
external: [
"react",
"react-dom",
"@vinxi/react-server-dom-vite",
"react-server-dom-vite",
],
},
};
}
},

configResolved(config) {
if (isBuild) {
const reactServerManifest = JSON.parse(
readFileSync(".build/rsc/_rsc/react-server-manifest.json", "utf-8"),
);
// const reactServerManifest = JSON.parse(
// readFileSync(".build/rsc/_rsc/react-server-manifest.json", "utf-8"),
// );
config.build.rollupOptions.input = input;
}
},
Expand All @@ -192,7 +227,10 @@ function viteServer() {
},
},
virtual({
"#vite-dev-server": () => `export default viteServers['${router.name}']`,
"#vite-dev-server": ({ env }) =>
env.command === "build"
? `export default undefined`
: `export default viteServers['${router.name}']`,
}),
];
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ServerComponent } from "./server-component";
globalThis.__vite__ = createModuleLoader({
loadModule: async (id) => {
return import(
/* @vite-ignore */ import.meta.env.MANIFEST["client"].inputs[id].output
/* @vite-ignore */ import.meta.env.MANIFEST["client"].chunks[id].output
.path
);
},
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ import "vinxi/runtime/client";
import { ServerComponent } from "./server-component";

globalThis.__vite__ = createModuleLoader({
loadModule: import.meta.env.DEV
? async (id) => {
return import(
/* @vite-ignore */ import.meta.env.MANIFEST["client"].inputs[id]
.output.path
);
}
: async (id) => {
return import("/_build/" + id + ".js");
},
loadModule: async (id) => {
return import(
/* @vite-ignore */ import.meta.env.MANIFEST["client"].chunks[id].output
.path
);
},
});

startTransition(() => {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dependencies": {
"@picocss/pico": "^1.5.7",
"@vinxi/react": "workspace:^",
"@vinxi/react-server-dom-vite": "^0.0.2",
"@vitejs/plugin-react": "^4.0.1",
"autoprefixer": "^10.4.14",
"react": "0.0.0-experimental-035a41c4e-20230704",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions packages/vinxi/client.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/// <reference types="vite/client" />

declare interface Window {
manifest: any;
MANIFEST: any;
}

interface ImportMetaEnv {
// more env variables...
readonly manifest: any;
readonly MANIFEST: any;
}

interface ImportMeta {
Expand Down
16 changes: 16 additions & 0 deletions packages/vinxi/lib/manifest/client-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ const manifest = new Proxy(
handler: import.meta.env.DEV
? join(import.meta.env.CWD, import.meta.env.ROUTER_HANDLER)
: import.meta.env.ROUTER_HANDLER,
chunks: new Proxy(
{},
{
get(target, chunk) {
invariant(typeof chunk === "string", "Chunk expected");
let outputPath = import.meta.env.DEV
? join(import.meta.env.BASE_URL, "@fs", chunk)
: join(import.meta.env.BASE_URL, chunk + ".js");
return {
output: {
path: outputPath,
},
};
},
},
),
inputs: new Proxy(
{},
{
Expand Down
26 changes: 26 additions & 0 deletions packages/vinxi/lib/manifest/dev-server-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@ export function createDevManifest(app) {
return {};
},
handler: router.handler,
chunks: new Proxy(
{},
{
get(target, chunk) {
invariant(typeof chunk === "string", "Chunk expected");
let absolutePath = isAbsolute(chunk)
? chunk
: join(app.config.root, chunk);

let relativePath = relative(app.config.root, chunk);
if (router.build.target === "browser") {
return {
output: {
path: join(router.base, "@fs", absolutePath),
},
};
} else {
return {
output: {
path: absolutePath,
},
};
}
},
},
),
inputs: new Proxy(
{},
{
Expand Down
4 changes: 2 additions & 2 deletions packages/vinxi/server.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/// <reference types="vite/client" />

declare interface Window {
manifest: any;
MANIFEST: any;
}

interface ImportMetaEnv {
// more env variables...
readonly manifest: any;
readonly MANIFEST: any;
}

interface ImportMeta {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fb28cf9

Please sign in to comment.