Skip to content

Commit

Permalink
refactor from exports strategy to vite-env-only
Browse files Browse the repository at this point in the history
Signed-off-by: Marc MacLeod <[email protected]>
  • Loading branch information
marbemac committed Sep 27, 2024
1 parent c90f00c commit df9add7
Show file tree
Hide file tree
Showing 50 changed files with 429 additions and 199 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
"typescript.tsdk": "node_modules/typescript/lib",
"vitest.disableWorkspaceWarning": true
}
2 changes: 1 addition & 1 deletion examples/bun-react-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions examples/remix-vite/app/routes/wait.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 () => {
Expand Down
1 change: 1 addition & 0 deletions examples/remix-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
3 changes: 2 additions & 1 deletion examples/remix-vite/vite.config.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-react-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"exports": {
".": {
"types": "./src/index.ts",
"development": "./src/index.ts",
"bun": "./src/index.ts",
"default": "./dist/index.js"
}
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-solid-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"exports": {
".": {
"types": "./src/index.ts",
"development": "./src/index.ts",
"bun": "./src/index.ts",
"default": "./dist/index.js"
}
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-tanstack-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"exports": {
".": {
"types": "./src/index.ts",
"development": "./src/index.ts",
"bun": "./src/index.ts",
"default": "./dist/index.js"
}
Expand Down
5 changes: 5 additions & 0 deletions packages/plugin-tanstack-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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",
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-trpc-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"exports": {
".": {
"types": "./src/index.ts",
"development": "./src/index.ts",
"bun": "./src/index.ts",
"default": "./dist/index.js"
}
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-unhead/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"exports": {
".": {
"types": "./src/index.ts",
"development": "./src/index.ts",
"bun": "./src/index.ts",
"default": "./dist/index.js"
}
Expand Down
23 changes: 8 additions & 15 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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"
}
}
2 changes: 1 addition & 1 deletion packages/react/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});

Expand Down
15 changes: 0 additions & 15 deletions packages/react/src/client/handler.ts

This file was deleted.

4 changes: 0 additions & 4 deletions packages/react/src/client/index.ts

This file was deleted.

14 changes: 14 additions & 0 deletions packages/react/src/handler-client.ts
Original file line number Diff line number Diff line change
@@ -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<P extends RenderPlugin<any>[]>(opts: SetOptional<ClientHandlerOpts<P>, 'RootLayout'>) {
return baseCreateApp({
RootLayout,
...opts,
});
}
20 changes: 20 additions & 0 deletions packages/react/src/handler-server.ts
Original file line number Diff line number Diff line change
@@ -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<P extends RenderPlugin<any>[]>(
opts: SetOptional<ServerHandlerOpts<P>, 'renderer' | 'RootLayout'>,
) {
return baseCreateApp({
RootLayout,
renderer: {
renderToStream: renderToStreamServer!,
},
...opts,
});
}
7 changes: 7 additions & 0 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
@@ -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)!;
21 changes: 0 additions & 21 deletions packages/react/src/server/handler.ts

This file was deleted.

6 changes: 1 addition & 5 deletions packages/react/src/server/index.ts
Original file line number Diff line number Diff line change
@@ -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';
4 changes: 2 additions & 2 deletions packages/react/src/server/stream.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<RenderToReadableStreamOptions> = async ({
export const renderToStreamServer: RenderToStreamFn<RenderToReadableStreamOptions> = async ({
app,
req,
injectToStream,
Expand Down
27 changes: 7 additions & 20 deletions packages/remix/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"
}
}
2 changes: 1 addition & 1 deletion packages/remix/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});

Expand Down
17 changes: 0 additions & 17 deletions packages/remix/src/client.tsx

This file was deleted.

16 changes: 16 additions & 0 deletions packages/remix/src/handler-client.tsx
Original file line number Diff line number Diff line change
@@ -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<P extends RenderPlugin<any>[]>(
opts: SetOptional<ClientHandlerOpts<P>, 'appRenderer'> & Pick<RemixServerProps, 'abortDelay'>,
) {
return baseCreateApp({
appRenderer: () => () => <RemixBrowser />,
...opts,
});
}
Original file line number Diff line number Diff line change
@@ -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<P extends RenderPlugin<any>[]>(
function _createAppServer<P extends RenderPlugin<any>[]>(
opts: SetOptional<ServerHandlerOpts<P>, 'appRenderer' | 'renderer'> & Pick<RemixServerProps, 'abortDelay'> = {},
) {
return baseCreateApp({
Expand Down
6 changes: 6 additions & 0 deletions packages/remix/src/index.ts
Original file line number Diff line number Diff line change
@@ -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)!;
Loading

0 comments on commit df9add7

Please sign in to comment.