Skip to content

Commit

Permalink
fix: waku build will bundle browser files in server side
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 committed Sep 30, 2024
1 parent 811f5ff commit 3c2e0fa
Show file tree
Hide file tree
Showing 15 changed files with 191 additions and 0 deletions.
1 change: 1 addition & 0 deletions e2e/fixtures/exports-condition/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Exports Condition
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log('browser runtime called')
export const runtime = 'browser'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const runtime: string;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log('server runtime called')
export const runtime = 'server'
12 changes: 12 additions & 0 deletions e2e/fixtures/exports-condition/modules/my-module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "my-module",
"private": true,
"type": "module",
"types": "./dist/index.d.ts",
"exports": {
".": {
"browser": "./dist/index.browser.js",
"import": "./dist/index.js"
}
}
}
23 changes: 23 additions & 0 deletions e2e/fixtures/exports-condition/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "ssr-basic",
"version": "0.1.0",
"type": "module",
"private": true,
"scripts": {
"dev": "waku dev",
"build": "waku build",
"start": "waku start"
},
"dependencies": {
"react": "19.0.0-rc-d6cb4e77-20240911",
"react-dom": "19.0.0-rc-d6cb4e77-20240911",
"react-server-dom-webpack": "19.0.0-rc-d6cb4e77-20240911",
"waku": "workspace:*",
"my-module": "link:./modules/my-module"
},
"devDependencies": {
"@types/react": "^18.3.5",
"@types/react-dom": "^18.3.0",
"typescript": "^5.6.2"
}
}
23 changes: 23 additions & 0 deletions e2e/fixtures/exports-condition/src/components/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { runtime } from 'my-module';
import { Client } from './Client.js';

const App = ({ name }: { name: string }) => {
return (
<html>
<head>
<title>Waku example</title>
</head>
<body>
<div
style={{ border: '3px red dashed', margin: '1em', padding: '1em' }}
>
<h1 data-testid="app-name">{name}</h1>
<div data-testid="server-runtime">{runtime}</div>
<Client />
</div>
</body>
</html>
);
};

export default App;
15 changes: 15 additions & 0 deletions e2e/fixtures/exports-condition/src/components/Client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use client';
import { runtime } from 'my-module';
import { useEffect, useState } from 'react';

export const Client = () => {
// avoid hydration error, since runtime is different between client/server
const [mount, setMount] = useState(false);
useEffect(() => {
setMount(true);
}, []);
if (mount) {
return <div data-testid="client-runtime">{runtime}</div>;
}
return null;
};
28 changes: 28 additions & 0 deletions e2e/fixtures/exports-condition/src/entries.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { lazy } from 'react';
import { defineEntries } from 'waku/server';

Check failure on line 2 in e2e/fixtures/exports-condition/src/entries.tsx

View workflow job for this annotation

GitHub Actions / test

File '/home/runner/work/waku/waku/packages/waku/src/server.ts' is not listed within the file list of project '/home/runner/work/waku/waku/tsconfig.e2e.json'. Projects must list all files or use an 'include' pattern.
import { Slot } from 'waku/client';

Check failure on line 3 in e2e/fixtures/exports-condition/src/entries.tsx

View workflow job for this annotation

GitHub Actions / test

File '/home/runner/work/waku/waku/packages/waku/src/client.ts' is not listed within the file list of project '/home/runner/work/waku/waku/tsconfig.e2e.json'. Projects must list all files or use an 'include' pattern.

const App = lazy(() => import('./components/App.js'));

export default defineEntries(
// renderEntries
async (input) => {
return {
App: <App name={input || 'Waku'} />,
};
},
// getBuildConfig
async () => [{ pathname: '/', entries: [{ input: '' }] }],
// getSsrConfig
async (pathname) => {
switch (pathname) {
case '/':
return {
input: '',
html: <Slot id="App" />,
};
default:
return null;
}
},
);
17 changes: 17 additions & 0 deletions e2e/fixtures/exports-condition/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { StrictMode } from 'react';
import { createRoot, hydrateRoot } from 'react-dom/client';
import { Root, Slot } from 'waku/client';

const rootElement = (
<StrictMode>
<Root>
<Slot id="App" />
</Root>
</StrictMode>
);

if ((globalThis as any).__WAKU_HYDRATE__) {
hydrateRoot(document, rootElement);
} else {
createRoot(document as any).render(rootElement);
}
17 changes: 17 additions & 0 deletions e2e/fixtures/exports-condition/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"composite": true,
"strict": true,
"target": "esnext",
"downlevelIteration": true,
"esModuleInterop": true,
"module": "nodenext",
"skipLibCheck": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
"types": ["react/experimental"],
"jsx": "react-jsx",
"outDir": "./dist"
},
"include": ["./src", "./vite.config.ts"]
}
10 changes: 10 additions & 0 deletions e2e/fixtures/exports-condition/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default ({ mode }: { mode: string }) => {
if (mode === 'development') {
return {
optimizeDeps: {
exclude: ['ai/rsc'],
},
};
}
return {};
};
9 changes: 9 additions & 0 deletions packages/waku/src/lib/builder/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ const buildServerBundle = async (
esbuild: {
jsx: 'automatic',
},
resolve: {
conditions: ['import', 'module', 'default', 'production'],
},
define: {
'process.env.NODE_ENV': JSON.stringify('production'),
},
Expand Down Expand Up @@ -311,6 +314,9 @@ const buildSsrBundle = async (
esbuild: {
jsx: 'automatic',
},
resolve: {
conditions: ['import', 'module', 'default', 'production'],
},
define: {
'process.env.NODE_ENV': JSON.stringify('production'),
},
Expand Down Expand Up @@ -373,6 +379,9 @@ const buildClientBundle = async (
rscTransformPlugin({ isClient: true, isBuild: true, serverEntryFiles }),
...deployPlugins(config),
],
resolve: {
conditions: ['import', 'module', 'default', 'browser', 'production'],
},
build: {
emptyOutDir: !partial,
outDir: joinPath(rootDir, config.distDir, DIST_PUBLIC),
Expand Down
3 changes: 3 additions & 0 deletions packages/waku/src/lib/middleware/dev-server-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ const createMainViteServer = (
rscHmrPlugin(),
fsRouterTypegenPlugin(config),
],
resolve: {
conditions: ['import', 'module', 'default', 'production'],
},
optimizeDeps: {
include: ['react-server-dom-webpack/client', 'react-dom'],
exclude: ['waku', 'rsc-html-stream/server'],
Expand Down
28 changes: 28 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 3c2e0fa

Please sign in to comment.