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

refactor: Use swc AST to determine use client and server directives #54358

Merged
merged 11 commits into from
Aug 23, 2023
25 changes: 22 additions & 3 deletions packages/next/src/build/analysis/get-page-static-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ export interface PageStaticInfo {

const CLIENT_MODULE_LABEL =
/\/\* __next_internal_client_entry_do_not_use__ ([^ ]*) (cjs|auto) \*\//

const ACTION_MODULE_LABEL =
/\/\* __next_internal_action_entry_do_not_use__ ([^ ]+) \*\//

const CLIENT_DIRECTIVE_REGEX = /^use client$/
const SERVER_ACTION_DIRECTIVE_REGEX = /^use server$/

export type RSCModuleType = 'server' | 'client'
export function getRSCModuleInformation(
source: string,
Expand All @@ -75,14 +79,12 @@ export function getRSCModuleInformation(
const clientEntryType = clientInfoMatch?.[2] as 'cjs' | 'auto'

const type = clientRefs ? RSC_MODULE_TYPES.client : RSC_MODULE_TYPES.server
const hasUseClientDirective = /^\s*['"]use client['"]/.test(source)
return {
type,
actions,
clientRefs,
clientEntryType,
isClientRef,
hasUseClientDirective,
}
}

Expand Down Expand Up @@ -124,6 +126,7 @@ function checkExports(
generateSitemaps?: boolean
generateStaticParams: boolean
extraProperties?: Set<string>
directives?: Set<string>
} {
const exportsSet = new Set<string>([
'getStaticProps',
Expand All @@ -142,6 +145,7 @@ function checkExports(
let generateSitemaps: boolean = false
let generateStaticParams = false
let extraProperties = new Set<string>()
let directives = new Set<string>()

for (const node of swcAST.body) {
huozhi marked this conversation as resolved.
Show resolved Hide resolved
if (
Expand Down Expand Up @@ -229,6 +233,18 @@ function checkExports(
)
}
}

if (node.type === 'ExpressionStatement') {
huozhi marked this conversation as resolved.
Show resolved Hide resolved
if (node.expression.type === 'StringLiteral') {
const directive = node.expression.value
if (CLIENT_DIRECTIVE_REGEX.test(directive)) {
huozhi marked this conversation as resolved.
Show resolved Hide resolved
directives.add('client')
}
if (SERVER_ACTION_DIRECTIVE_REGEX.test(directive)) {
huozhi marked this conversation as resolved.
Show resolved Hide resolved
directives.add('server')
}
}
}
}

return {
Expand All @@ -240,6 +256,7 @@ function checkExports(
generateSitemaps,
generateStaticParams,
extraProperties,
directives,
}
} catch (err) {}
}
Expand All @@ -253,6 +270,7 @@ function checkExports(
generateSitemaps: false,
generateStaticParams: false,
extraProperties: undefined,
directives: undefined,
}
}

Expand Down Expand Up @@ -467,6 +485,7 @@ export async function getPageStaticInfo(params: {
preferredRegion,
generateStaticParams,
extraProperties,
directives,
} = checkExports(swcAST, pageFilePath)
const rscInfo = getRSCModuleInformation(fileContent)
const rsc = rscInfo.type
Expand Down Expand Up @@ -575,7 +594,7 @@ export async function getPageStaticInfo(params: {

if (
pageType === 'app' &&
rscInfo.hasUseClientDirective &&
directives?.has('client') &&
generateStaticParams
) {
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export interface RSCMeta {
clientRefs?: string[]
clientEntryType?: 'cjs' | 'auto'
isClientRef?: boolean
hasUseClientDirective?: boolean
requests?: string[] // client requests in flight client entry
}

Expand Down