-
Notifications
You must be signed in to change notification settings - Fork 27.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply optimization for unused actions (#69178)
- Loading branch information
1 parent
5eff016
commit 9c4efb9
Showing
39 changed files
with
841 additions
and
24 deletions.
There are no files selected for viewing
211 changes: 187 additions & 24 deletions
211
packages/next/src/build/webpack/plugins/flight-client-entry-plugin.ts
Large diffs are not rendered by default.
Oops, something went wrong.
78 changes: 78 additions & 0 deletions
78
test/production/app-dir/actions-tree-shaking/_testing/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { type NextInstance } from 'e2e-utils' | ||
|
||
async function getActionsMappingByRuntime( | ||
next: NextInstance, | ||
runtime: 'node' | 'edge' | ||
) { | ||
const manifest = JSON.parse( | ||
await next.readFile('.next/server/server-reference-manifest.json') | ||
) | ||
|
||
return manifest[runtime] | ||
} | ||
|
||
export function markLayoutAsEdge(next: NextInstance) { | ||
beforeAll(async () => { | ||
await next.stop() | ||
const layoutContent = await next.readFile('app/layout.js') | ||
await next.patchFile( | ||
'app/layout.js', | ||
layoutContent + `\nexport const runtime = 'edge'` | ||
) | ||
await next.start() | ||
}) | ||
} | ||
|
||
/* | ||
{ | ||
[route path]: { [layer]: Set<workerId> ] | ||
} | ||
*/ | ||
type ActionsMappingOfRuntime = { | ||
[actionId: string]: { | ||
workers: { | ||
[route: string]: string | ||
} | ||
layer: { | ||
[route: string]: string | ||
} | ||
} | ||
} | ||
type ActionState = { | ||
[route: string]: { | ||
[layer: string]: number | ||
} | ||
} | ||
|
||
function getActionsRoutesState( | ||
actionsMappingOfRuntime: ActionsMappingOfRuntime | ||
): ActionState { | ||
const state: ActionState = {} | ||
Object.keys(actionsMappingOfRuntime).forEach((actionId) => { | ||
const action = actionsMappingOfRuntime[actionId] | ||
const routePaths = Object.keys(action.workers) | ||
|
||
routePaths.forEach((routePath) => { | ||
if (!state[routePath]) { | ||
state[routePath] = {} | ||
} | ||
const layer = action.layer[routePath] | ||
|
||
if (!state[routePath][layer]) { | ||
state[routePath][layer] = 0 | ||
} | ||
|
||
state[routePath][layer]++ | ||
}) | ||
}) | ||
|
||
return state | ||
} | ||
|
||
export async function getActionsRoutesStateByRuntime(next: NextInstance) { | ||
const actionsMappingOfRuntime = await getActionsMappingByRuntime( | ||
next, | ||
process.env.TEST_EDGE ? 'edge' : 'node' | ||
) | ||
return getActionsRoutesState(actionsMappingOfRuntime) | ||
} |
13 changes: 13 additions & 0 deletions
13
test/production/app-dir/actions-tree-shaking/basic/app/actions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use server' | ||
|
||
export async function serverComponentAction() { | ||
return 'server-action' | ||
} | ||
|
||
export async function clientComponentAction() { | ||
return 'client-action' | ||
} | ||
|
||
export async function unusedExportedAction() { | ||
return 'unused-exported-action' | ||
} |
21 changes: 21 additions & 0 deletions
21
test/production/app-dir/actions-tree-shaking/basic/app/client/page.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use client' | ||
|
||
import { useState } from 'react' | ||
import { clientComponentAction } from '../actions' | ||
|
||
export default function Page() { | ||
const [text, setText] = useState('initial') | ||
return ( | ||
<div> | ||
<button | ||
id="action-1" | ||
onClick={async () => { | ||
setText(await clientComponentAction()) | ||
}} | ||
> | ||
Action 1 | ||
</button> | ||
<span>{text}</span> | ||
</div> | ||
) | ||
} |
13 changes: 13 additions & 0 deletions
13
test/production/app-dir/actions-tree-shaking/basic/app/inline/page.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export default function Page() { | ||
// Inline Server Action | ||
async function inlineServerAction() { | ||
'use server' | ||
return 'inline-server-action' | ||
} | ||
|
||
return ( | ||
<form action={inlineServerAction}> | ||
<button type="submit">Submit</button> | ||
</form> | ||
) | ||
} |
7 changes: 7 additions & 0 deletions
7
test/production/app-dir/actions-tree-shaking/basic/app/layout.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function Layout({ children }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
10 changes: 10 additions & 0 deletions
10
test/production/app-dir/actions-tree-shaking/basic/app/server/page.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { serverComponentAction } from '../actions' | ||
|
||
export default function Page() { | ||
return ( | ||
<form> | ||
<input type="text" placeholder="input" /> | ||
<button formAction={serverComponentAction}>submit</button> | ||
</form> | ||
) | ||
} |
3 changes: 3 additions & 0 deletions
3
test/production/app-dir/actions-tree-shaking/basic/basic-edge.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
process.env.TEST_EDGE = '1' | ||
|
||
require('./basic.test') |
33 changes: 33 additions & 0 deletions
33
test/production/app-dir/actions-tree-shaking/basic/basic.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
import { | ||
getActionsRoutesStateByRuntime, | ||
markLayoutAsEdge, | ||
} from '../_testing/utils' | ||
|
||
describe('actions-tree-shaking - basic', () => { | ||
const { next } = nextTestSetup({ | ||
files: __dirname, | ||
}) | ||
|
||
if (process.env.TEST_EDGE) { | ||
markLayoutAsEdge(next) | ||
} | ||
|
||
it('should not have the unused action in the manifest', async () => { | ||
const actionsRoutesState = await getActionsRoutesStateByRuntime(next) | ||
|
||
expect(actionsRoutesState).toMatchObject({ | ||
// only one server layer action | ||
'app/server/page': { | ||
rsc: 1, | ||
}, | ||
// only one browser layer action | ||
'app/client/page': { | ||
'action-browser': 1, | ||
}, | ||
'app/inline/page': { | ||
rsc: 1, | ||
}, | ||
}) | ||
}) | ||
}) |
7 changes: 7 additions & 0 deletions
7
test/production/app-dir/actions-tree-shaking/mixed-module-actions/app/layout.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function Layout({ children }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
13 changes: 13 additions & 0 deletions
13
...duction/app-dir/actions-tree-shaking/mixed-module-actions/app/mixed-module/cjs/actions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use server' | ||
|
||
export async function esmModuleTypeAction() { | ||
return 'esm-module-type-action' | ||
} | ||
|
||
export async function cjsModuleTypeAction() { | ||
return 'cjs-module-type-action' | ||
} | ||
|
||
export async function unusedModuleTypeAction1() { | ||
return 'unused-module-type-action-1' | ||
} |
13 changes: 13 additions & 0 deletions
13
...production/app-dir/actions-tree-shaking/mixed-module-actions/app/mixed-module/cjs/page.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const { cjsModuleTypeAction } = require('./actions') | ||
|
||
export default function Page() { | ||
return ( | ||
<div> | ||
<h3>One</h3> | ||
<form> | ||
<input type="text" placeholder="input" /> | ||
<button formAction={cjsModuleTypeAction}>submit</button> | ||
</form> | ||
</div> | ||
) | ||
} |
13 changes: 13 additions & 0 deletions
13
...duction/app-dir/actions-tree-shaking/mixed-module-actions/app/mixed-module/esm/actions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use server' | ||
|
||
export async function esmModuleTypeAction() { | ||
return 'esm-module-type-action' | ||
} | ||
|
||
export async function cjsModuleTypeAction() { | ||
return 'cjs-module-type-action' | ||
} | ||
|
||
export async function unusedModuleTypeAction1() { | ||
return 'unused-module-type-action-1' | ||
} |
13 changes: 13 additions & 0 deletions
13
...production/app-dir/actions-tree-shaking/mixed-module-actions/app/mixed-module/esm/page.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { esmModuleTypeAction } from './actions' | ||
|
||
export default function Page() { | ||
return ( | ||
<div> | ||
<h3>One</h3> | ||
<form> | ||
<input type="text" placeholder="input" /> | ||
<button formAction={esmModuleTypeAction}>submit</button> | ||
</form> | ||
</div> | ||
) | ||
} |
3 changes: 3 additions & 0 deletions
3
...ction/app-dir/actions-tree-shaking/mixed-module-actions/mixed-module-actions-edge.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
process.env.TEST_EDGE = '1' | ||
|
||
require('./mixed-module-actions.test') |
29 changes: 29 additions & 0 deletions
29
...production/app-dir/actions-tree-shaking/mixed-module-actions/mixed-module-actions.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
import { | ||
getActionsRoutesStateByRuntime, | ||
markLayoutAsEdge, | ||
} from '../_testing/utils' | ||
|
||
describe('actions-tree-shaking - mixed-module-actions', () => { | ||
const { next } = nextTestSetup({ | ||
files: __dirname, | ||
}) | ||
|
||
if (process.env.TEST_EDGE) { | ||
markLayoutAsEdge(next) | ||
} | ||
|
||
it('should not do tree shake for cjs module when import server actions', async () => { | ||
const actionsRoutesState = await getActionsRoutesStateByRuntime(next) | ||
|
||
expect(actionsRoutesState).toMatchObject({ | ||
'app/mixed-module/esm/page': { | ||
rsc: 1, | ||
}, | ||
// CJS import is not able to tree shake, so it will include all actions | ||
'app/mixed-module/cjs/page': { | ||
rsc: 3, | ||
}, | ||
}) | ||
}) | ||
}) |
7 changes: 7 additions & 0 deletions
7
test/production/app-dir/actions-tree-shaking/reexport/app/layout.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function Layout({ children }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
13 changes: 13 additions & 0 deletions
13
test/production/app-dir/actions-tree-shaking/reexport/app/named-reexport/client/actions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use server' | ||
|
||
export async function sharedClientLayerAction() { | ||
return 'shared-client-layer-action' | ||
} | ||
|
||
export async function unusedClientLayerAction1() { | ||
return 'unused-client-layer-action-1' | ||
} | ||
|
||
export async function unusedClientLayerAction2() { | ||
return 'unused-client-layer-action-2' | ||
} |
21 changes: 21 additions & 0 deletions
21
test/production/app-dir/actions-tree-shaking/reexport/app/named-reexport/client/page.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use client' | ||
|
||
import { useState } from 'react' | ||
import { sharedClientLayerAction } from './reexport-action' | ||
|
||
export default function Page() { | ||
const [text, setText] = useState('initial') | ||
return ( | ||
<div> | ||
<button | ||
id="action-1" | ||
onClick={async () => { | ||
setText(await sharedClientLayerAction()) | ||
}} | ||
> | ||
Action 1 | ||
</button> | ||
<span>{text}</span> | ||
</div> | ||
) | ||
} |
5 changes: 5 additions & 0 deletions
5
...uction/app-dir/actions-tree-shaking/reexport/app/named-reexport/client/reexport-action.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export { | ||
sharedClientLayerAction, | ||
unusedClientLayerAction1, | ||
unusedClientLayerAction2, | ||
} from './actions' |
13 changes: 13 additions & 0 deletions
13
test/production/app-dir/actions-tree-shaking/reexport/app/named-reexport/server/actions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use server' | ||
|
||
export async function sharedServerLayerAction() { | ||
return 'shared-server-layer-action' | ||
} | ||
|
||
export async function unusedServerLayerAction1() { | ||
return 'unused-server-layer-action-1' | ||
} | ||
|
||
export async function unusedServerLayerAction2() { | ||
return 'unused-server-layer-action-2' | ||
} |
12 changes: 12 additions & 0 deletions
12
test/production/app-dir/actions-tree-shaking/reexport/app/named-reexport/server/page.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { sharedServerLayerAction } from './reexport-action' | ||
|
||
export default function Page() { | ||
return ( | ||
<div> | ||
<form> | ||
<input type="text" placeholder="input" /> | ||
<button formAction={sharedServerLayerAction}>submit</button> | ||
</form> | ||
</div> | ||
) | ||
} |
5 changes: 5 additions & 0 deletions
5
...uction/app-dir/actions-tree-shaking/reexport/app/named-reexport/server/reexport-action.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export { | ||
sharedServerLayerAction, | ||
unusedServerLayerAction1, | ||
unusedServerLayerAction2, | ||
} from './actions' |
13 changes: 13 additions & 0 deletions
13
...production/app-dir/actions-tree-shaking/reexport/app/namespace-reexport/client/actions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use server' | ||
|
||
export async function sharedClientLayerAction() { | ||
return 'shared-client-layer-action' | ||
} | ||
|
||
export async function unusedClientLayerAction1() { | ||
return 'unused-client-layer-action-1' | ||
} | ||
|
||
export async function unusedClientLayerAction2() { | ||
return 'unused-client-layer-action-2' | ||
} |
Oops, something went wrong.