-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
178 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import react from '@vitejs/plugin-react' | ||
import { build as viteBuild } from 'vite' | ||
|
||
import { onWarn } from '../lib/onWarn' | ||
import { rscIndexPlugin } from '../waku-lib/vite-plugin-rsc' | ||
|
||
/** | ||
* RSC build. Step 2. | ||
* buildFeServer -> buildRscFeServer -> rscBuildClient | ||
* Generate the client bundle | ||
*/ | ||
export async function rscBuildClient( | ||
webSrc: string, | ||
webHtml: string, | ||
webDist: string, | ||
clientEntryFiles: Record<string, string> | ||
) { | ||
const clientBuildOutput = await viteBuild({ | ||
// configFile: viteConfigPath, | ||
root: webSrc, | ||
plugins: [react(), rscIndexPlugin()], | ||
build: { | ||
outDir: webDist, | ||
emptyOutDir: true, // Needed because `outDir` is not inside `root` | ||
// TODO (RSC) Enable this when we switch to a server-first approach | ||
// emptyOutDir: false, // Already done when building server | ||
rollupOptions: { | ||
onwarn: onWarn, | ||
input: { | ||
main: webHtml, | ||
...clientEntryFiles, | ||
}, | ||
preserveEntrySignatures: 'exports-only', | ||
output: { | ||
// This is not ideal. See | ||
// https://rollupjs.org/faqs/#why-do-additional-imports-turn-up-in-my-entry-chunks-when-code-splitting | ||
// But we need it to prevent `import 'client-only'` from being | ||
// hoisted into App.tsx | ||
// TODO (RSC): Fix when https://github.com/rollup/rollup/issues/5235 | ||
// is resolved | ||
hoistTransitiveImports: false, | ||
}, | ||
}, | ||
manifest: 'client-build-manifest.json', | ||
}, | ||
esbuild: { | ||
logLevel: 'debug', | ||
}, | ||
}) | ||
|
||
if (!('output' in clientBuildOutput)) { | ||
throw new Error('Unexpected vite client build output') | ||
} | ||
|
||
return clientBuildOutput.output | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import fs from 'fs/promises' | ||
|
||
import type { rscBuildClient } from './rscBuildClient' | ||
import type { rscBuildServer } from './rscBuildServer' | ||
|
||
/** | ||
* RSC build. Step 5. | ||
* Append a mapping of server asset names to client asset names to the | ||
* `web/dist/server/entries.js` file. | ||
*/ | ||
export function rscBuildClientEntriesMappings( | ||
clientBuildOutput: Awaited<ReturnType<typeof rscBuildClient>>, | ||
serverBuildOutput: Awaited<ReturnType<typeof rscBuildServer>>, | ||
clientEntryFiles: Record<string, string>, | ||
webDistServerEntries: string | ||
) { | ||
const clientEntries: Record<string, string> = {} | ||
for (const item of clientBuildOutput) { | ||
const { name, fileName } = item | ||
const entryFile = | ||
name && | ||
// TODO (RSC) Can't we just compare the names? `item.name === name` | ||
serverBuildOutput.find( | ||
(item) => | ||
'moduleIds' in item && | ||
item.moduleIds.includes(clientEntryFiles[name] as string) | ||
)?.fileName | ||
|
||
if (entryFile) { | ||
console.log('entryFile', entryFile) | ||
if (process.platform === 'win32') { | ||
const entryFileSlash = entryFile.replaceAll('\\', '/') | ||
console.log('entryFileSlash', entryFileSlash) | ||
// Prevent errors on Windows like | ||
// Error: No client entry found for D:/a/redwood/rsc-project/web/dist/server/assets/rsc0.js | ||
clientEntries[entryFileSlash] = fileName | ||
} else { | ||
clientEntries[entryFile] = fileName | ||
} | ||
} | ||
} | ||
|
||
console.log('clientEntries', clientEntries) | ||
|
||
return fs.appendFile( | ||
webDistServerEntries, | ||
`export const clientEntries=${JSON.stringify(clientEntries)};` | ||
) | ||
} |
Oops, something went wrong.