2021-12-20 14:01:35 +01:00
|
|
|
import * as acorn from 'next/dist/compiled/acorn'
|
2021-10-26 18:50:56 +02:00
|
|
|
import { getRawPageExtensions } from '../../utils'
|
|
|
|
|
|
|
|
function isClientComponent(importSource: string, pageExtensions: string[]) {
|
|
|
|
return new RegExp(`\\.client(\\.(${pageExtensions.join('|')}))?`).test(
|
|
|
|
importSource
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-11-30 23:54:47 +01:00
|
|
|
function isServerComponent(importSource: string, pageExtensions: string[]) {
|
|
|
|
return new RegExp(`\\.server(\\.(${pageExtensions.join('|')}))?`).test(
|
|
|
|
importSource
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-10-26 18:50:56 +02:00
|
|
|
function isNextComponent(importSource: string) {
|
|
|
|
return (
|
|
|
|
importSource.includes('next/link') || importSource.includes('next/image')
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isImageImport(importSource: string) {
|
|
|
|
// TODO: share extension with next/image
|
|
|
|
// TODO: add other static assets, jpeg -> jpg
|
|
|
|
return ['jpg', 'jpeg', 'png', 'webp', 'avif'].some((imageExt) =>
|
|
|
|
importSource.endsWith('.' + imageExt)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function parseImportsInfo(
|
|
|
|
source: string,
|
|
|
|
imports: Array<string>,
|
|
|
|
isClientCompilation: boolean,
|
|
|
|
pageExtensions: string[]
|
2022-01-14 14:01:00 +01:00
|
|
|
): Promise<{
|
|
|
|
source: string
|
|
|
|
defaultExportName: string
|
|
|
|
}> {
|
2021-10-26 18:50:56 +02:00
|
|
|
const { body } = acorn.parse(source, {
|
2021-11-05 04:27:02 +01:00
|
|
|
ecmaVersion: 11,
|
2021-10-26 18:50:56 +02:00
|
|
|
sourceType: 'module',
|
|
|
|
}) as any
|
|
|
|
|
|
|
|
let transformedSource = ''
|
|
|
|
let lastIndex = 0
|
2022-01-24 17:19:57 +01:00
|
|
|
let defaultExportName = 'RSCComponent'
|
2021-10-26 18:50:56 +02:00
|
|
|
|
|
|
|
for (let i = 0; i < body.length; i++) {
|
|
|
|
const node = body[i]
|
|
|
|
switch (node.type) {
|
2022-01-14 14:01:00 +01:00
|
|
|
case 'ImportDeclaration': {
|
2021-10-26 18:50:56 +02:00
|
|
|
const importSource = node.source.value
|
|
|
|
|
|
|
|
if (!isClientCompilation) {
|
2021-11-03 03:14:14 +01:00
|
|
|
if (
|
|
|
|
!(
|
|
|
|
isClientComponent(importSource, pageExtensions) ||
|
|
|
|
isNextComponent(importSource) ||
|
|
|
|
isImageImport(importSource)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
continue
|
|
|
|
}
|
2022-01-14 14:01:00 +01:00
|
|
|
transformedSource += source.substring(
|
2021-10-26 18:50:56 +02:00
|
|
|
lastIndex,
|
2022-01-14 14:01:00 +01:00
|
|
|
node.source.start - 1
|
2021-10-26 18:50:56 +02:00
|
|
|
)
|
|
|
|
transformedSource += JSON.stringify(`${node.source.value}?flight`)
|
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 (
|
|
|
|
!(
|
|
|
|
isClientComponent(importSource, pageExtensions) ||
|
|
|
|
isServerComponent(importSource, pageExtensions) ||
|
|
|
|
// Special cases for Next.js APIs that are considered as client
|
|
|
|
// components:
|
|
|
|
isNextComponent(importSource) ||
|
|
|
|
isImageImport(importSource)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
continue
|
|
|
|
}
|
2021-10-26 18:50:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
lastIndex = node.source.end
|
|
|
|
imports.push(`require(${JSON.stringify(importSource)})`)
|
|
|
|
continue
|
2022-01-14 14:01:00 +01:00
|
|
|
}
|
|
|
|
case 'ExportDefaultDeclaration': {
|
2022-01-24 17:19:57 +01:00
|
|
|
const def = node.declaration
|
|
|
|
if (def.type === 'Identifier') {
|
|
|
|
defaultExportName = def.name
|
|
|
|
} else if (def.type === 'FunctionDeclaration') {
|
|
|
|
defaultExportName = def.id.name
|
|
|
|
}
|
2022-01-14 14:01:00 +01:00
|
|
|
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-01-14 14:01:00 +01:00
|
|
|
return { source: transformedSource, defaultExportName }
|
2021-10-26 18:50:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default async function transformSource(
|
|
|
|
this: any,
|
|
|
|
source: string
|
|
|
|
): Promise<string> {
|
|
|
|
const { client: isClientCompilation, pageExtensions: pageExtensionsJson } =
|
2021-11-02 16:13:15 +01:00
|
|
|
this.getOptions()
|
2021-10-26 18:50:56 +02:00
|
|
|
const { resourcePath } = this
|
|
|
|
const pageExtensions = JSON.parse(pageExtensionsJson)
|
|
|
|
|
|
|
|
if (typeof source !== 'string') {
|
|
|
|
throw new Error('Expected source to have been transformed to a string.')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resourcePath.includes('/node_modules/')) {
|
|
|
|
return source
|
|
|
|
}
|
|
|
|
|
|
|
|
const imports: string[] = []
|
2022-01-14 14:01:00 +01:00
|
|
|
const { source: transformedSource, defaultExportName } =
|
|
|
|
await parseImportsInfo(
|
|
|
|
source,
|
|
|
|
imports,
|
|
|
|
isClientCompilation,
|
|
|
|
getRawPageExtensions(pageExtensions)
|
|
|
|
)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Server side component module output:
|
|
|
|
*
|
|
|
|
* export default function ServerComponent() { ... }
|
|
|
|
* + export const __rsc_noop__=()=>{ ... }
|
|
|
|
* + ServerComponent.__next_rsc__=1;
|
|
|
|
*
|
|
|
|
* Client side component module output:
|
|
|
|
*
|
|
|
|
* The function body of ServerComponent will be removed
|
|
|
|
*/
|
|
|
|
|
|
|
|
const noop = `export const __rsc_noop__=()=>{${imports.join(';')}}`
|
2021-10-26 18:50:56 +02:00
|
|
|
const defaultExportNoop = isClientCompilation
|
2022-01-14 14:01:00 +01:00
|
|
|
? `export default function ${defaultExportName}(){}\n${defaultExportName}.__next_rsc__=1;`
|
|
|
|
: `${defaultExportName}.__next_rsc__=1;`
|
|
|
|
|
|
|
|
const transformed = transformedSource + '\n' + noop + '\n' + defaultExportNoop
|
2021-11-03 03:14:14 +01:00
|
|
|
|
2022-01-14 14:01:00 +01:00
|
|
|
return transformed
|
2021-10-26 18:50:56 +02:00
|
|
|
}
|