|
1 | | -// @ts-ignore avoid ts errors during manual testing |
2 | | -import { type NextInstance } from 'e2e-utils' |
| 1 | +import { nextTestSetup, type NextInstance } from 'e2e-utils' |
| 2 | + |
| 3 | +// This is from 'next/dist/build/webpack/plugins/flight-client-entry-plugin', but unfortunately |
| 4 | +// Typescript breaks when importing it directly. |
| 5 | +type Actions = { |
| 6 | + [actionId: string]: { |
| 7 | + exportedName?: string |
| 8 | + filename?: string |
| 9 | + workers: { |
| 10 | + [name: string]: { |
| 11 | + moduleId: string | number |
| 12 | + async: boolean |
| 13 | + } |
| 14 | + } |
| 15 | + layer: { |
| 16 | + [name: string]: string |
| 17 | + } |
| 18 | + } |
| 19 | +} |
3 | 20 |
|
4 | 21 | async function getActionsMappingByRuntime( |
5 | 22 | next: NextInstance, |
6 | 23 | runtime: 'node' | 'edge' |
7 | | -) { |
| 24 | +): Promise<Actions> { |
8 | 25 | const manifest = JSON.parse( |
9 | 26 | await next.readFile('.next/server/server-reference-manifest.json') |
10 | 27 | ) |
11 | 28 |
|
12 | 29 | return manifest[runtime] |
13 | 30 | } |
14 | 31 |
|
15 | | -export function markLayoutAsEdge(next: NextInstance) { |
16 | | - beforeAll(async () => { |
17 | | - await next.stop() |
18 | | - const layoutContent = await next.readFile('app/layout.js') |
19 | | - await next.patchFile( |
20 | | - 'app/layout.js', |
21 | | - layoutContent + `\nexport const runtime = 'edge'` |
22 | | - ) |
23 | | - await next.start() |
| 32 | +export function nextTestSetupActionTreeShaking(opts) { |
| 33 | + let result = nextTestSetup({ |
| 34 | + ...opts, |
| 35 | + skipStart: !!process.env.TEST_EDGE, |
24 | 36 | }) |
25 | | -} |
26 | 37 |
|
27 | | -/* |
28 | | -{ |
29 | | - [route path]: { [layer]: Set<workerId> ] |
30 | | -} |
31 | | -*/ |
32 | | -type ActionsMappingOfRuntime = { |
33 | | - [actionId: string]: { |
34 | | - workers: { |
35 | | - [route: string]: string |
36 | | - } |
37 | | - layer: { |
38 | | - [route: string]: string |
39 | | - } |
| 38 | + if (process.env.TEST_EDGE) { |
| 39 | + beforeAll(async () => { |
| 40 | + const layoutContent = await result.next.readFile('app/layout.js') |
| 41 | + await result.next.patchFile( |
| 42 | + 'app/layout.js', |
| 43 | + layoutContent + `\nexport const runtime = 'edge'` |
| 44 | + ) |
| 45 | + await result.next.start() |
| 46 | + }) |
40 | 47 | } |
| 48 | + |
| 49 | + return result |
41 | 50 | } |
| 51 | + |
42 | 52 | type ActionState = { |
43 | 53 | [route: string]: { |
44 | | - [layer: string]: number |
| 54 | + [layer: string]: string[] |
45 | 55 | } |
46 | 56 | } |
47 | 57 |
|
48 | | -function getActionsRoutesState( |
49 | | - actionsMappingOfRuntime: ActionsMappingOfRuntime |
50 | | -): ActionState { |
| 58 | +function getActionsRoutesState(actionsMappingOfRuntime: Actions): ActionState { |
51 | 59 | const state: ActionState = {} |
52 | | - Object.keys(actionsMappingOfRuntime).forEach((actionId) => { |
| 60 | + for (const actionId in actionsMappingOfRuntime) { |
53 | 61 | const action = actionsMappingOfRuntime[actionId] |
54 | | - const routePaths = Object.keys(action.workers) |
55 | | - |
56 | | - routePaths.forEach((routePath) => { |
| 62 | + for (const routePath in action.workers) { |
57 | 63 | if (!state[routePath]) { |
58 | 64 | state[routePath] = {} |
59 | 65 | } |
60 | 66 | const layer = action.layer[routePath] |
61 | 67 |
|
62 | 68 | if (!state[routePath][layer]) { |
63 | | - state[routePath][layer] = 0 |
| 69 | + state[routePath][layer] = [] |
64 | 70 | } |
65 | 71 |
|
66 | | - state[routePath][layer]++ |
67 | | - }) |
68 | | - }) |
| 72 | + // Normalize when NEXT_SKIP_ISOLATE=1 |
| 73 | + const filename = action.filename.startsWith('test/tmp/next-test-') |
| 74 | + ? action.filename.slice( |
| 75 | + action.filename.indexOf('/', 'test/tmp/next-test-'.length) + 1 |
| 76 | + ) |
| 77 | + : action.filename |
| 78 | + state[routePath][layer].push(`${filename}#${action.exportedName}`) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + for (const layer of Object.values(state)) { |
| 83 | + for (const list of Object.values(layer)) { |
| 84 | + list.sort() |
| 85 | + } |
| 86 | + } |
69 | 87 |
|
70 | 88 | return state |
71 | 89 | } |
|
0 commit comments