2022-01-27 16:48:09 +01:00
|
|
|
import { parse } from '../../swc'
|
2021-10-26 18:50:56 +02:00
|
|
|
import { getRawPageExtensions } from '../../utils'
|
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-03-02 00:28:34 +01:00
|
|
|
const createClientComponentFilter = (pageExtensions: string[]) => {
|
|
|
|
// Special cases for Next.js APIs that are considered as client components:
|
|
|
|
// - .client.[ext]
|
|
|
|
// - next/link, next/image
|
|
|
|
// - .[imageExt]
|
|
|
|
const regex = new RegExp(
|
|
|
|
'(' +
|
|
|
|
`\\.client(\\.(${pageExtensions.join('|')}))?|` +
|
|
|
|
`next/link|next/image|` +
|
|
|
|
`\\.(${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-03-02 00:28:34 +01:00
|
|
|
const createServerComponentFilter = (pageExtensions: string[]) => {
|
|
|
|
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-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-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-01-14 14:01:00 +01: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-01-14 14:01:00 +01:00
|
|
|
}
|
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-03-25 22:17:33 +01:00
|
|
|
return { source: transformedSource, imports, isEsm }
|
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-02-28 16:00:28 +01:00
|
|
|
// We currently assume that all components are shared components (unsuffixed)
|
|
|
|
// from node_modules.
|
2021-10-26 18:50:56 +02:00
|
|
|
if (resourcePath.includes('/node_modules/')) {
|
|
|
|
return source
|
|
|
|
}
|
|
|
|
|
2022-02-28 16:00:28 +01:00
|
|
|
const rawRawPageExtensions = getRawPageExtensions(pageExtensions)
|
2022-03-01 19:27:59 +01:00
|
|
|
const isServerComponent = createServerComponentFilter(rawRawPageExtensions)
|
|
|
|
const isClientComponent = createClientComponentFilter(rawRawPageExtensions)
|
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,
|
|
|
|
} = 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.)
|
|
|
|
* export const __next_rsc__ = { __webpack_require__, _: () => { ... } }
|
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.)
|
|
|
|
* export const __next_rsc__ = { __webpack_require__, _: () => { ... } }
|
2022-01-14 14:01:00 +01:00
|
|
|
*/
|
|
|
|
|
2022-03-25 22:17:33 +01:00
|
|
|
const rscExports: any = {
|
|
|
|
__next_rsc__: `{
|
|
|
|
__webpack_require__,
|
|
|
|
_: () => {\n${imports}\n}
|
|
|
|
}`,
|
2022-03-28 16:24:57 +02:00
|
|
|
__next_rsc_server__: isServerComponent(resourcePath) ? 'true' : 'false',
|
2022-03-25 22:17:33 +01:00
|
|
|
}
|
2022-02-11 19:30:39 +01:00
|
|
|
|
|
|
|
if (isClientCompilation) {
|
2022-03-25 22:17:33 +01:00
|
|
|
rscExports['default'] = 'function RSC() {}'
|
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
|
|
|
}
|