Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSC: Improve type safety in RSC transform plugins #11155

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/vite/src/plugins/vite-plugin-rsc-transform-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path'

import type { Statement, ModuleDeclaration, AssignmentExpression } from 'acorn'
import type { AssignmentExpression, Program } from 'acorn'
import * as acorn from 'acorn-loose'
import { normalizePath, type Plugin } from 'vite'

Expand All @@ -27,7 +27,7 @@ export function rscTransformUseClientPlugin(
return code
}

let body: (Statement | ModuleDeclaration)[]
let body: Program['body']

try {
body = acorn.parse(code, {
Expand Down Expand Up @@ -126,7 +126,7 @@ function addExportNames(names: Array<string>, node: any) {
*/
async function parseExportNamesIntoNames(
code: string,
body: (Statement | ModuleDeclaration)[],
body: Program['body'],
names: Array<string>,
): Promise<void> {
for (let i = 0; i < body.length; i++) {
Expand Down Expand Up @@ -246,7 +246,7 @@ async function parseExportNamesIntoNames(

async function transformClientModule(
code: string,
body: (Statement | ModuleDeclaration)[],
body: Program['body'],
url: string,
clientEntryFiles: Record<string, string>,
): Promise<string> {
Expand Down
34 changes: 26 additions & 8 deletions packages/vite/src/plugins/vite-plugin-rsc-transform-server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as acorn from 'acorn-loose'
import type { AssignmentProperty, Expression, Pattern, Program } from 'acorn'
import { parse } from 'acorn-loose'
import type { Plugin } from 'vite'

export function rscTransformUseServerPlugin(): Plugin {
Expand All @@ -20,10 +21,10 @@ export function rscTransformUseServerPlugin(): Plugin {
return code
}

let body
let body: Program['body']

try {
body = acorn.parse(code, {
body = parse(code, {
ecmaVersion: 2024,
sourceType: 'module',
}).body
Expand Down Expand Up @@ -68,7 +69,10 @@ export function rscTransformUseServerPlugin(): Plugin {
}
}

function addLocalExportedNames(names: Map<string, string>, node: any) {
function addLocalExportedNames(
names: Map<string, string>,
node: Pattern | AssignmentProperty | Expression,
) {
switch (node.type) {
case 'Identifier':
names.set(node.name, node.name)
Expand Down Expand Up @@ -106,13 +110,20 @@ function addLocalExportedNames(names: Map<string, string>, node: any) {
case 'ParenthesizedExpression':
addLocalExportedNames(names, node.expression)
return

default:
throw new Error(`Unsupported node type: ${node.type}`)
}
}

function transformServerModule(body: any, url: string, code: string): string {
function transformServerModule(
body: Program['body'],
url: string,
code: string,
): string {
// If the same local name is exported more than once, we only need one of the names.
const localNames = new Map()
const localTypes = new Map()
const localNames = new Map<string, string>()
const localTypes = new Map<string, string>()

for (let i = 0; i < body.length; i++) {
const node = body[i]
Expand Down Expand Up @@ -157,7 +168,14 @@ function transformServerModule(body: any, url: string, code: string): string {

for (let j = 0; j < specifiers.length; j++) {
const specifier = specifiers[j]
localNames.set(specifier.local.name, specifier.exported.name)
if (
specifier.local.type === 'Identifier' &&
specifier.exported.type === 'Identifier'
) {
localNames.set(specifier.local.name, specifier.exported.name)
} else {
throw new Error('Unsupported export specifier')
}
}
}

Expand Down
Loading