Unverified Commit 926ca238 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Handle empty ESM client modules (#72404)

If an ESM module only includes a `'use client'` directive and no
exports, which is the case for one of the modules that are included in
`@chakra-ui/system` for example, we must not try to add
`registerClientReference` statements for this module. This is a
regression from #71968.

fixes #72358
closes #72359
parent 1c219b16
...@@ -135,10 +135,13 @@ export function getRSCModuleInformation( ...@@ -135,10 +135,13 @@ export function getRSCModuleInformation(
} }
} }
const clientRefs = clientInfoMatch?.[1]?.split(',') const clientRefsString = clientInfoMatch?.[1]
const clientEntryType = clientInfoMatch?.[2] as 'cjs' | 'auto' const clientRefs = clientRefsString ? clientRefsString.split(',') : []
const clientEntryType = clientInfoMatch?.[2] as 'cjs' | 'auto' | undefined
const type = clientRefs ? RSC_MODULE_TYPES.client : RSC_MODULE_TYPES.server const type = clientInfoMatch
? RSC_MODULE_TYPES.client
: RSC_MODULE_TYPES.server
return { return {
type, type,
......
...@@ -34,10 +34,7 @@ export function getAssumedSourceType( ...@@ -34,10 +34,7 @@ export function getAssumedSourceType(
if (sourceType === 'auto') { if (sourceType === 'auto') {
if (detectedClientEntryType === 'auto') { if (detectedClientEntryType === 'auto') {
if ( if (clientRefs.length === 0) {
clientRefs.length === 0 ||
(clientRefs.length === 1 && clientRefs[0] === '')
) {
// If there's zero export detected in the client boundary, and it's the // If there's zero export detected in the client boundary, and it's the
// `auto` type, we can safely assume it's a CJS module because it doesn't // `auto` type, we can safely assume it's a CJS module because it doesn't
// have ESM exports. // have ESM exports.
...@@ -102,6 +99,10 @@ export default function transformSource( ...@@ -102,6 +99,10 @@ export default function transformSource(
const stringifiedResourceKey = JSON.stringify(resourceKey) const stringifiedResourceKey = JSON.stringify(resourceKey)
if (assumedSourceType === 'module') { if (assumedSourceType === 'module') {
if (clientRefs.length === 0) {
return this.callback(null, 'export {}')
}
if (clientRefs.includes('*')) { if (clientRefs.includes('*')) {
this.callback( this.callback(
new Error( new Error(
......
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import 'lib'
export default function Page() {
return <p>hello world</p>
}
import { nextTestSetup } from 'e2e-utils'
describe('esm-client-module-without-exports', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should render without errors', async () => {
const html = await next.render('/')
expect(html).toContain('hello world')
})
})
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment