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

Fix cjs client components tree-shaking #64558

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-s
import { getProxiedPluginState } from '../../build-context'
import { PAGE_TYPES } from '../../../lib/page-types'
import { isWebpackServerOnlyLayer } from '../../utils'
import { getModuleBuildInfo } from '../loaders/get-module-build-info'

interface Options {
dev: boolean
Expand Down Expand Up @@ -664,7 +665,16 @@ export class FlightClientEntryPlugin {
if (!modRequest) return
if (visited.has(modRequest)) {
if (clientComponentImports[modRequest]) {
const isCjsModule =
getModuleBuildInfo(mod).rsc?.clientEntryType === 'cjs'
for (const name of importedIdentifiers) {
// For cjs module default import, we include the whole module since
const isCjsDefaultImport = isCjsModule && name === 'default'
// Always include __esModule along with cjs module default export,
// to make sure it work with client module proxy from React.
if (isCjsDefaultImport) {
clientComponentImports[modRequest].add('__esModule')
}
clientComponentImports[modRequest].add(name)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import CjsClientDefault from 'cjs-client-module'

export default function Page() {
return <CjsClientDefault />
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,32 @@ createNextDescribe(
)
).toBe(false)
})

it('should only include the imported identifier of CJS module in browser bundle', async () => {
const clientChunksDir = join(
next.testDir,
'.next',
'static',
'chunks',
'app',
'cjs-dep'
)

const chunkContents = fs
.readdirSync(clientChunksDir, {
withFileTypes: true,
})
.filter((dirent) => dirent.isFile())
.map((chunkDirent) =>
fs.readFileSync(join(chunkDirent.path, chunkDirent.name), 'utf8')
)

expect(
chunkContents.some((content) => content.includes('cjs-client:default'))
).toBe(true)
expect(
chunkContents.every((content) => content.includes('cjs-client:foo'))
).toBe(false)
})
}
)

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion test/turbopack-build-tests-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14603,7 +14603,8 @@
"passed": [],
"failed": [
"app-dir client-components-tree-shaking should only include imported components 3rd party package in browser bundle with direct imports",
"app-dir client-components-tree-shaking should only include imported relative components in browser bundle with direct imports"
"app-dir client-components-tree-shaking should only include imported relative components in browser bundle with direct imports",
"app-dir client-components-tree-shaking should only include the imported identifier of CJS module in browser bundle"
],
"pending": [],
"flakey": [],
Expand Down