2022-01-27 16:48:09 +01:00
|
|
|
import { parse } from '../../swc'
|
2022-03-28 13:34:32 +02:00
|
|
|
import { buildExports } from './utils'
|
2021-10-26 18:50:56 +02:00
|
|
|
|
2022-03-02 00:28:34 +01:00
|
|
|
const imageExtensions = ['jpg', 'jpeg', 'png', 'webp', 'avif']
|
2021-10-26 18:50:56 +02:00
|
|
|
|
2022-04-05 21:46:17 +02:00
|
|
|
export const createClientComponentFilter = (pageExtensions: string[]) => {
|
2022-03-02 00:28:34 +01:00
|
|
|
// Special cases for Next.js APIs that are considered as client components:
|
|
|
|
// - .client.[ext]
|
2022-04-05 21:46:17 +02:00
|
|
|
// - next built-in client components
|
2022-03-02 00:28:34 +01:00
|
|
|
// - .[imageExt]
|
|
|
|
const regex = new RegExp(
|
|
|
|
'(' +
|
|
|
|
`\\.client(\\.(${pageExtensions.join('|')}))?|` +
|
2022-04-05 21:46:17 +02:00
|
|
|
`next/(link|image)(\\.js)?|` +
|
2022-03-02 00:28:34 +01:00
|
|
|
`\\.(${imageExtensions.join('|')})` +
|
|
|
|
')$'
|
2021-10-26 18:50:56 +02:00
|
|
|
)
|
2022-03-02 00:28:34 +01:00
|
|
|
|
|
|
|
return (importSource: string) => regex.test(importSource)
|
2021-10-26 18:50:56 +02:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:46:17 +02:00
|
|
|
export const createServerComponentFilter = (pageExtensions: string[]) => {
|
2022-03-02 00:28:34 +01:00
|
|
|
const regex = new RegExp(`\\.server(\\.(${pageExtensions.join('|')}))?$`)
|
|
|
|
return (importSource: string) => regex.test(importSource)
|
2021-10-26 18:50:56 +02:00
|
|
|
}
|
|
|
|
|
2022-03-25 22:17:33 +01:00
|
|
|
async function parseModuleInfo({
|
2022-02-28 16:00:28 +01:00
|
|
|
resourcePath,
|
|
|
|
source,
|
|
|
|
isClientCompilation,
|
|
|
|
isServerComponent,
|
|
|
|
isClientComponent,
|
|
|
|
}: {
|
|
|
|
resourcePath: string
|
|
|
|
source: string
|
|
|
|
isClientCompilation: boolean
|
|
|
|
isServerComponent: (name: string) => boolean
|
|
|
|
isClientComponent: (name: string) => boolean
|
|
|
|
}): Promise<{
|
2022-01-14 14:01:00 +01:00
|
|
|
source: string
|
2022-03-02 19:29:54 +01:00
|
|
|
imports: string
|
2022-03-25 22:17:33 +01:00
|
|
|
isEsm: boolean
|
2022-04-01 18:13:38 +02:00
|
|
|
__N_SSP: boolean
|
|
|
|
pageRuntime: 'edge' | 'nodejs' | null
|
2022-01-14 14:01:00 +01:00
|
|
|
}> {
|
2022-03-28 13:34:32 +02:00
|
|
|
const ast = await parse(source, {
|
|
|
|
filename: resourcePath,
|
|
|
|
isModule: 'unknown',
|
|
|
|
})
|
|
|
|
const { type, body } = ast
|
2021-10-26 18:50:56 +02:00
|
|
|
let transformedSource = ''
|
|
|
|
let lastIndex = 0
|
2022-03-02 19:29:54 +01:00
|
|
|
let imports = ''
|
2022-04-01 18:13:38 +02:00
|
|
|
let __N_SSP = false
|
|
|
|
let pageRuntime = null
|
|
|
|
|
2022-03-28 13:34:32 +02:00
|
|
|
const isEsm = type === 'Module'
|
2022-03-02 19:29:54 +01:00
|
|
|
|
2021-10-26 18:50:56 +02:00
|
|
|
for (let i = 0; i < body.length; i++) {
|
|
|
|
const node = body[i]
|
|
|
|
switch (node.type) {
|
2022-04-01 18:13:38 +02:00
|
|
|
case 'ImportDeclaration':
|
2021-10-26 18:50:56 +02:00
|
|
|
const importSource = node.source.value
|
|
|
|
if (!isClientCompilation) {
|
2022-02-28 16:00:28 +01:00
|
|
|
// Server compilation for .server.js.
|
|
|
|
if (isServerComponent(importSource)) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
const importDeclarations = source.substring(
|
|
|
|
lastIndex,
|
2022-03-01 19:27:59 +01:00
|
|
|
node.source.span.start
|
2022-02-28 16:00:28 +01:00
|
|
|
)
|
|
|
|
|
2022-03-01 19:27:59 +01:00
|
|
|
if (isClientComponent(importSource)) {
|
|
|
|
// A client component. It should be loaded as module reference.
|
|
|
|
transformedSource += importDeclarations
|
|
|
|
transformedSource += JSON.stringify(`${importSource}?__sc_client__`)
|
2022-03-02 19:29:54 +01:00
|
|
|
imports += `require(${JSON.stringify(importSource)})\n`
|
2022-03-01 19:27:59 +01:00
|
|
|
} else {
|
2022-03-02 22:21:18 +01:00
|
|
|
// FIXME
|
|
|
|
// case: 'react'
|
|
|
|
// Avoid module resolution error like Cannot find `./?__rsc_server__` in react/package.json
|
|
|
|
|
|
|
|
// cases: 'react/jsx-runtime', 'react/jsx-dev-runtime'
|
2022-03-01 19:27:59 +01:00
|
|
|
// This is a special case to avoid the Duplicate React error.
|
|
|
|
// Since we already include React in the SSR runtime,
|
|
|
|
// here we can't create a new module with the ?__rsc_server__ query.
|
2022-02-28 16:00:28 +01:00
|
|
|
if (
|
2022-03-02 22:21:18 +01:00
|
|
|
['react', 'react/jsx-runtime', 'react/jsx-dev-runtime'].includes(
|
2022-02-28 16:00:28 +01:00
|
|
|
importSource
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// A shared component. It should be handled as a server
|
|
|
|
// component.
|
|
|
|
transformedSource += importDeclarations
|
|
|
|
transformedSource += JSON.stringify(`${importSource}?__sc_server__`)
|
2021-11-03 03:14:14 +01:00
|
|
|
}
|
2021-11-30 23:54:47 +01:00
|
|
|
} else {
|
|
|
|
// For the client compilation, we skip all modules imports but
|
|
|
|
// always keep client components in the bundle. All client components
|
|
|
|
// have to be imported from either server or client components.
|
|
|
|
if (
|
|
|
|
!(
|
2022-03-01 19:27:59 +01:00
|
|
|
isClientComponent(importSource) || isServerComponent(importSource)
|
2021-11-30 23:54:47 +01:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
continue
|
|
|
|
}
|
2022-02-28 16:00:28 +01:00
|
|
|
|
2022-03-02 19:29:54 +01:00
|
|
|
imports += `require(${JSON.stringify(importSource)})\n`
|
2021-10-26 18:50:56 +02:00
|
|
|
}
|
|
|
|
|
2022-03-01 19:27:59 +01:00
|
|
|
lastIndex = node.source.span.end
|
2022-02-28 16:00:28 +01:00
|
|
|
break
|
2022-04-01 18:13:38 +02:00
|
|
|
case 'ExportDeclaration':
|
|
|
|
if (isClientCompilation) {
|
|
|
|
// Keep `__N_SSG` and `__N_SSP` exports.
|
|
|
|
if (node.declaration?.type === 'VariableDeclaration') {
|
|
|
|
for (const declaration of node.declaration.declarations) {
|
|
|
|
if (declaration.type === 'VariableDeclarator') {
|
|
|
|
if (declaration.id?.type === 'Identifier') {
|
|
|
|
const value = declaration.id.value
|
|
|
|
if (value === '__N_SSP') {
|
|
|
|
__N_SSP = true
|
|
|
|
} else if (value === 'config') {
|
|
|
|
const props = declaration.init.properties
|
|
|
|
const runtimeKeyValue = props.find(
|
|
|
|
(prop: any) => prop.key.value === 'runtime'
|
|
|
|
)
|
|
|
|
const runtime = runtimeKeyValue?.value?.value
|
|
|
|
if (runtime === 'nodejs' || runtime === 'edge') {
|
|
|
|
pageRuntime = runtime
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
2021-10-26 18:50:56 +02:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isClientCompilation) {
|
2022-01-14 14:01:00 +01:00
|
|
|
transformedSource += source.substring(lastIndex)
|
2021-10-26 18:50:56 +02:00
|
|
|
}
|
|
|
|
|
2022-04-01 18:13:38 +02:00
|
|
|
return { source: transformedSource, imports, isEsm, __N_SSP, pageRuntime }
|
2021-10-26 18:50:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default async function transformSource(
|
|
|
|
this: any,
|
|
|
|
source: string
|
|
|
|
): Promise<string> {
|
2022-02-28 16:00:28 +01:00
|
|
|
const { client: isClientCompilation, pageExtensions } = this.getOptions()
|
|
|
|
const { resourcePath, resourceQuery } = this
|
2021-10-26 18:50:56 +02:00
|
|
|
|
|
|
|
if (typeof source !== 'string') {
|
|
|
|
throw new Error('Expected source to have been transformed to a string.')
|
|
|
|
}
|
|
|
|
|
2022-04-05 21:46:17 +02:00
|
|
|
const isServerComponent = createServerComponentFilter(pageExtensions)
|
|
|
|
const isClientComponent = createClientComponentFilter(pageExtensions)
|
2022-02-28 16:00:28 +01:00
|
|
|
|
|
|
|
if (!isClientCompilation) {
|
|
|
|
// We only apply the loader to server components, or shared components that
|
|
|
|
// are imported by a server component.
|
|
|
|
if (
|
|
|
|
!isServerComponent(resourcePath) &&
|
|
|
|
resourceQuery !== '?__sc_server__'
|
|
|
|
) {
|
|
|
|
return source
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 22:17:33 +01:00
|
|
|
const {
|
|
|
|
source: transformedSource,
|
|
|
|
imports,
|
|
|
|
isEsm,
|
2022-04-01 18:13:38 +02:00
|
|
|
__N_SSP,
|
|
|
|
pageRuntime,
|
2022-03-25 22:17:33 +01:00
|
|
|
} = await parseModuleInfo({
|
2022-03-02 19:29:54 +01:00
|
|
|
resourcePath,
|
|
|
|
source,
|
|
|
|
isClientCompilation,
|
|
|
|
isServerComponent,
|
|
|
|
isClientComponent,
|
|
|
|
})
|
2022-01-14 14:01:00 +01:00
|
|
|
|
|
|
|
/**
|
2022-02-08 14:16:46 +01:00
|
|
|
* For .server.js files, we handle this loader differently.
|
2022-01-14 14:01:00 +01:00
|
|
|
*
|
2022-02-08 14:16:46 +01:00
|
|
|
* Server compilation output:
|
2022-03-02 19:29:54 +01:00
|
|
|
* (The content of the Server Component module will be kept.)
|
2022-04-05 21:46:17 +02:00
|
|
|
* export const __next_rsc__ = { __webpack_require__, _: () => { ... }, server: true }
|
2022-01-14 14:01:00 +01:00
|
|
|
*
|
2022-02-08 14:16:46 +01:00
|
|
|
* Client compilation output:
|
2022-03-02 19:29:54 +01:00
|
|
|
* (The content of the Server Component module will be removed.)
|
2022-04-05 21:46:17 +02:00
|
|
|
* export const __next_rsc__ = { __webpack_require__, _: () => { ... }, server: false }
|
2022-01-14 14:01:00 +01:00
|
|
|
*/
|
|
|
|
|
2022-03-25 22:17:33 +01:00
|
|
|
const rscExports: any = {
|
|
|
|
__next_rsc__: `{
|
|
|
|
__webpack_require__,
|
2022-04-05 21:46:17 +02:00
|
|
|
_: () => {\n${imports}\n},
|
|
|
|
server: ${isServerComponent(resourcePath) ? 'true' : 'false'}
|
2022-03-25 22:17:33 +01:00
|
|
|
}`,
|
|
|
|
}
|
2022-02-11 19:30:39 +01:00
|
|
|
|
|
|
|
if (isClientCompilation) {
|
2022-04-01 18:13:38 +02:00
|
|
|
rscExports.default = 'function RSC() {}'
|
|
|
|
|
|
|
|
if (pageRuntime === 'edge') {
|
|
|
|
// Currently for the Edge runtime, we treat all RSC pages as SSR pages.
|
|
|
|
rscExports.__N_SSP = 'true'
|
|
|
|
} else {
|
|
|
|
if (__N_SSP) {
|
|
|
|
rscExports.__N_SSP = 'true'
|
|
|
|
} else {
|
|
|
|
// Server component pages are always considered as SSG by default because
|
|
|
|
// the flight data is needed for client navigation.
|
|
|
|
rscExports.__N_SSG = 'true'
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 19:30:39 +01:00
|
|
|
}
|
2022-01-14 14:01:00 +01:00
|
|
|
|
2022-03-25 22:17:33 +01:00
|
|
|
const output = transformedSource + '\n' + buildExports(rscExports, isEsm)
|
|
|
|
return output
|
2021-10-26 18:50:56 +02:00
|
|
|
}
|