Unverified Commit a6d4c77f authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Prefer module over main on main fields for app router server compiler (#56960)

Reverts vercel/next.js#56766
Reland vercel/next.js#56532 

x-ref: https://vercel.slack.com/archives/C04DUD7EB1B/p1697576563771229?thread_ts=1697146531.305779&cid=C04DUD7EB1B

This change was breaking package which was actually a dual package but marked as esm module, and when the package is ESM but import a CJS dependency without fully spepcified file path it will fail to resolve. The way is to either remove the esmodule type in package.json or change the import to full path import with extension for the package
parent a3e56c9c
...@@ -44,7 +44,6 @@ export async function resolveExternal( ...@@ -44,7 +44,6 @@ export async function resolveExternal(
context: string, context: string,
request: string, request: string,
isEsmRequested: boolean, isEsmRequested: boolean,
hasAppDir: boolean,
getResolve: ( getResolve: (
options: any options: any
) => ( ) => (
...@@ -66,11 +65,7 @@ export async function resolveExternal( ...@@ -66,11 +65,7 @@ export async function resolveExternal(
let preferEsmOptions = let preferEsmOptions =
esmExternals && isEsmRequested ? [true, false] : [false] esmExternals && isEsmRequested ? [true, false] : [false]
// Disable esm resolving for app/ and pages/ so for esm package using under pages/
// won't load react through esm loader
if (hasAppDir) {
preferEsmOptions = [false]
}
for (const preferEsm of preferEsmOptions) { for (const preferEsm of preferEsmOptions) {
const resolve = getResolve( const resolve = getResolve(
preferEsm ? esmResolveOptions : nodeResolveOptions preferEsm ? esmResolveOptions : nodeResolveOptions
...@@ -135,12 +130,10 @@ export function makeExternalHandler({ ...@@ -135,12 +130,10 @@ export function makeExternalHandler({
config, config,
optOutBundlingPackageRegex, optOutBundlingPackageRegex,
dir, dir,
hasAppDir,
}: { }: {
config: NextConfigComplete config: NextConfigComplete
optOutBundlingPackageRegex: RegExp optOutBundlingPackageRegex: RegExp
dir: string dir: string
hasAppDir: boolean
}) { }) {
let resolvedExternalPackageDirs: Map<string, string> let resolvedExternalPackageDirs: Map<string, string>
const looseEsmExternals = config.experimental?.esmExternals === 'loose' const looseEsmExternals = config.experimental?.esmExternals === 'loose'
...@@ -293,7 +286,6 @@ export function makeExternalHandler({ ...@@ -293,7 +286,6 @@ export function makeExternalHandler({
context, context,
request, request,
isEsmRequested, isEsmRequested,
hasAppDir,
getResolve, getResolve,
isLocal ? resolveNextExternal : undefined isLocal ? resolveNextExternal : undefined
) )
...@@ -353,7 +345,6 @@ export function makeExternalHandler({ ...@@ -353,7 +345,6 @@ export function makeExternalHandler({
config.experimental.esmExternals, config.experimental.esmExternals,
context, context,
pkg + '/package.json', pkg + '/package.json',
hasAppDir,
isEsmRequested, isEsmRequested,
getResolve, getResolve,
isLocal ? resolveNextExternal : undefined isLocal ? resolveNextExternal : undefined
......
import {
COMPILER_NAMES,
type CompilerNameValues,
} from '../../shared/lib/constants'
// exports.<conditionName>
export const edgeConditionNames = [
'edge-light',
'worker',
// inherits the default conditions
'...',
]
const mainFieldsPerCompiler: Record<
CompilerNameValues | 'app-router-server',
string[]
> = {
// For default case, prefer CJS over ESM on server side. e.g. pages dir SSR
[COMPILER_NAMES.server]: ['main', 'module'],
[COMPILER_NAMES.client]: ['browser', 'module', 'main'],
[COMPILER_NAMES.edgeServer]: edgeConditionNames,
// For app router since everything is bundled, prefer ESM over CJS
'app-router-server': ['module', 'main'],
}
export function getMainField(
pageType: 'app' | 'pages',
compilerType: CompilerNameValues
) {
if (compilerType === COMPILER_NAMES.edgeServer) {
return edgeConditionNames
} else if (compilerType === COMPILER_NAMES.client) {
return mainFieldsPerCompiler[COMPILER_NAMES.client]
}
// Prefer module fields over main fields for isomorphic packages on server layer
return pageType === 'app'
? mainFieldsPerCompiler['app-router-server']
: mainFieldsPerCompiler[COMPILER_NAMES.server]
}
...@@ -74,6 +74,10 @@ import { needsExperimentalReact } from '../lib/needs-experimental-react' ...@@ -74,6 +74,10 @@ import { needsExperimentalReact } from '../lib/needs-experimental-react'
import { getDefineEnvPlugin } from './webpack/plugins/define-env-plugin' import { getDefineEnvPlugin } from './webpack/plugins/define-env-plugin'
import type { SWCLoaderOptions } from './webpack/loaders/next-swc-loader' import type { SWCLoaderOptions } from './webpack/loaders/next-swc-loader'
import { isResourceInPackages, makeExternalHandler } from './handle-externals' import { isResourceInPackages, makeExternalHandler } from './handle-externals'
import {
getMainField,
edgeConditionNames,
} from './webpack-config-rules/resolve'
type ExcludesFalse = <T>(x: T | false) => x is T type ExcludesFalse = <T>(x: T | false) => x is T
type ClientEntries = { type ClientEntries = {
...@@ -104,21 +108,6 @@ const babelIncludeRegexes: RegExp[] = [ ...@@ -104,21 +108,6 @@ const babelIncludeRegexes: RegExp[] = [
const asyncStoragesRegex = const asyncStoragesRegex =
/next[\\/]dist[\\/](esm[\\/])?client[\\/]components[\\/](static-generation-async-storage|action-async-storage|request-async-storage)/ /next[\\/]dist[\\/](esm[\\/])?client[\\/]components[\\/](static-generation-async-storage|action-async-storage|request-async-storage)/
// exports.<conditionName>
const edgeConditionNames = [
'edge-light',
'worker',
// inherits the default conditions
'...',
]
// packageJson.<mainField>
const mainFieldsPerCompiler: Record<CompilerNameValues, string[]> = {
[COMPILER_NAMES.server]: ['main', 'module'],
[COMPILER_NAMES.client]: ['browser', 'module', 'main'],
[COMPILER_NAMES.edgeServer]: edgeConditionNames,
}
// Support for NODE_PATH // Support for NODE_PATH
const nodePathList = (process.env.NODE_PATH || '') const nodePathList = (process.env.NODE_PATH || '')
.split(process.platform === 'win32' ? ';' : ':') .split(process.platform === 'win32' ? ';' : ':')
...@@ -936,7 +925,8 @@ export default async function getBaseWebpackConfig( ...@@ -936,7 +925,8 @@ export default async function getBaseWebpackConfig(
}, },
} }
: undefined), : undefined),
mainFields: mainFieldsPerCompiler[compilerType], // default main fields use pages dir ones, and customize app router ones in loaders.
mainFields: getMainField('pages', compilerType),
...(isEdgeServer && { ...(isEdgeServer && {
conditionNames: edgeConditionNames, conditionNames: edgeConditionNames,
}), }),
...@@ -1044,7 +1034,6 @@ export default async function getBaseWebpackConfig( ...@@ -1044,7 +1034,6 @@ export default async function getBaseWebpackConfig(
config, config,
optOutBundlingPackageRegex, optOutBundlingPackageRegex,
dir, dir,
hasAppDir,
}) })
const shouldIncludeExternalDirs = const shouldIncludeExternalDirs =
...@@ -1615,6 +1604,7 @@ export default async function getBaseWebpackConfig( ...@@ -1615,6 +1604,7 @@ export default async function getBaseWebpackConfig(
], ],
}, },
resolve: { resolve: {
mainFields: getMainField('app', compilerType),
conditionNames: reactServerCondition, conditionNames: reactServerCondition,
// If missing the alias override here, the default alias will be used which aliases // If missing the alias override here, the default alias will be used which aliases
// react to the direct file path, not the package name. In that case the condition // react to the direct file path, not the package name. In that case the condition
...@@ -1759,6 +1749,9 @@ export default async function getBaseWebpackConfig( ...@@ -1759,6 +1749,9 @@ export default async function getBaseWebpackConfig(
], ],
exclude: [codeCondition.exclude], exclude: [codeCondition.exclude],
use: swcLoaderForClientLayer, use: swcLoaderForClientLayer,
resolve: {
mainFields: getMainField('app', compilerType),
},
}, },
] ]
: []), : []),
......
...@@ -743,7 +743,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { ...@@ -743,7 +743,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
context, context,
request, request,
isEsmRequested, isEsmRequested,
!!this.appDirEnabled,
(options) => (_: string, resRequest: string) => { (options) => (_: string, resRequest: string) => {
return getResolve(options)(parent, resRequest, job) return getResolve(options)(parent, resRequest, job)
}, },
......
...@@ -39,7 +39,7 @@ createNextDescribe( ...@@ -39,7 +39,7 @@ createNextDescribe(
buildCommand: 'yarn build', buildCommand: 'yarn build',
skipDeployment: true, skipDeployment: true,
}, },
({ next, isNextDev }) => { ({ next }) => {
it('should be able to opt-out 3rd party packages being bundled in server components', async () => { it('should be able to opt-out 3rd party packages being bundled in server components', async () => {
await next.fetch('/react-server/optout').then(async (response) => { await next.fetch('/react-server/optout').then(async (response) => {
const result = await resolveStreamResponse(response) const result = await resolveStreamResponse(response)
...@@ -47,6 +47,7 @@ createNextDescribe( ...@@ -47,6 +47,7 @@ createNextDescribe(
expect(result).toContain('Server subpath: subpath.default') expect(result).toContain('Server subpath: subpath.default')
expect(result).toContain('Client: index.default') expect(result).toContain('Client: index.default')
expect(result).toContain('Client subpath: subpath.default') expect(result).toContain('Client subpath: subpath.default')
expect(result).toContain('opt-out-react-version: 18.2.0')
}) })
}) })
...@@ -91,24 +92,23 @@ createNextDescribe( ...@@ -91,24 +92,23 @@ createNextDescribe(
}) })
it('should resolve 3rd party package exports based on the react-server condition', async () => { it('should resolve 3rd party package exports based on the react-server condition', async () => {
await next const $ = await next.render$('/react-server/3rd-party-package')
.fetch('/react-server/3rd-party-package')
.then(async (response) => { const result = $('body').text()
const result = await resolveStreamResponse(response)
// Package should be resolved based on the react-server condition,
// Package should be resolved based on the react-server condition, // as well as package's internal & external dependencies.
// as well as package's internal & external dependencies. expect(result).toContain(
expect(result).toContain( 'Server: index.react-server:react.subset:dep.server'
'Server: index.react-server:react.subset:dep.server' )
) expect(result).toContain('Client: index.default:react.full:dep.default')
expect(result).toContain(
'Client: index.default:react.full:dep.default' // Subpath exports should be resolved based on the condition too.
) expect(result).toContain('Server subpath: subpath.react-server')
expect(result).toContain('Client subpath: subpath.default')
// Subpath exports should be resolved based on the condition too.
expect(result).toContain('Server subpath: subpath.react-server') // Prefer `module` field for isomorphic packages.
expect(result).toContain('Client subpath: subpath.default') expect($('#main-field').text()).toContain('server-module-field:module')
})
}) })
it('should correctly collect global css imports and mark them as side effects', async () => { it('should correctly collect global css imports and mark them as side effects', async () => {
......
import v from 'conditional-exports' import v from 'conditional-exports'
import v1 from 'conditional-exports/subpath' import v1 from 'conditional-exports/subpath'
import { name as serverFieldName } from 'server-module-field'
import Client from './client' import Client from './client'
...@@ -11,6 +12,8 @@ export default function Page() { ...@@ -11,6 +12,8 @@ export default function Page() {
{`Server subpath: ${v1}`} {`Server subpath: ${v1}`}
<br /> <br />
<Client /> <Client />
<br />
<div id="main-field">{`Server module field: ${serverFieldName}`}</div>
</div> </div>
) )
} }
import v from 'conditional-exports-optout' import v from 'conditional-exports-optout'
import v1 from 'conditional-exports-optout/subpath' import v1 from 'conditional-exports-optout/subpath'
import { getReactVersion } from 'conditional-exports-optout/react'
import Client from './client' import Client from './client'
...@@ -11,6 +12,9 @@ export default function Page() { ...@@ -11,6 +12,9 @@ export default function Page() {
{`Server subpath: ${v1}`} {`Server subpath: ${v1}`}
<br /> <br />
<Client /> <Client />
<p id="optout-react-version">
{`opt-out-react-version: ${getReactVersion()}`}
</p>
</div> </div>
) )
} }
...@@ -10,6 +10,9 @@ ...@@ -10,6 +10,9 @@
"react-server": "./subpath.server.js", "react-server": "./subpath.server.js",
"default": "./subpath.js" "default": "./subpath.js"
}, },
"./react": {
"import": "./react.js"
},
"./package.json": "./package.json" "./package.json": "./package.json"
} }
} }
import React from 'react'
export function getReactVersion() {
return React.version
}
...@@ -16,6 +16,9 @@ ...@@ -16,6 +16,9 @@
"react-server": "./subpath.server.js", "react-server": "./subpath.server.js",
"default": "./subpath.js" "default": "./subpath.js"
}, },
"./react": {
"import": "./react.js"
},
"./package.json": "./package.json" "./package.json": "./package.json"
} }
} }
import React from 'react'
export function getReactVersion() {
return React.version
}
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