Skip to content

Commit

Permalink
fix various issues with an example
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Dec 29, 2023
1 parent d0843c4 commit dbc3d29
Show file tree
Hide file tree
Showing 23 changed files with 88 additions and 69 deletions.
2 changes: 1 addition & 1 deletion e2e/fixtures/ssr-basic/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const rootElement = (
</StrictMode>
);

if (import.meta.env.WAKU_SSR_ENABLED) {
if (import.meta.env.WAKU_HYDRATE) {
hydrateRoot(document.body, rootElement);
} else {
createRoot(document.body).render(rootElement);
Expand Down
2 changes: 1 addition & 1 deletion examples/01_counter/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const rootElement = (
</StrictMode>
);

if (import.meta.env.WAKU_SSR_ENABLED) {
if (import.meta.env.WAKU_HYDRATE) {
hydrateRoot(document.body, rootElement);
} else {
createRoot(document.body).render(rootElement);
Expand Down
2 changes: 1 addition & 1 deletion examples/02_async/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const rootElement = (
</StrictMode>
);

if (import.meta.env.WAKU_SSR_ENABLED) {
if (import.meta.env.WAKU_HYDRATE) {
hydrateRoot(document.body, rootElement);
} else {
createRoot(document.body).render(rootElement);
Expand Down
2 changes: 1 addition & 1 deletion examples/03_promise/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const rootElement = (
</StrictMode>
);

if (import.meta.env.WAKU_SSR_ENABLED) {
if (import.meta.env.WAKU_HYDRATE) {
hydrateRoot(document.body, rootElement);
} else {
createRoot(document.body).render(rootElement);
Expand Down
2 changes: 1 addition & 1 deletion examples/04_callserver/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const rootElement = (
</StrictMode>
);

if (import.meta.env.WAKU_SSR_ENABLED) {
if (import.meta.env.WAKU_HYDRATE) {
hydrateRoot(document.body, rootElement);
} else {
createRoot(document.body).render(rootElement);
Expand Down
2 changes: 1 addition & 1 deletion examples/05_mutation/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const rootElement = (
</StrictMode>
);

if (import.meta.env.WAKU_SSR_ENABLED) {
if (import.meta.env.WAKU_HYDRATE) {
hydrateRoot(document.body, rootElement);
} else {
createRoot(document.body).render(rootElement);
Expand Down
2 changes: 1 addition & 1 deletion examples/06_nesting/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const rootElement = (
</StrictMode>
);

if (import.meta.env.WAKU_SSR_ENABLED) {
if (import.meta.env.WAKU_HYDRATE) {
hydrateRoot(document.body, rootElement);
} else {
createRoot(document.body).render(rootElement);
Expand Down
2 changes: 1 addition & 1 deletion examples/07_router/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const rootElement = (
</StrictMode>
);

if (import.meta.env.WAKU_SSR_ENABLED) {
if (import.meta.env.WAKU_HYDRATE) {
hydrateRoot(document.body, rootElement);
} else {
createRoot(document.body).render(rootElement);
Expand Down
2 changes: 1 addition & 1 deletion examples/08_cookies/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const rootElement = (
</StrictMode>
);

if (import.meta.env.WAKU_SSR_ENABLED) {
if (import.meta.env.WAKU_HYDRATE) {
hydrateRoot(document.body, rootElement);
} else {
createRoot(document.body).render(rootElement);
Expand Down
1 change: 1 addition & 0 deletions examples/09_cssmodules/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WAKU_PUBLIC_HELLO="Hello from env file"
1 change: 1 addition & 0 deletions examples/09_cssmodules/src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const App = ({ name }: { name: string }) => {
<h1 className={styles.title}>Hello {name}!!</h1>
<h3>This is a server component.</h3>
<Counter />
Env: {import.meta.env.WAKU_PUBLIC_HELLO}
</div>
);
};
Expand Down
1 change: 1 addition & 0 deletions examples/09_cssmodules/src/components/Counter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const Counter = () => {
<p>Count: {count}</p>
<button onClick={() => setCount((c) => c + 1)}>Increment</button>
<h3>This is a client component.</h3>
Env: {import.meta.env.WAKU_PUBLIC_HELLO}
</div>
);
};
2 changes: 1 addition & 1 deletion examples/09_cssmodules/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const rootElement = (
</StrictMode>
);

if (import.meta.env.WAKU_SSR_ENABLED) {
if (import.meta.env.WAKU_HYDRATE) {
hydrateRoot(document.body, rootElement);
} else {
createRoot(document.body).render(rootElement);
Expand Down
2 changes: 1 addition & 1 deletion examples/10_dynamicroute/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const rootElement = (
</StrictMode>
);

if (import.meta.env.WAKU_SSR_ENABLED) {
if (import.meta.env.WAKU_HYDRATE) {
hydrateRoot(document.body, rootElement);
} else {
createRoot(document.body).render(rootElement);
Expand Down
2 changes: 1 addition & 1 deletion examples/11_form/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const rootElement = (
</StrictMode>
);

if (import.meta.env.WAKU_SSR_ENABLED) {
if (import.meta.env.WAKU_HYDRATE) {
hydrateRoot(document.body, rootElement);
} else {
createRoot(document.body).render(rootElement);
Expand Down
2 changes: 1 addition & 1 deletion examples/12_css/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const rootElement = (
</StrictMode>
);

if (import.meta.env.WAKU_SSR_ENABLED) {
if (import.meta.env.WAKU_HYDRATE) {
hydrateRoot(document.body, rootElement);
} else {
createRoot(document.body).render(rootElement);
Expand Down
3 changes: 2 additions & 1 deletion packages/waku/src/lib/builder/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ const buildServerBundle = async (
},
serverEntryFiles,
}),
rscEnvPlugin({ config }),
],
ssr: {
resolve: {
Expand Down Expand Up @@ -260,7 +261,7 @@ const buildClientBundle = async (
plugins: [
patchReactRefresh(viteReact()),
rscIndexPlugin({ ...config, cssAssets }),
rscEnvPlugin({ config, ssr }),
rscEnvPlugin({ config, hydrate: ssr }),
],
build: {
outDir: joinPath(rootDir, config.distDir, config.publicDir),
Expand Down
1 change: 1 addition & 0 deletions packages/waku/src/lib/handlers/dev-worker-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type RenderRequest = {
context: unknown;
stream?: ReadableStream;
moduleIdCallback?: (id: string) => void;
env: Record<string, string>;
};

export type BuildOutput = {
Expand Down
85 changes: 46 additions & 39 deletions packages/waku/src/lib/handlers/dev-worker-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { pathToFileURL } from 'node:url';
import { parentPort } from 'node:worker_threads';
import { Server } from 'node:http';
import { createServer as createViteServer } from 'vite';
import type { ViteDevServer } from 'vite';

import type { EntriesDev } from '../../server.js';
import type { ResolvedConfig } from '../config.js';
Expand All @@ -17,6 +18,7 @@ import type {
import { renderRsc } from '../renderers/rsc-renderer.js';
import { nonjsResolvePlugin } from '../plugins/vite-plugin-nonjs-resolve.js';
import { rscTransformPlugin } from '../plugins/vite-plugin-rsc-transform.js';
import { rscEnvPlugin } from '../plugins/vite-plugin-rsc-env.js';
import { rscReloadPlugin } from '../plugins/vite-plugin-rsc-reload.js';
import { rscDelegatePlugin } from '../plugins/vite-plugin-rsc-delegate.js';
import { mergeUserViteConfig } from '../utils/merge-vite-config.js';
Expand All @@ -31,6 +33,7 @@ const controllerMap = new Map<number, ReadableStreamDefaultController>();
const handleRender = async (mesg: MessageReq & { type: 'render' }) => {
const { id, type: _removed, hasModuleIdCallback, ...rest } = mesg;
const rr: RenderRequest = rest;
(globalThis as any).__WAKU_PRIVATE_ENV__ = rr.env || {};
try {
const stream = new ReadableStream({
start(controller) {
Expand Down Expand Up @@ -89,52 +92,56 @@ const handleRender = async (mesg: MessageReq & { type: 'render' }) => {
}
};

const dummyServer = new Server(); // FIXME we hope to avoid this hack

const moduleImports: Set<string> = new Set();

const mergedViteConfig = await mergeUserViteConfig({
plugins: [
nonjsResolvePlugin(),
rscTransformPlugin({ isBuild: false }),
rscReloadPlugin(moduleImports, (type) => {
const mesg: MessageRes = { type };
parentPort!.postMessage(mesg);
}),
rscDelegatePlugin(moduleImports, (resultOrSource) => {
const mesg: MessageRes =
typeof resultOrSource === 'object'
? { type: 'module-import', result: resultOrSource }
: { type: 'hot-import', source: resultOrSource };
parentPort!.postMessage(mesg);
}),
],
// HACK to suppress 'Skipping dependency pre-bundling' warning
optimizeDeps: { include: [] },
ssr: {
resolve: {
conditions: ['react-server', 'workerd'],
externalConditions: ['react-server', 'workerd'],
let lastViteServer: ViteDevServer | undefined;
const getViteServer = async () => {
if (lastViteServer) {
return lastViteServer;
}
const dummyServer = new Server(); // FIXME we hope to avoid this hack
const moduleImports: Set<string> = new Set();
const mergedViteConfig = await mergeUserViteConfig({
plugins: [
nonjsResolvePlugin(),
rscTransformPlugin({ isBuild: false }),
rscEnvPlugin({}),
rscReloadPlugin(moduleImports, (type) => {
const mesg: MessageRes = { type };
parentPort!.postMessage(mesg);
}),
rscDelegatePlugin(moduleImports, (resultOrSource) => {
const mesg: MessageRes =
typeof resultOrSource === 'object'
? { type: 'module-import', result: resultOrSource }
: { type: 'hot-import', source: resultOrSource };
parentPort!.postMessage(mesg);
}),
],
// HACK to suppress 'Skipping dependency pre-bundling' warning
optimizeDeps: { include: [] },
ssr: {
resolve: {
conditions: ['react-server', 'workerd'],
externalConditions: ['react-server', 'workerd'],
},
external: ['react', 'react-server-dom-webpack'],
noExternal: /^(?!node:)/,
},
external: ['react', 'react-server-dom-webpack'],
noExternal: /^(?!node:)/,
},
appType: 'custom',
server: { middlewareMode: true, hmr: { server: dummyServer } },
});

const vitePromise = createViteServer(mergedViteConfig).then(async (vite) => {
await vite.ws.close();
return vite;
});
appType: 'custom',
server: { middlewareMode: true, hmr: { server: dummyServer } },
});
const viteServer = await createViteServer(mergedViteConfig);
await viteServer.ws.close();
lastViteServer = viteServer;
return viteServer;
};

const loadServerFile = async (fileURL: string) => {
const vite = await vitePromise;
const vite = await getViteServer();
return vite.ssrLoadModule(fileURLToFilePath(fileURL));
};

const loadEntries = async (config: ResolvedConfig) => {
const vite = await vitePromise;
const vite = await getViteServer();
const filePath = joinPath(vite.config.root, config.srcDir, config.entriesJs);
return vite.ssrLoadModule(filePath) as Promise<EntriesDev>;
};
Expand Down
4 changes: 3 additions & 1 deletion packages/waku/src/lib/handlers/handler-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function createHandler<
patchReactRefresh(viteReact()),
rscIndexPlugin(config),
rscHmrPlugin(),
rscEnvPlugin({ config, ssr }),
rscEnvPlugin({ config, hydrate: ssr }),
],
ssr: {
external: ['waku'],
Expand Down Expand Up @@ -149,6 +149,7 @@ export function createHandler<
contentType: undefined,
config,
context,
env: (globalThis as any).__WAKU_PRIVATE_ENV__,
});
context = nextCtx as Context;
return readable;
Expand Down Expand Up @@ -184,6 +185,7 @@ export function createHandler<
config,
context,
stream: req.stream,
env: (globalThis as any).__WAKU_PRIVATE_ENV__,
});
unstable_posthook?.(req, res, nextCtx as Context);
readable.pipeTo(res.stream);
Expand Down
30 changes: 17 additions & 13 deletions packages/waku/src/lib/plugins/vite-plugin-rsc-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import type { Plugin } from 'vite';

export function rscEnvPlugin({
config,
ssr,
hydrate,
}: {
config: {
config?: {
basePath: string;
rscPath: string;
};
ssr: boolean | undefined;
hydrate?: boolean | undefined;
}): Plugin {
return {
name: 'rsc-env-plugin',
Expand All @@ -22,16 +22,20 @@ export function rscEnvPlugin({
? [[`import.meta.env.${k}`, JSON.stringify(v)]]
: [],
),
[
'import.meta.env.WAKU_CONFIG_BASE_PATH',
JSON.stringify(config.basePath),
],
[
'import.meta.env.WAKU_CONFIG_RSC_PATH',
JSON.stringify(config.rscPath),
],
...(ssr
? [['import.meta.env.WAKU_SSR_ENABLED', JSON.stringify('true')]]
...(config
? [
[
'import.meta.env.WAKU_CONFIG_BASE_PATH',
JSON.stringify(config.basePath),
],
[
'import.meta.env.WAKU_CONFIG_RSC_PATH',
JSON.stringify(config.rscPath),
],
]
: []),
...(hydrate
? [['import.meta.env.WAKU_HYDRATE', JSON.stringify('true')]]
: []),
]),
};
Expand Down
3 changes: 2 additions & 1 deletion packages/waku/src/lib/renderers/html-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ const getViteServer = async () => {
const { nonjsResolvePlugin } = await import(
'../plugins/vite-plugin-nonjs-resolve.js'
);
const { rscEnvPlugin } = await import('../plugins/vite-plugin-rsc-env.js');
const viteServer = await createViteServer({
plugins: [nonjsResolvePlugin()],
plugins: [nonjsResolvePlugin(), rscEnvPlugin({})],
// HACK to suppress 'Skipping dependency pre-bundling' warning
optimizeDeps: { include: [] },
ssr: {
Expand Down
2 changes: 1 addition & 1 deletion packages/website/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const rootElement = (
</StrictMode>
);

if (import.meta.env.WAKU_SSR_ENABLED) {
if (import.meta.env.WAKU_HYDRATE) {
hydrateRoot(document.body, rootElement);
} else {
createRoot(document.body).render(rootElement);
Expand Down

0 comments on commit dbc3d29

Please sign in to comment.