diff --git a/.vscode/settings.json b/.vscode/settings.json index 25fa621..940599e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,4 @@ { - "typescript.tsdk": "node_modules/typescript/lib" + "typescript.tsdk": "node_modules/typescript/lib", + "vitest.disableWorkspaceWarning": true } diff --git a/examples/bun-react-router/package.json b/examples/bun-react-router/package.json index c825907..42be67f 100644 --- a/examples/bun-react-router/package.json +++ b/examples/bun-react-router/package.json @@ -15,7 +15,7 @@ "dependencies": { "@elysiajs/static": "1.1.1", "@ssrx/react": "latest", - "elysia": "1.1.12", + "elysia": "1.1.16", "react": "18.3.1", "react-dom": "18.3.1", "react-router-dom": "6.26.2" diff --git a/examples/remix-vite/app/routes/wait.tsx b/examples/remix-vite/app/routes/wait.tsx index 4edec61..3b1bfd6 100644 --- a/examples/remix-vite/app/routes/wait.tsx +++ b/examples/remix-vite/app/routes/wait.tsx @@ -1,4 +1,4 @@ -import { useQuery } from '@tanstack/react-query'; +import { useSuspenseQuery } from '@tanstack/react-query'; import { Suspense } from 'react'; import { sleep } from '~/utils.ts'; @@ -63,7 +63,7 @@ export default function Wait() { } function useWaitQuery(props: { wait: number; defer?: boolean }) { - const query = useQuery({ + const query = useSuspenseQuery({ queryKey: ['wait', props.wait], meta: { deferStream: props.defer }, queryFn: async () => { diff --git a/examples/remix-vite/package.json b/examples/remix-vite/package.json index 35f008a..605e39d 100644 --- a/examples/remix-vite/package.json +++ b/examples/remix-vite/package.json @@ -63,6 +63,7 @@ "typescript": "5.6.2", "typescript-remix-routes-plugin": "1.0.1", "vite": "5.4.5", + "vite-env-only": "3.0.3", "vite-tsconfig-paths": "5.0.1" } } diff --git a/examples/remix-vite/vite.config.ts b/examples/remix-vite/vite.config.ts index 6e4da9b..aeb25d9 100644 --- a/examples/remix-vite/vite.config.ts +++ b/examples/remix-vite/vite.config.ts @@ -1,9 +1,10 @@ import { vitePlugin as remix } from '@remix-run/dev'; import { defineConfig } from 'vite'; +import { envOnlyMacros } from 'vite-env-only'; import tsconfigPaths from 'vite-tsconfig-paths'; export default defineConfig({ - plugins: [tsconfigPaths(), remix()], + plugins: [envOnlyMacros(), tsconfigPaths(), remix()], ssr: { resolve: { diff --git a/package.json b/package.json index 40aada9..58241be 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "typecheck": "nx run-many -t typecheck", "lint": "nx run-many --target lint --all", "release": "yarn build && changeset publish", - "clean": "rimraf node_modules packages/**/dist examples/**/dist packages/**/node_modules examples/**/node_modules", + "clean": "rimraf **/dist **/node_modules", "dep-graph": "nx graph" }, "resolutions": { diff --git a/packages/plugin-react-router/package.json b/packages/plugin-react-router/package.json index 14f6016..20e2b66 100644 --- a/packages/plugin-react-router/package.json +++ b/packages/plugin-react-router/package.json @@ -20,6 +20,7 @@ "exports": { ".": { "types": "./src/index.ts", + "development": "./src/index.ts", "bun": "./src/index.ts", "default": "./dist/index.js" } diff --git a/packages/plugin-solid-router/package.json b/packages/plugin-solid-router/package.json index 2d335ed..bf58cb6 100644 --- a/packages/plugin-solid-router/package.json +++ b/packages/plugin-solid-router/package.json @@ -17,6 +17,7 @@ "exports": { ".": { "types": "./src/index.ts", + "development": "./src/index.ts", "bun": "./src/index.ts", "default": "./dist/index.js" } diff --git a/packages/plugin-tanstack-query/package.json b/packages/plugin-tanstack-query/package.json index be163ae..7c867f8 100644 --- a/packages/plugin-tanstack-query/package.json +++ b/packages/plugin-tanstack-query/package.json @@ -17,6 +17,7 @@ "exports": { ".": { "types": "./src/index.ts", + "development": "./src/index.ts", "bun": "./src/index.ts", "default": "./dist/index.js" } diff --git a/packages/plugin-tanstack-router/package.json b/packages/plugin-tanstack-router/package.json index 0aee5f9..dd6b71e 100644 --- a/packages/plugin-tanstack-router/package.json +++ b/packages/plugin-tanstack-router/package.json @@ -17,11 +17,13 @@ "exports": { ".": { "types": "./src/index.ts", + "development": "./src/index.ts", "bun": "./src/index.ts", "import": "./dist/index.js" }, "./adapter": { "types": "./src/adapter.ts", + "development": "./src/adapter.ts", "bun": "./src/adapter.ts", "import": "./dist/adapter.js" } @@ -36,6 +38,9 @@ "peerDependencies": { "@tanstack/react-router": ">=1.57" }, + "dependencies": { + "@ssrx/renderer": "^0.4.0" + }, "devDependencies": { "@ssrx/vite": "^0.6.0", "@tanstack/react-router": "1.57.13", diff --git a/packages/plugin-trpc-react/package.json b/packages/plugin-trpc-react/package.json index 99c26df..6d82813 100644 --- a/packages/plugin-trpc-react/package.json +++ b/packages/plugin-trpc-react/package.json @@ -20,6 +20,7 @@ "exports": { ".": { "types": "./src/index.ts", + "development": "./src/index.ts", "bun": "./src/index.ts", "default": "./dist/index.js" } diff --git a/packages/plugin-unhead/package.json b/packages/plugin-unhead/package.json index d8c4c61..d3b3641 100644 --- a/packages/plugin-unhead/package.json +++ b/packages/plugin-unhead/package.json @@ -17,6 +17,7 @@ "exports": { ".": { "types": "./src/index.ts", + "development": "./src/index.ts", "bun": "./src/index.ts", "default": "./dist/index.js" } diff --git a/packages/react/package.json b/packages/react/package.json index 0c864c8..e7cb359 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -20,22 +20,14 @@ ], "exports": { ".": { - "types": "./src/server/index.ts", - "bun": "./src/server/index.ts", - "node": "./dist/server/index.js", - "worker": "./dist/server/index.js", - "workerd": "./dist/server/index.js", - "deno": "./dist/server/index.js", - "edge": "./dist/server/index.js", - "default": "./dist/client/index.js" - }, - "./client": { - "types": "./src/client/index.ts", - "bun": "./src/client/index.ts", - "default": "./dist/client/index.js" + "types": "./src/index.ts", + "development": "./src/index.ts", + "bun": "./src/index.ts", + "default": "./dist/index.js" }, "./server": { "types": "./src/server/index.ts", + "development": "./src/server/index.ts", "bun": "./src/server/index.ts", "default": "./dist/server/index.js" } @@ -59,7 +51,8 @@ "esbuild": "0.23.1", "react": "18.3.1", "react-dom": "18.3.1", - "rollup": "4.21.3", - "rollup-plugin-esbuild": "6.1.1" + "rollup": "4.22.5", + "rollup-plugin-esbuild": "6.1.1", + "vite-env-only": "3.0.3" } } diff --git a/packages/react/rollup.config.mjs b/packages/react/rollup.config.mjs index a48dd7a..7a700f4 100644 --- a/packages/react/rollup.config.mjs +++ b/packages/react/rollup.config.mjs @@ -2,7 +2,7 @@ import esbuild from 'rollup-plugin-esbuild'; const bundle = config => ({ ...config, - input: ['src/client/index.ts', 'src/server/index.ts'], + input: ['src/index.ts', 'src/server/index.ts'], external: id => !/^[./]/.test(id), }); diff --git a/packages/react/src/client/handler.ts b/packages/react/src/client/handler.ts deleted file mode 100644 index 1a5c86f..0000000 --- a/packages/react/src/client/handler.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { - type ClientHandlerOpts, - createApp as baseCreateApp, - type RenderPlugin, - type SetOptional, -} from '@ssrx/renderer/client'; - -import { RootLayout } from '../default-root.tsx'; - -export function createApp

[]>(opts: SetOptional, 'RootLayout'>) { - return baseCreateApp({ - RootLayout, - ...opts, - }); -} diff --git a/packages/react/src/client/index.ts b/packages/react/src/client/index.ts deleted file mode 100644 index 56af591..0000000 --- a/packages/react/src/client/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -import '../namespace.ts'; - -export { renderAssets } from '../assets.tsx'; -export { createApp } from './handler.ts'; diff --git a/packages/react/src/handler-client.ts b/packages/react/src/handler-client.ts new file mode 100644 index 0000000..5bf3fd7 --- /dev/null +++ b/packages/react/src/handler-client.ts @@ -0,0 +1,14 @@ +import { type ClientHandlerOpts, type RenderPlugin, type SetOptional } from '@ssrx/renderer'; +import { createApp as baseCreateApp } from '@ssrx/renderer/client'; +import { clientOnly$ } from 'vite-env-only/macros'; + +import { RootLayout } from './default-root.tsx'; + +export const createAppClient = clientOnly$(_createAppClient); + +function _createAppClient

[]>(opts: SetOptional, 'RootLayout'>) { + return baseCreateApp({ + RootLayout, + ...opts, + }); +} diff --git a/packages/react/src/handler-server.ts b/packages/react/src/handler-server.ts new file mode 100644 index 0000000..f2e0f9b --- /dev/null +++ b/packages/react/src/handler-server.ts @@ -0,0 +1,20 @@ +import { type RenderPlugin, type ServerHandlerOpts, type SetOptional } from '@ssrx/renderer'; +import { createApp as baseCreateApp } from '@ssrx/renderer/server'; +import { serverOnly$ } from 'vite-env-only/macros'; + +import { RootLayout } from './default-root.tsx'; +import { renderToStreamServer } from './server/stream.ts'; + +export const createAppServer = serverOnly$(_createAppServer); + +function _createAppServer

[]>( + opts: SetOptional, 'renderer' | 'RootLayout'>, +) { + return baseCreateApp({ + RootLayout, + renderer: { + renderToStream: renderToStreamServer!, + }, + ...opts, + }); +} diff --git a/packages/react/src/index.ts b/packages/react/src/index.ts new file mode 100644 index 0000000..36a9842 --- /dev/null +++ b/packages/react/src/index.ts @@ -0,0 +1,7 @@ +import './namespace.ts'; + +import { createAppClient } from './handler-client.ts'; +import { createAppServer } from './handler-server.ts'; +export { renderAssets } from './assets.tsx'; + +export const createApp = (import.meta.env.SSR ? createAppServer : createAppClient)!; diff --git a/packages/react/src/server/handler.ts b/packages/react/src/server/handler.ts deleted file mode 100644 index 0783f1f..0000000 --- a/packages/react/src/server/handler.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { - createApp as baseCreateApp, - type RenderPlugin, - type ServerHandlerOpts, - type SetOptional, -} from '@ssrx/renderer/server'; - -import { RootLayout } from '../default-root.tsx'; -import { renderToStream } from './stream.ts'; - -export function createApp

[]>( - opts: SetOptional, 'renderer' | 'RootLayout'>, -) { - return baseCreateApp({ - RootLayout, - renderer: { - renderToStream, - }, - ...opts, - }); -} diff --git a/packages/react/src/server/index.ts b/packages/react/src/server/index.ts index df5d81f..15e300a 100644 --- a/packages/react/src/server/index.ts +++ b/packages/react/src/server/index.ts @@ -1,5 +1 @@ -import '../namespace.ts'; - -export { renderAssets } from '../assets.tsx'; -export { createApp } from './handler.ts'; -export { renderToStream } from './stream.ts'; +export { renderToStreamServer as renderToStream } from './stream.ts'; diff --git a/packages/react/src/server/stream.ts b/packages/react/src/server/stream.ts index e1dd4ec..3629f81 100644 --- a/packages/react/src/server/stream.ts +++ b/packages/react/src/server/stream.ts @@ -1,4 +1,4 @@ -import type { RenderToStreamFn } from '@ssrx/renderer/server'; +import type { RenderToStreamFn } from '@ssrx/renderer'; import { injectIntoStream } from '@ssrx/streaming'; // @ts-expect-error no types import isbot from 'isbot-fast'; @@ -7,7 +7,7 @@ import rd from 'react-dom/server'; // @ts-expect-error ignore import { renderToReadableStream as fallbackRenderToReadableStream } from 'react-dom/server.browser'; -export const renderToStream: RenderToStreamFn = async ({ +export const renderToStreamServer: RenderToStreamFn = async ({ app, req, injectToStream, diff --git a/packages/remix/package.json b/packages/remix/package.json index d234427..1cbcd33 100644 --- a/packages/remix/package.json +++ b/packages/remix/package.json @@ -20,24 +20,10 @@ ], "exports": { ".": { - "types": "./src/server.tsx", - "bun": "./src/server.tsx", - "node": "./dist/server.js", - "worker": "./dist/server.js", - "workerd": "./dist/server.js", - "deno": "./dist/server.js", - "edge": "./dist/server.js", - "default": "./dist/client.js" - }, - "./client": { - "types": "./src/client.tsx", - "bun": "./src/client.tsx", - "default": "./dist/client.js" - }, - "./server": { - "types": "./src/server.tsx", - "bun": "./src/server.tsx", - "default": "./dist/server.js" + "types": "./src/index.ts", + "development": "./src/index.ts", + "bun": "./src/index.ts", + "default": "./dist/index.js" } }, "scripts": { @@ -55,7 +41,8 @@ "devDependencies": { "@remix-run/react": "2.12.0", "esbuild": "0.23.1", - "rollup": "4.21.3", - "rollup-plugin-esbuild": "6.1.1" + "rollup": "4.22.5", + "rollup-plugin-esbuild": "6.1.1", + "vite-env-only": "3.0.3" } } diff --git a/packages/remix/rollup.config.mjs b/packages/remix/rollup.config.mjs index 9f8d37c..df8dd35 100644 --- a/packages/remix/rollup.config.mjs +++ b/packages/remix/rollup.config.mjs @@ -2,7 +2,7 @@ import esbuild from 'rollup-plugin-esbuild'; const bundle = config => ({ ...config, - input: ['src/client.tsx', 'src/server.tsx'], + input: ['src/index.ts'], external: id => !/^[./]/.test(id), }); diff --git a/packages/remix/src/client.tsx b/packages/remix/src/client.tsx deleted file mode 100644 index bf0ae04..0000000 --- a/packages/remix/src/client.tsx +++ /dev/null @@ -1,17 +0,0 @@ -import type { RemixServerProps } from '@remix-run/react'; -import { RemixBrowser } from '@remix-run/react'; -import { - type ClientHandlerOpts, - createApp as baseCreateApp, - type RenderPlugin, - type SetOptional, -} from '@ssrx/renderer/client'; - -export function createApp

[]>( - opts: SetOptional, 'appRenderer'> & Pick, -) { - return baseCreateApp({ - appRenderer: () => () => , - ...opts, - }); -} diff --git a/packages/remix/src/handler-client.tsx b/packages/remix/src/handler-client.tsx new file mode 100644 index 0000000..6447f46 --- /dev/null +++ b/packages/remix/src/handler-client.tsx @@ -0,0 +1,16 @@ +import type { RemixServerProps } from '@remix-run/react'; +import { RemixBrowser } from '@remix-run/react'; +import { type ClientHandlerOpts, type RenderPlugin, type SetOptional } from '@ssrx/renderer'; +import { createApp as baseCreateApp } from '@ssrx/renderer/client'; +import { clientOnly$ } from 'vite-env-only/macros'; + +export const createAppClient = clientOnly$(_createAppClient); + +function _createAppClient

[]>( + opts: SetOptional, 'appRenderer'> & Pick, +) { + return baseCreateApp({ + appRenderer: () => () => , + ...opts, + }); +} diff --git a/packages/remix/src/server.tsx b/packages/remix/src/handler-server.tsx similarity index 72% rename from packages/remix/src/server.tsx rename to packages/remix/src/handler-server.tsx index 9825503..f51939c 100644 --- a/packages/remix/src/server.tsx +++ b/packages/remix/src/handler-server.tsx @@ -1,19 +1,13 @@ import type { RemixServerProps } from '@remix-run/react'; import { RemixServer } from '@remix-run/react'; -import type { EntryContext } from '@remix-run/react/dist/entry'; import { renderToStream } from '@ssrx/react/server'; -import type { RenderPlugin, ServerHandlerOpts, SetOptional } from '@ssrx/renderer/server'; +import type { RenderPlugin, ServerHandlerOpts, SetOptional } from '@ssrx/renderer'; import { createApp as baseCreateApp } from '@ssrx/renderer/server'; +import { serverOnly$ } from 'vite-env-only/macros'; -declare global { - namespace SSRx { - interface ReqMeta { - entryContext: EntryContext; - } - } -} +export const createAppServer = serverOnly$(_createAppServer); -export function createApp

[]>( +function _createAppServer

[]>( opts: SetOptional, 'appRenderer' | 'renderer'> & Pick = {}, ) { return baseCreateApp({ diff --git a/packages/remix/src/index.ts b/packages/remix/src/index.ts new file mode 100644 index 0000000..f9de9ee --- /dev/null +++ b/packages/remix/src/index.ts @@ -0,0 +1,6 @@ +import './namespace.ts'; + +import { createAppClient } from './handler-client.tsx'; +import { createAppServer } from './handler-server.tsx'; + +export const createApp = (import.meta.env.SSR ? createAppServer : createAppClient)!; diff --git a/packages/remix/src/namespace.ts b/packages/remix/src/namespace.ts new file mode 100644 index 0000000..6ba8490 --- /dev/null +++ b/packages/remix/src/namespace.ts @@ -0,0 +1,14 @@ +import type { EntryContext } from '@remix-run/react/dist/entry'; +import type { ReactNode } from 'react'; + +declare global { + namespace SSRx { + interface Config { + jsxElement: ReactNode; + } + + interface ReqMeta { + entryContext: EntryContext; + } + } +} diff --git a/packages/renderer/package.json b/packages/renderer/package.json index da79fcb..bb093e5 100644 --- a/packages/renderer/package.json +++ b/packages/renderer/package.json @@ -16,34 +16,28 @@ ], "exports": { ".": { - "types": "./src/server/index.ts", - "bun": "./src/server/index.ts", - "node": "./dist/server/index.js", - "worker": "./dist/server/index.js", - "workerd": "./dist/server/index.js", - "deno": "./dist/server/index.js", - "edge": "./dist/server/index.js", - "default": "./dist/client/index.js" + "types": "./src/index.ts", + "development": "./src/index.ts", + "bun": "./src/index.ts", + "default": "./dist/index.js" + }, + "./assets": { + "types": "./src/assets/index.ts", + "development": "./src/assets/index.ts", + "bun": "./src/assets/index.ts", + "default": "./dist/assets/index.js" }, "./client": { "types": "./src/client/index.ts", + "development": "./src/client/index.ts", "bun": "./src/client/index.ts", "default": "./dist/client/index.js" }, "./server": { "types": "./src/server/index.ts", + "development": "./src/server/index.ts", "bun": "./src/server/index.ts", "default": "./dist/server/index.js" - }, - "./assets": { - "types": "./src/server/assets.ts", - "bun": "./src/server/assets.ts", - "node": "./dist/server/assets.js", - "worker": "./dist/server/assets.js", - "workerd": "./dist/server/assets.js", - "deno": "./dist/server/assets.js", - "edge": "./dist/server/assets.js", - "default": "./dist/client/assets.js" } }, "scripts": { @@ -57,10 +51,11 @@ "deepmerge": "4.3.1" }, "devDependencies": { - "@types/node": "22.5.4", + "@types/node": "22.7.4", "esbuild": "0.23.1", - "rollup": "4.21.3", + "rollup": "4.22.5", "rollup-plugin-esbuild": "6.1.1", - "type-fest": "4.26.1" + "type-fest": "4.26.1", + "vite-env-only": "3.0.3" } } diff --git a/packages/renderer/rollup.config.mjs b/packages/renderer/rollup.config.mjs index 3a4100e..4be326e 100644 --- a/packages/renderer/rollup.config.mjs +++ b/packages/renderer/rollup.config.mjs @@ -2,7 +2,7 @@ import esbuild from 'rollup-plugin-esbuild'; const bundle = config => ({ ...config, - input: ['src/client/index.ts', 'src/server/index.ts', 'src/client/assets.ts', 'src/server/assets.ts'], + input: ['src/client/index.ts', 'src/server/index.ts', 'src/assets/index.ts', 'src/index.ts'], external: id => !/^[./]/.test(id), }); diff --git a/packages/renderer/src/assets/index.ts b/packages/renderer/src/assets/index.ts new file mode 100644 index 0000000..da27eab --- /dev/null +++ b/packages/renderer/src/assets/index.ts @@ -0,0 +1,4 @@ +import { assetsPluginClient } from './plugin-client.ts'; +import { assetsPluginServer } from './plugin-server.ts'; + +export const assetsPlugin = (import.meta.env.SSR ? assetsPluginServer : assetsPluginClient)!; diff --git a/packages/renderer/src/assets/plugin-client.ts b/packages/renderer/src/assets/plugin-client.ts new file mode 100644 index 0000000..d1fd77f --- /dev/null +++ b/packages/renderer/src/assets/plugin-client.ts @@ -0,0 +1,9 @@ +import { clientOnly$ } from 'vite-env-only/macros'; + +import { ASSETS_PLUGIN_ID, defineRenderPlugin } from '../common.ts'; + +export const assetsPluginClient = clientOnly$(() => { + return defineRenderPlugin({ + id: ASSETS_PLUGIN_ID, + }); +}); diff --git a/packages/renderer/src/server/assets.ts b/packages/renderer/src/assets/plugin-server.ts similarity index 52% rename from packages/renderer/src/server/assets.ts rename to packages/renderer/src/assets/plugin-server.ts index 05076b7..2895217 100644 --- a/packages/renderer/src/server/assets.ts +++ b/packages/renderer/src/assets/plugin-server.ts @@ -1,24 +1,19 @@ -import type { AssetHtmlTag } from '@ssrx/vite/runtime'; +import { assetsForRequest, renderAssetsToHtml } from '@ssrx/vite/runtime'; +import { serverOnly$ } from 'vite-env-only/macros'; import { ASSETS_PLUGIN_ID, defineRenderPlugin } from '../common.ts'; -export type AssetsPluginCtx = { - headAssets: AssetHtmlTag[]; - bodyAssets: AssetHtmlTag[]; -}; - -export const assetsPlugin = () => - defineRenderPlugin({ +export const assetsPluginServer = serverOnly$(() => { + return defineRenderPlugin({ id: ASSETS_PLUGIN_ID, hooksForReq: async ({ req }) => ({ server: await injectAssetsToStream({ req }), }), }); +}); -export const injectAssetsToStream = async ({ req }: { req: Request }) => { - const { assetsForRequest, renderAssetsToHtml } = await import('@ssrx/vite/runtime'); - +const injectAssetsToStream = async ({ req }: { req: Request }) => { const assets = await assetsForRequest(req.url); return { diff --git a/packages/renderer/src/client/assets.ts b/packages/renderer/src/client/assets.ts deleted file mode 100644 index 72e2aa3..0000000 --- a/packages/renderer/src/client/assets.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { ASSETS_PLUGIN_ID, defineRenderPlugin } from '../common.ts'; - -export const assetsPlugin = () => - defineRenderPlugin({ - id: ASSETS_PLUGIN_ID, - }); - -export const injectAssetsToStream = async () => { - return {}; -}; diff --git a/packages/renderer/src/client/ctx.ts b/packages/renderer/src/client/ctx.ts deleted file mode 100644 index 3a24b58..0000000 --- a/packages/renderer/src/client/ctx.ts +++ /dev/null @@ -1,4 +0,0 @@ -export const getPageCtx = () => { - // @ts-expect-error ignore - return window.__PAGE_CTX__ || {}; -}; diff --git a/packages/renderer/src/client/index.ts b/packages/renderer/src/client/index.ts index 3cbf6ef..0f3a11a 100644 --- a/packages/renderer/src/client/index.ts +++ b/packages/renderer/src/client/index.ts @@ -1,7 +1 @@ -import deepmerge from 'deepmerge'; - -export { defineRenderPlugin } from '../common.ts'; -export type { ClientHandlerOpts, Config, RenderPlugin, SetOptional } from '../types.ts'; -export { getPageCtx } from './ctx.ts'; export { createApp } from './handler.tsx'; -export { deepmerge }; diff --git a/packages/renderer/src/ctx-client.ts b/packages/renderer/src/ctx-client.ts new file mode 100644 index 0000000..3900700 --- /dev/null +++ b/packages/renderer/src/ctx-client.ts @@ -0,0 +1,6 @@ +import { clientOnly$ } from 'vite-env-only/macros'; + +export const getPageCtxClient = clientOnly$(() => { + // @ts-expect-error ignore + return window.__PAGE_CTX__ || {}; +}); diff --git a/packages/renderer/src/ctx-server.ts b/packages/renderer/src/ctx-server.ts new file mode 100644 index 0000000..79ab5c1 --- /dev/null +++ b/packages/renderer/src/ctx-server.ts @@ -0,0 +1,7 @@ +import { serverOnly$ } from 'vite-env-only/macros'; + +import { storage } from './server/handler.tsx'; + +export const getPageCtxServer = serverOnly$(() => { + return storage.getStore(); +}); diff --git a/packages/renderer/src/index.ts b/packages/renderer/src/index.ts new file mode 100644 index 0000000..0ae2602 --- /dev/null +++ b/packages/renderer/src/index.ts @@ -0,0 +1,20 @@ +import './namespace.ts'; + +import deepmerge from 'deepmerge'; + +import { getPageCtxClient } from './ctx-client.ts'; +import { getPageCtxServer } from './ctx-server.ts'; + +export const getPageCtx = (import.meta.env.SSR ? getPageCtxServer : getPageCtxClient)!; + +export { defineRenderPlugin } from './common.ts'; +export type { + ClientHandlerOpts, + Config, + RenderPlugin, + RenderToStreamFn, + ServerHandlerOpts, + SetOptional, +} from './types.ts'; + +export { deepmerge }; diff --git a/packages/renderer/src/server/ctx.ts b/packages/renderer/src/server/ctx.ts deleted file mode 100644 index 154f155..0000000 --- a/packages/renderer/src/server/ctx.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { AsyncLocalStorage } from 'node:async_hooks'; - -export const storage = new AsyncLocalStorage<{ appCtx: Record }>(); - -export const getPageCtx = () => { - return storage.getStore(); -}; diff --git a/packages/renderer/src/server/handler.tsx b/packages/renderer/src/server/handler.tsx index 93c6799..922d8c3 100644 --- a/packages/renderer/src/server/handler.tsx +++ b/packages/renderer/src/server/handler.tsx @@ -1,3 +1,5 @@ +import { AsyncLocalStorage } from 'node:async_hooks'; + import type { Simplify } from 'type-fest'; import type { @@ -9,7 +11,8 @@ import type { ServerHandlerOpts, ServerHooks, } from '../types.ts'; -import { storage } from './ctx.ts'; + +export const storage = new AsyncLocalStorage<{ appCtx: Record }>(); export function createApp

[]>({ RootLayout, diff --git a/packages/renderer/src/server/index.ts b/packages/renderer/src/server/index.ts index db76c27..0f3a11a 100644 --- a/packages/renderer/src/server/index.ts +++ b/packages/renderer/src/server/index.ts @@ -1,7 +1 @@ -import deepmerge from 'deepmerge'; - -export { defineRenderPlugin } from '../common.ts'; -export type { Config, RenderPlugin, RenderToStreamFn, ServerHandlerOpts, SetOptional } from '../types.ts'; -export { getPageCtx } from './ctx.ts'; export { createApp } from './handler.tsx'; -export { deepmerge }; diff --git a/packages/renderer/tsconfig.json b/packages/renderer/tsconfig.json index b2a5830..5169ad9 100644 --- a/packages/renderer/tsconfig.json +++ b/packages/renderer/tsconfig.json @@ -2,7 +2,6 @@ "extends": "../../tsconfig.base.json", "include": ["**/*.ts", "**/*.tsx"], "compilerOptions": { - "types": ["@types/node"], "paths": { "~/*": ["./src/*"] } diff --git a/packages/solid/package.json b/packages/solid/package.json index 9443f34..437733f 100644 --- a/packages/solid/package.json +++ b/packages/solid/package.json @@ -17,6 +17,7 @@ "exports": { ".": { "types": "./src/server/index.ts", + "development": "./src/server/index.ts", "bun": "./src/server/index.ts", "node": "./dist/server/index.jsx", "worker": "./dist/server/index.jsx", @@ -27,11 +28,13 @@ }, "./client": { "types": "./src/client/index.ts", + "development": "./src/client/index.ts", "bun": "./src/client/index.ts", "default": "./dist/client/index.jsx" }, "./server": { "types": "./src/server/index.ts", + "development": "./src/server/index.ts", "bun": "./src/server/index.ts", "default": "./dist/server/index.jsx" } diff --git a/packages/streaming/package.json b/packages/streaming/package.json index 8c13f34..18d08fa 100644 --- a/packages/streaming/package.json +++ b/packages/streaming/package.json @@ -17,6 +17,7 @@ "exports": { ".": { "types": "./src/index.ts", + "development": "./src/index.ts", "bun": "./src/index.ts", "default": "./dist/index.js" } diff --git a/packages/trpc-react-query/package.json b/packages/trpc-react-query/package.json index a28d86b..a2ba193 100644 --- a/packages/trpc-react-query/package.json +++ b/packages/trpc-react-query/package.json @@ -22,6 +22,7 @@ "exports": { ".": { "types": "./src/index.ts", + "development": "./src/index.ts", "bun": "./src/index.ts", "default": "./dist/index.js" } diff --git a/packages/vite/package.json b/packages/vite/package.json index baf1d24..1191ad0 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -49,8 +49,9 @@ "devDependencies": { "@total-typescript/ts-reset": "~0.6.1", "esbuild": "0.23.1", - "rollup": "4.21.3", + "rollup": "4.22.5", "rollup-plugin-esbuild": "6.1.1", - "vite": "5.4.5" + "vite": "5.4.5", + "vite-env-only": "3.0.3" } } diff --git a/packages/vite/src/plugin-entry.ts b/packages/vite/src/plugin-entry.ts index a657915..5a6b0ca 100644 --- a/packages/vite/src/plugin-entry.ts +++ b/packages/vite/src/plugin-entry.ts @@ -1,4 +1,5 @@ import type { PluginOption } from 'vite'; +import { envOnlyMacros } from 'vite-env-only'; import { Config } from './config.ts'; import { defaultRouterAdapter } from './default-router-adapter.ts'; @@ -54,6 +55,7 @@ export const ssrx = ({ globalThis.MANIFEST = manifest; return [ + envOnlyMacros(), configPlugin({ config, router, manifest }), emptyModulesPlugin(), virtualPlugin({ config, router, manifest }), diff --git a/yarn.lock b/yarn.lock index 30149fd..36881c6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3178,6 +3178,13 @@ __metadata: languageName: node linkType: hard +"@rollup/rollup-android-arm-eabi@npm:4.22.5": + version: 4.22.5 + resolution: "@rollup/rollup-android-arm-eabi@npm:4.22.5" + conditions: os=android & cpu=arm + languageName: node + linkType: hard + "@rollup/rollup-android-arm64@npm:4.21.3": version: 4.21.3 resolution: "@rollup/rollup-android-arm64@npm:4.21.3" @@ -3185,6 +3192,13 @@ __metadata: languageName: node linkType: hard +"@rollup/rollup-android-arm64@npm:4.22.5": + version: 4.22.5 + resolution: "@rollup/rollup-android-arm64@npm:4.22.5" + conditions: os=android & cpu=arm64 + languageName: node + linkType: hard + "@rollup/rollup-darwin-arm64@npm:4.21.3": version: 4.21.3 resolution: "@rollup/rollup-darwin-arm64@npm:4.21.3" @@ -3192,6 +3206,13 @@ __metadata: languageName: node linkType: hard +"@rollup/rollup-darwin-arm64@npm:4.22.5": + version: 4.22.5 + resolution: "@rollup/rollup-darwin-arm64@npm:4.22.5" + conditions: os=darwin & cpu=arm64 + languageName: node + linkType: hard + "@rollup/rollup-darwin-x64@npm:4.21.3": version: 4.21.3 resolution: "@rollup/rollup-darwin-x64@npm:4.21.3" @@ -3199,6 +3220,13 @@ __metadata: languageName: node linkType: hard +"@rollup/rollup-darwin-x64@npm:4.22.5": + version: 4.22.5 + resolution: "@rollup/rollup-darwin-x64@npm:4.22.5" + conditions: os=darwin & cpu=x64 + languageName: node + linkType: hard + "@rollup/rollup-linux-arm-gnueabihf@npm:4.21.3": version: 4.21.3 resolution: "@rollup/rollup-linux-arm-gnueabihf@npm:4.21.3" @@ -3206,6 +3234,13 @@ __metadata: languageName: node linkType: hard +"@rollup/rollup-linux-arm-gnueabihf@npm:4.22.5": + version: 4.22.5 + resolution: "@rollup/rollup-linux-arm-gnueabihf@npm:4.22.5" + conditions: os=linux & cpu=arm & libc=glibc + languageName: node + linkType: hard + "@rollup/rollup-linux-arm-musleabihf@npm:4.21.3": version: 4.21.3 resolution: "@rollup/rollup-linux-arm-musleabihf@npm:4.21.3" @@ -3213,6 +3248,13 @@ __metadata: languageName: node linkType: hard +"@rollup/rollup-linux-arm-musleabihf@npm:4.22.5": + version: 4.22.5 + resolution: "@rollup/rollup-linux-arm-musleabihf@npm:4.22.5" + conditions: os=linux & cpu=arm & libc=musl + languageName: node + linkType: hard + "@rollup/rollup-linux-arm64-gnu@npm:4.21.3": version: 4.21.3 resolution: "@rollup/rollup-linux-arm64-gnu@npm:4.21.3" @@ -3220,6 +3262,13 @@ __metadata: languageName: node linkType: hard +"@rollup/rollup-linux-arm64-gnu@npm:4.22.5": + version: 4.22.5 + resolution: "@rollup/rollup-linux-arm64-gnu@npm:4.22.5" + conditions: os=linux & cpu=arm64 & libc=glibc + languageName: node + linkType: hard + "@rollup/rollup-linux-arm64-musl@npm:4.21.3": version: 4.21.3 resolution: "@rollup/rollup-linux-arm64-musl@npm:4.21.3" @@ -3227,6 +3276,13 @@ __metadata: languageName: node linkType: hard +"@rollup/rollup-linux-arm64-musl@npm:4.22.5": + version: 4.22.5 + resolution: "@rollup/rollup-linux-arm64-musl@npm:4.22.5" + conditions: os=linux & cpu=arm64 & libc=musl + languageName: node + linkType: hard + "@rollup/rollup-linux-powerpc64le-gnu@npm:4.21.3": version: 4.21.3 resolution: "@rollup/rollup-linux-powerpc64le-gnu@npm:4.21.3" @@ -3234,6 +3290,13 @@ __metadata: languageName: node linkType: hard +"@rollup/rollup-linux-powerpc64le-gnu@npm:4.22.5": + version: 4.22.5 + resolution: "@rollup/rollup-linux-powerpc64le-gnu@npm:4.22.5" + conditions: os=linux & cpu=ppc64 & libc=glibc + languageName: node + linkType: hard + "@rollup/rollup-linux-riscv64-gnu@npm:4.21.3": version: 4.21.3 resolution: "@rollup/rollup-linux-riscv64-gnu@npm:4.21.3" @@ -3241,6 +3304,13 @@ __metadata: languageName: node linkType: hard +"@rollup/rollup-linux-riscv64-gnu@npm:4.22.5": + version: 4.22.5 + resolution: "@rollup/rollup-linux-riscv64-gnu@npm:4.22.5" + conditions: os=linux & cpu=riscv64 & libc=glibc + languageName: node + linkType: hard + "@rollup/rollup-linux-s390x-gnu@npm:4.21.3": version: 4.21.3 resolution: "@rollup/rollup-linux-s390x-gnu@npm:4.21.3" @@ -3248,6 +3318,13 @@ __metadata: languageName: node linkType: hard +"@rollup/rollup-linux-s390x-gnu@npm:4.22.5": + version: 4.22.5 + resolution: "@rollup/rollup-linux-s390x-gnu@npm:4.22.5" + conditions: os=linux & cpu=s390x & libc=glibc + languageName: node + linkType: hard + "@rollup/rollup-linux-x64-gnu@npm:4.21.3": version: 4.21.3 resolution: "@rollup/rollup-linux-x64-gnu@npm:4.21.3" @@ -3255,6 +3332,13 @@ __metadata: languageName: node linkType: hard +"@rollup/rollup-linux-x64-gnu@npm:4.22.5": + version: 4.22.5 + resolution: "@rollup/rollup-linux-x64-gnu@npm:4.22.5" + conditions: os=linux & cpu=x64 & libc=glibc + languageName: node + linkType: hard + "@rollup/rollup-linux-x64-musl@npm:4.21.3": version: 4.21.3 resolution: "@rollup/rollup-linux-x64-musl@npm:4.21.3" @@ -3262,6 +3346,13 @@ __metadata: languageName: node linkType: hard +"@rollup/rollup-linux-x64-musl@npm:4.22.5": + version: 4.22.5 + resolution: "@rollup/rollup-linux-x64-musl@npm:4.22.5" + conditions: os=linux & cpu=x64 & libc=musl + languageName: node + linkType: hard + "@rollup/rollup-win32-arm64-msvc@npm:4.21.3": version: 4.21.3 resolution: "@rollup/rollup-win32-arm64-msvc@npm:4.21.3" @@ -3269,6 +3360,13 @@ __metadata: languageName: node linkType: hard +"@rollup/rollup-win32-arm64-msvc@npm:4.22.5": + version: 4.22.5 + resolution: "@rollup/rollup-win32-arm64-msvc@npm:4.22.5" + conditions: os=win32 & cpu=arm64 + languageName: node + linkType: hard + "@rollup/rollup-win32-ia32-msvc@npm:4.21.3": version: 4.21.3 resolution: "@rollup/rollup-win32-ia32-msvc@npm:4.21.3" @@ -3276,6 +3374,13 @@ __metadata: languageName: node linkType: hard +"@rollup/rollup-win32-ia32-msvc@npm:4.22.5": + version: 4.22.5 + resolution: "@rollup/rollup-win32-ia32-msvc@npm:4.22.5" + conditions: os=win32 & cpu=ia32 + languageName: node + linkType: hard + "@rollup/rollup-win32-x64-msvc@npm:4.21.3": version: 4.21.3 resolution: "@rollup/rollup-win32-x64-msvc@npm:4.21.3" @@ -3283,6 +3388,13 @@ __metadata: languageName: node linkType: hard +"@rollup/rollup-win32-x64-msvc@npm:4.22.5": + version: 4.22.5 + resolution: "@rollup/rollup-win32-x64-msvc@npm:4.22.5" + conditions: os=win32 & cpu=x64 + languageName: node + linkType: hard + "@rtsao/scc@npm:^1.1.0": version: 1.1.0 resolution: "@rtsao/scc@npm:1.1.0" @@ -3368,6 +3480,7 @@ __metadata: version: 0.0.0-use.local resolution: "@ssrx/plugin-tanstack-router@workspace:packages/plugin-tanstack-router" dependencies: + "@ssrx/renderer": "npm:^0.4.0" "@ssrx/vite": "npm:^0.6.0" "@tanstack/react-router": "npm:1.57.13" react: "npm:18.3.1" @@ -3417,8 +3530,9 @@ __metadata: isbot-fast: "npm:1.2.0" react: "npm:18.3.1" react-dom: "npm:18.3.1" - rollup: "npm:4.21.3" + rollup: "npm:4.22.5" rollup-plugin-esbuild: "npm:6.1.1" + vite-env-only: "npm:3.0.3" peerDependencies: react: ">=18" react-dom: ">=18" @@ -3433,8 +3547,9 @@ __metadata: "@ssrx/react": "npm:^0.3.0" "@ssrx/renderer": "npm:^0.4.0" esbuild: "npm:0.23.1" - rollup: "npm:4.21.3" + rollup: "npm:4.22.5" rollup-plugin-esbuild: "npm:6.1.1" + vite-env-only: "npm:3.0.3" peerDependencies: "@remix-run/react": ">=2.2.0" languageName: unknown @@ -3446,12 +3561,13 @@ __metadata: dependencies: "@ssrx/streaming": "npm:^0.2.0" "@ssrx/vite": "npm:^0.6.0" - "@types/node": "npm:22.5.4" + "@types/node": "npm:22.7.4" deepmerge: "npm:4.3.1" esbuild: "npm:0.23.1" - rollup: "npm:4.21.3" + rollup: "npm:4.22.5" rollup-plugin-esbuild: "npm:6.1.1" type-fest: "npm:4.26.1" + vite-env-only: "npm:3.0.3" languageName: unknown linkType: soft @@ -3522,9 +3638,10 @@ __metadata: "@total-typescript/ts-reset": "npm:~0.6.1" esbuild: "npm:0.23.1" radix3: "npm:^1.1.2" - rollup: "npm:4.21.3" + rollup: "npm:4.22.5" rollup-plugin-esbuild: "npm:6.1.1" vite: "npm:5.4.5" + vite-env-only: "npm:3.0.3" peerDependencies: vite: ">=4" languageName: unknown @@ -3871,6 +3988,13 @@ __metadata: languageName: node linkType: hard +"@types/estree@npm:1.0.6": + version: 1.0.6 + resolution: "@types/estree@npm:1.0.6" + checksum: 10c0/cdfd751f6f9065442cd40957c07fd80361c962869aa853c1c2fd03e101af8b9389d8ff4955a43a6fcfa223dd387a089937f95be0f3eec21ca527039fd2d9859a + languageName: node + linkType: hard + "@types/hast@npm:^2.0.0": version: 2.3.10 resolution: "@types/hast@npm:2.3.10" @@ -3949,7 +4073,7 @@ __metadata: languageName: node linkType: hard -"@types/node@npm:*, @types/node@npm:22.5.4": +"@types/node@npm:*": version: 22.5.4 resolution: "@types/node@npm:22.5.4" dependencies: @@ -3958,6 +4082,15 @@ __metadata: languageName: node linkType: hard +"@types/node@npm:22.7.4": + version: 22.7.4 + resolution: "@types/node@npm:22.7.4" + dependencies: + undici-types: "npm:~6.19.2" + checksum: 10c0/c22bf54515c78ff3170142c1e718b90e2a0003419dc2d55f79c9c9362edd590a6ab1450deb09ff6e1b32d1b4698da407930b16285e8be3a009ea6cd2695cac01 + languageName: node + linkType: hard + "@types/node@npm:^12.7.1": version: 12.20.55 resolution: "@types/node@npm:12.20.55" @@ -5360,7 +5493,7 @@ __metadata: "@types/react": "npm:18.3.5" "@types/react-dom": "npm:18.3.0" "@vitejs/plugin-react": "npm:4.3.1" - elysia: "npm:1.1.12" + elysia: "npm:1.1.16" react: "npm:18.3.1" react-dom: "npm:18.3.1" react-router-dom: "npm:6.26.2" @@ -6695,9 +6828,9 @@ __metadata: languageName: node linkType: hard -"elysia@npm:1.1.12": - version: 1.1.12 - resolution: "elysia@npm:1.1.12" +"elysia@npm:1.1.16": + version: 1.1.16 + resolution: "elysia@npm:1.1.16" dependencies: "@sinclair/typebox": "npm:0.32.34" cookie: "npm:^0.6.0" @@ -6712,7 +6845,7 @@ __metadata: optional: true typescript: optional: true - checksum: 10c0/022e25567f96ff4e25cff17fbda41483a13e81386c7106bdf713644d867825bc6bfffcd95a02a88084fc7965878df96b547699b26215bf5a908ed8e0c491e796 + checksum: 10c0/c74a45a6e914636313d0d7ca509742e9e618bb7389a6407f314546f534ac4ed38908cacdcf5114e8cca8f76fb9bbd0267f83a38fe8fccaf981884e35ccfccc72 languageName: node linkType: hard @@ -13443,6 +13576,7 @@ __metadata: typescript-remix-routes-plugin: "npm:1.0.1" valibot: "npm:0.41.0" vite: "npm:5.4.5" + vite-env-only: "npm:3.0.3" vite-tsconfig-paths: "npm:5.0.1" languageName: unknown linkType: soft @@ -13643,7 +13777,70 @@ __metadata: languageName: node linkType: hard -"rollup@npm:4.21.3, rollup@npm:^4.18.0, rollup@npm:^4.19.0, rollup@npm:^4.20.0": +"rollup@npm:4.22.5": + version: 4.22.5 + resolution: "rollup@npm:4.22.5" + dependencies: + "@rollup/rollup-android-arm-eabi": "npm:4.22.5" + "@rollup/rollup-android-arm64": "npm:4.22.5" + "@rollup/rollup-darwin-arm64": "npm:4.22.5" + "@rollup/rollup-darwin-x64": "npm:4.22.5" + "@rollup/rollup-linux-arm-gnueabihf": "npm:4.22.5" + "@rollup/rollup-linux-arm-musleabihf": "npm:4.22.5" + "@rollup/rollup-linux-arm64-gnu": "npm:4.22.5" + "@rollup/rollup-linux-arm64-musl": "npm:4.22.5" + "@rollup/rollup-linux-powerpc64le-gnu": "npm:4.22.5" + "@rollup/rollup-linux-riscv64-gnu": "npm:4.22.5" + "@rollup/rollup-linux-s390x-gnu": "npm:4.22.5" + "@rollup/rollup-linux-x64-gnu": "npm:4.22.5" + "@rollup/rollup-linux-x64-musl": "npm:4.22.5" + "@rollup/rollup-win32-arm64-msvc": "npm:4.22.5" + "@rollup/rollup-win32-ia32-msvc": "npm:4.22.5" + "@rollup/rollup-win32-x64-msvc": "npm:4.22.5" + "@types/estree": "npm:1.0.6" + fsevents: "npm:~2.3.2" + dependenciesMeta: + "@rollup/rollup-android-arm-eabi": + optional: true + "@rollup/rollup-android-arm64": + optional: true + "@rollup/rollup-darwin-arm64": + optional: true + "@rollup/rollup-darwin-x64": + optional: true + "@rollup/rollup-linux-arm-gnueabihf": + optional: true + "@rollup/rollup-linux-arm-musleabihf": + optional: true + "@rollup/rollup-linux-arm64-gnu": + optional: true + "@rollup/rollup-linux-arm64-musl": + optional: true + "@rollup/rollup-linux-powerpc64le-gnu": + optional: true + "@rollup/rollup-linux-riscv64-gnu": + optional: true + "@rollup/rollup-linux-s390x-gnu": + optional: true + "@rollup/rollup-linux-x64-gnu": + optional: true + "@rollup/rollup-linux-x64-musl": + optional: true + "@rollup/rollup-win32-arm64-msvc": + optional: true + "@rollup/rollup-win32-ia32-msvc": + optional: true + "@rollup/rollup-win32-x64-msvc": + optional: true + fsevents: + optional: true + bin: + rollup: dist/bin/rollup + checksum: 10c0/9b9432206ecc2f68edca965f8cf119eccd5346c86c392f733a8062b7c6a309b70c35e8448024146bd0e3444d8b3797758c8e29248b273d1433de94a4ea265246 + languageName: node + linkType: hard + +"rollup@npm:^4.18.0, rollup@npm:^4.19.0, rollup@npm:^4.20.0": version: 4.21.3 resolution: "rollup@npm:4.21.3" dependencies: @@ -15823,6 +16020,23 @@ __metadata: languageName: node linkType: hard +"vite-env-only@npm:3.0.3": + version: 3.0.3 + resolution: "vite-env-only@npm:3.0.3" + dependencies: + "@babel/core": "npm:^7.23.7" + "@babel/generator": "npm:^7.23.6" + "@babel/parser": "npm:^7.23.6" + "@babel/traverse": "npm:^7.23.7" + "@babel/types": "npm:^7.23.6" + babel-dead-code-elimination: "npm:^1.0.6" + micromatch: "npm:^4.0.5" + peerDependencies: + vite: "*" + checksum: 10c0/7ee80d71c74bc179fe03093e9ba80070c3ff015cd2059be1972579d604ab4eab79844fc4eedb0c2c10a28c973ee24c40b2d16095ea3e0e01e2156ed11e6bc07b + languageName: node + linkType: hard + "vite-node@npm:1.6.0, vite-node@npm:^1.2.0": version: 1.6.0 resolution: "vite-node@npm:1.6.0"