Skip to content

Commit b86ae3f

Browse files
committed
Prevent exports of nextFlightActionEntryLoader from getting DCE'd
1 parent d8ab0f1 commit b86ae3f

File tree

7 files changed

+27
-13
lines changed

7 files changed

+27
-13
lines changed

packages/next/src/build/webpack/loaders/next-flight-action-entry-loader.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,13 @@ function nextFlightActionEntryLoader(this: any) {
2020
.flat()
2121

2222
return `
23-
${individualActions
24-
.map(([_, path]) => {
25-
// This import ensures that the module is always bundled even if there's no
26-
// explicit import in the codebase, to avoid the action being DCE'd.
27-
return `import(/* webpackMode: "eager" */ ${JSON.stringify(path)});`
28-
})
29-
.join('\n')}
30-
3123
${individualActions
3224
.map(([id, path, name]) => {
3325
// Re-export the same functions from the original module path as action IDs.
34-
return `export { ${name} as "${id}" } from ${JSON.stringify(path)}`
26+
return `
27+
const { ${name}: _${id} } = await import(${JSON.stringify(path)});
28+
export { _${id} as "${id}" };
29+
`
3530
})
3631
.join('\n')}
3732
`

packages/next/src/build/webpack/plugins/flight-client-entry-plugin.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,13 @@ export class FlightClientEntryPlugin {
928928
}
929929

930930
compilation.hooks.succeedEntry.call(dependency, options, module)
931+
932+
compilation.moduleGraph
933+
.getExportsInfo(module)
934+
.setUsedInUnknownWay(
935+
this.isEdgeServer ? EDGE_RUNTIME_WEBPACK : DEFAULT_RUNTIME_WEBPACK
936+
)
937+
931938
return resolve(module)
932939
}
933940
)

packages/next/src/compiled/react-server-dom-webpack/cjs/react-server-dom-webpack-client.edge.production.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ function resolveServerReference(bundlerConfig, id) {
4848
'" in the React Server Manifest. This is probably a bug in the React Server Components bundler.'
4949
);
5050
}
51-
return [resolvedModuleData.id, resolvedModuleData.chunks, name];
51+
return resolvedModuleData.async
52+
? [resolvedModuleData.id, resolvedModuleData.chunks, name, 1]
53+
: [resolvedModuleData.id, resolvedModuleData.chunks, name];
5254
}
5355
var chunkCache = new Map();
5456
function requireAsyncModule(id) {

packages/next/src/compiled/react-server-dom-webpack/cjs/react-server-dom-webpack-server.edge.production.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1947,7 +1947,9 @@ function resolveServerReference(bundlerConfig, id) {
19471947
'" in the React Server Manifest. This is probably a bug in the React Server Components bundler.'
19481948
);
19491949
}
1950-
return [resolvedModuleData.id, resolvedModuleData.chunks, name];
1950+
return resolvedModuleData.async
1951+
? [resolvedModuleData.id, resolvedModuleData.chunks, name, 1]
1952+
: [resolvedModuleData.id, resolvedModuleData.chunks, name];
19511953
}
19521954
var chunkCache = new Map();
19531955
function requireAsyncModule(id) {

packages/next/src/compiled/react-server-dom-webpack/cjs/react-server-dom-webpack-server.node.development.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2472,7 +2472,9 @@
24722472
'" in the React Server Manifest. This is probably a bug in the React Server Components bundler.'
24732473
);
24742474
}
2475-
return [resolvedModuleData.id, resolvedModuleData.chunks, name];
2475+
return resolvedModuleData.async
2476+
? [resolvedModuleData.id, resolvedModuleData.chunks, name, 1]
2477+
: [resolvedModuleData.id, resolvedModuleData.chunks, name];
24762478
}
24772479
function requireAsyncModule(id) {
24782480
var promise = globalThis.__next_require__(id);

packages/next/src/compiled/react-server-dom-webpack/cjs/react-server-dom-webpack-server.node.production.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1983,7 +1983,9 @@ function resolveServerReference(bundlerConfig, id) {
19831983
'" in the React Server Manifest. This is probably a bug in the React Server Components bundler.'
19841984
);
19851985
}
1986-
return [resolvedModuleData.id, resolvedModuleData.chunks, name];
1986+
return resolvedModuleData.async
1987+
? [resolvedModuleData.id, resolvedModuleData.chunks, name, 1]
1988+
: [resolvedModuleData.id, resolvedModuleData.chunks, name];
19871989
}
19881990
var chunkCache = new Map();
19891991
function requireAsyncModule(id) {

packages/next/src/server/app-render/action-utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export function createServerModuleMap({
2424
][id].workers[normalizeWorkerPageName(pageName)],
2525
name: id,
2626
chunks: [],
27+
// Because of how next-flight-action-entry-loader creates the action
28+
// entry modules, we know that these become async. So we set the flag
29+
// accordingly.
30+
async: true,
2731
}
2832
},
2933
}

0 commit comments

Comments
 (0)