Skip to content

Commit

Permalink
Improve Server Reference ID DCE tests (#70191)
Browse files Browse the repository at this point in the history
This PR improves the integration test of dead code elimination of Server
Reference IDs in the client bundle, to cover:
- Barrel files (re-exports and nested imports)
- Side effects in server boundary and intermediate modules
- SWC compression
  • Loading branch information
shuding authored Sep 17, 2024
1 parent 05dd22d commit 2397507
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
'use server'

// Ensure side effects won't affect tree shaking and DCE
console.log('This is a side effect')

export async function foo() {
console.log('This is action foo')
}

export async function bar() {
console.log('This is action bar')
}

export async function baz() {
console.log('This is action baz')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { baz } from '../actions'

// Ensure side effects won't affect tree shaking and DCE
console.log(1)

export { baz }
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use client'

import { baz } from './barrel'

export default function Page() {
// Test DCE
if (1 + 1 === 3) {
baz()
}

return <div>hi</div>
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ describe('app-dir - client-actions-tree-shaking', () => {
const route2Files = await fs.readdir(
join(appDir, '.next/static/chunks/app/route-2')
)
const route3Files = await fs.readdir(
join(appDir, '.next/static/chunks/app/route-3')
)

const route1Bundle = await fs.readFile(
join(
Expand All @@ -52,14 +55,25 @@ describe('app-dir - client-actions-tree-shaking', () => {
route2Files.find((file) => file.endsWith('.js'))
)
)
const route3Bundle = await fs.readFile(
join(
appDir,
'.next/static/chunks/app/route-3',
route3Files.find((file) => file.endsWith('.js'))
)
)

const bundle1Ids = getServerReferenceIdsFromBundle(route1Bundle.toString())
const bundle2Ids = getServerReferenceIdsFromBundle(route2Bundle.toString())
const bundle3Ids = getServerReferenceIdsFromBundle(route3Bundle.toString())

// Each should only have one ID.
// Bundle 1 and 2 should only have one ID.
expect(bundle1Ids).toHaveLength(1)
expect(bundle2Ids).toHaveLength(1)
expect(bundle1Ids[0]).not.toEqual(bundle2Ids[0])

// Bundle 3 should have no IDs.
expect(bundle3Ids).toHaveLength(0)
})

// Test the application
Expand Down

0 comments on commit 2397507

Please sign in to comment.