Commit 9e53eb1c authored by Tobias Koppers's avatar Tobias Koppers

Fix server-side bundling to not use the "module" export condition

The "module" export condition is intended for browser bundlers (like
Rollup/Vite) that support ESM tree-shaking. It must NOT be applied to
server-side bundling because server code may re-export to CommonJS
consumers or has different module semantics.

- Webpack: override `byDependency.esm.conditionNames` for the node
  server to explicitly exclude "module". Using '...' would still expand
  to byDependency.esm defaults which include "module", so an explicit
  list ['import', 'webpack', 'production'/'development', 'node'] is used.
- Turbopack: set `module: false` in `get_server_resolve_options_context`.
  The field was incorrectly set to `true` for all server contexts.

Adds a test fixture and e2e test to prevent regressions.
Co-Authored-By: 's avatarClaude <noreply@anthropic.com>
parent aa3ba7ed
...@@ -283,7 +283,9 @@ pub async fn get_server_resolve_options_context( ...@@ -283,7 +283,9 @@ pub async fn get_server_resolve_options_context(
enable_node_modules: Some(root_dir.clone()), enable_node_modules: Some(root_dir.clone()),
enable_node_externals: true, enable_node_externals: true,
enable_node_native_modules: true, enable_node_native_modules: true,
module: true, // The "module" condition is intended for browser bundlers that support ESM
// tree-shaking. It must NOT be used for server-side bundling.
module: false,
custom_conditions, custom_conditions,
import_map: Some(next_server_import_map), import_map: Some(next_server_import_map),
fallback_import_map: Some(next_server_fallback_import_map), fallback_import_map: Some(next_server_fallback_import_map),
......
...@@ -794,6 +794,26 @@ export default async function getBaseWebpackConfig( ...@@ -794,6 +794,26 @@ export default async function getBaseWebpackConfig(
: undefined), : undefined),
// default main fields use pages dir ones, and customize app router ones in loaders. // default main fields use pages dir ones, and customize app router ones in loaders.
mainFields: getMainField(compilerType, false), mainFields: getMainField(compilerType, false),
// On the node server, override byDependency.esm to not include the "module"
// condition. The "module" condition is intended for browser bundlers that
// support ESM tree-shaking and must not be applied to server-side bundling.
// Using '...' would expand to byDependency.esm defaults which include "module",
// so we must provide an explicit list. This mirrors webpack's byDependency.esm
// defaults for a node target but without "module".
...(isNodeServer
? {
byDependency: {
esm: {
conditionNames: [
'import',
'webpack',
dev ? 'development' : 'production',
'node',
],
},
},
}
: {}),
plugins: [ plugins: [
isNodeServer ? new OptionalPeerDependencyResolverPlugin() : undefined, isNodeServer ? new OptionalPeerDependencyResolverPlugin() : undefined,
].filter(Boolean) as webpack.ResolvePluginInstance[], ].filter(Boolean) as webpack.ResolvePluginInstance[],
......
import * as lib from 'library-with-module-condition'
export const runtime = 'nodejs'
export function GET() {
return Response.json({ source: lib.source })
}
...@@ -5,9 +5,11 @@ describe('react version', () => { ...@@ -5,9 +5,11 @@ describe('react version', () => {
? // `link` is incompatible with the npm version used when this test is deployed ? // `link` is incompatible with the npm version used when this test is deployed
{ {
'library-with-exports': 'file:./library-with-exports', 'library-with-exports': 'file:./library-with-exports',
'library-with-module-condition': 'file:./library-with-module-condition',
} }
: { : {
'library-with-exports': 'link:./library-with-exports', 'library-with-exports': 'link:./library-with-exports',
'library-with-module-condition': 'link:./library-with-module-condition',
} }
const { next } = nextTestSetup({ const { next } = nextTestSetup({
...@@ -284,4 +286,13 @@ describe('react version', () => { ...@@ -284,4 +286,13 @@ describe('react version', () => {
}, },
}) })
}) })
it('App Router Route Handler with nodejs runtime does not use "module" condition on server', async () => {
const response = await next.fetch('/module-condition-node-route')
const data = await response.json()
// The "module" export condition is intended for browser bundlers that support
// ESM tree-shaking. It must NOT be applied to server-side bundling.
// Both webpack and Turbopack must resolve "import" -> cjs-wrapper.mjs -> source = 'cjs'
expect(data).toEqual({ source: 'cjs' })
})
}) })
// ESM wrapper that re-exports the CommonJS implementation.
// This is the correct entry point for server-side ESM imports via the "import" condition.
export { source } from './cjs.cjs'
// This file is only meant to be used by browser bundlers via the "module" export condition.
// Server-side bundlers must NOT select this via "module" condition.
export const source = 'module-condition'
{
"name": "library-with-module-condition",
"version": "1.0.0",
"exports": {
".": {
"module": "./module-esm.mjs",
"import": "./cjs-wrapper.mjs",
"require": "./cjs.cjs",
"default": "./cjs.cjs"
}
}
}
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
"private": true, "private": true,
"name": "import-conditions", "name": "import-conditions",
"dependencies": { "dependencies": {
"library-with-exports": "link:./library-with-exports" "library-with-exports": "link:./library-with-exports",
"library-with-module-condition": "link:./library-with-module-condition"
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment