Skip to content

Commit

Permalink
RSC: Make rw dev not crash for /about
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe committed Mar 11, 2024
1 parent 598df34 commit b3bec47
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions packages/vite/src/plugins/vite-plugin-rsc-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { Plugin } from 'vite'
import { getPaths } from '@redwoodjs/project-config'

export function rscTransformPlugin(
clientEntryFiles: Record<string, string>,
clientEntryFiles: Record<string, string>

Check warning on line 9 in packages/vite/src/plugins/vite-plugin-rsc-transform.ts

View workflow job for this annotation

GitHub Actions / πŸ— Build, lint, test / ubuntu-latest / node 20 latest

Insert `,`

Check warning on line 9 in packages/vite/src/plugins/vite-plugin-rsc-transform.ts

View workflow job for this annotation

GitHub Actions / πŸ— Build, lint, test / windows-latest / node 20 latest

Insert `,`
): Plugin {
return {
name: 'rsc-transform-plugin',
Expand Down Expand Up @@ -63,7 +63,7 @@ export function rscTransformPlugin(

if (useClient && useServer) {
throw new Error(
'Cannot have both "use client" and "use server" directives in the same file.',
'Cannot have both "use client" and "use server" directives in the same file.'

Check warning on line 66 in packages/vite/src/plugins/vite-plugin-rsc-transform.ts

View workflow job for this annotation

GitHub Actions / πŸ— Build, lint, test / ubuntu-latest / node 20 latest

Insert `,`

Check warning on line 66 in packages/vite/src/plugins/vite-plugin-rsc-transform.ts

View workflow job for this annotation

GitHub Actions / πŸ— Build, lint, test / windows-latest / node 20 latest

Insert `,`
)
}

Expand All @@ -74,7 +74,7 @@ export function rscTransformPlugin(
code,
body,
id,
clientEntryFiles,
clientEntryFiles

Check warning on line 77 in packages/vite/src/plugins/vite-plugin-rsc-transform.ts

View workflow job for this annotation

GitHub Actions / πŸ— Build, lint, test / ubuntu-latest / node 20 latest

Insert `,`

Check warning on line 77 in packages/vite/src/plugins/vite-plugin-rsc-transform.ts

View workflow job for this annotation

GitHub Actions / πŸ— Build, lint, test / windows-latest / node 20 latest

Insert `,`
)
} else {
transformedCode = transformServerModule(code, body, id)
Expand Down Expand Up @@ -246,7 +246,7 @@ function addExportNames(names: Array<string>, node: any) {
async function parseExportNamesIntoNames(
code: string,
body: any,
names: Array<string>,
names: Array<string>

Check warning on line 249 in packages/vite/src/plugins/vite-plugin-rsc-transform.ts

View workflow job for this annotation

GitHub Actions / πŸ— Build, lint, test / ubuntu-latest / node 20 latest

Insert `,`

Check warning on line 249 in packages/vite/src/plugins/vite-plugin-rsc-transform.ts

View workflow job for this annotation

GitHub Actions / πŸ— Build, lint, test / windows-latest / node 20 latest

Insert `,`
): Promise<void> {
for (let i = 0; i < body.length; i++) {
const node = body[i]
Expand Down Expand Up @@ -308,27 +308,23 @@ async function transformClientModule(
code: string,
body: any,
url: string,
clientEntryFiles: Record<string, string>,
clientEntryFiles: Record<string, string>

Check warning on line 311 in packages/vite/src/plugins/vite-plugin-rsc-transform.ts

View workflow job for this annotation

GitHub Actions / πŸ— Build, lint, test / ubuntu-latest / node 20 latest

Insert `,`

Check warning on line 311 in packages/vite/src/plugins/vite-plugin-rsc-transform.ts

View workflow job for this annotation

GitHub Actions / πŸ— Build, lint, test / windows-latest / node 20 latest

Insert `,`
): Promise<string> {
const names: Array<string> = []

// This will insert the names into the `names` array
await parseExportNamesIntoNames(code, body, names)
console.log('transformClientModule names', names)

// entryRecord will be undefined for dev, because clientEntryFiles will just
// be an empty object. See rscWorker.ts, where we do rscTransformPlugin({})
const entryRecord = Object.entries(clientEntryFiles).find(
([_key, value]) => value === url,
([_key, value]) => value === url

Check warning on line 322 in packages/vite/src/plugins/vite-plugin-rsc-transform.ts

View workflow job for this annotation

GitHub Actions / πŸ— Build, lint, test / ubuntu-latest / node 20 latest

Insert `,`

Check warning on line 322 in packages/vite/src/plugins/vite-plugin-rsc-transform.ts

View workflow job for this annotation

GitHub Actions / πŸ— Build, lint, test / windows-latest / node 20 latest

Insert `,`
)

if (!entryRecord || !entryRecord[0]) {
throw new Error('Entry not found for ' + url)
}

const loadId = path.join(
getPaths().web.distRsc,
'assets',
`${entryRecord[0]}.mjs`,
)
const loadId = entryRecord
? path.join(getPaths().web.distRsc, 'assets', `${entryRecord[0]}.mjs`)
: url

let newSrc =
"const CLIENT_REFERENCE = Symbol.for('react.client.reference');\n"
Expand All @@ -346,7 +342,7 @@ async function transformClientModule(
url +
" from the server but it's on the client. It's not possible to " +
'invoke a client function from the server, it can only be ' +
'rendered as a Component or passed to props of a Client Component.',
'rendered as a Component or passed to props of a Client Component.'

Check warning on line 345 in packages/vite/src/plugins/vite-plugin-rsc-transform.ts

View workflow job for this annotation

GitHub Actions / πŸ— Build, lint, test / ubuntu-latest / node 20 latest

Insert `,`

Check warning on line 345 in packages/vite/src/plugins/vite-plugin-rsc-transform.ts

View workflow job for this annotation

GitHub Actions / πŸ— Build, lint, test / windows-latest / node 20 latest

Insert `,`
) +
');'
} else {
Expand All @@ -361,7 +357,7 @@ async function transformClientModule(
name +
' is on the client. ' +
"It's not possible to invoke a client function from the server, it can " +
'only be rendered as a Component or passed to props of a Client Component.',
'only be rendered as a Component or passed to props of a Client Component.'

Check warning on line 360 in packages/vite/src/plugins/vite-plugin-rsc-transform.ts

View workflow job for this annotation

GitHub Actions / πŸ— Build, lint, test / ubuntu-latest / node 20 latest

Insert `,`

Check warning on line 360 in packages/vite/src/plugins/vite-plugin-rsc-transform.ts

View workflow job for this annotation

GitHub Actions / πŸ— Build, lint, test / windows-latest / node 20 latest

Insert `,`
) +
');'
}
Expand Down

0 comments on commit b3bec47

Please sign in to comment.