Unverified Commit 74225d19 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Prefer module fields for RSC server layer (#51179)

Follow up for #50548 

There're some packages like `aws-sdk-js-v3` which doesn't have a exports field yet: 

```
  "main": "./dist-cjs/index.js",
  "types": "./dist-types/index.d.ts",
  "module": "./dist-es/index.js",
 ```
 
 This PR let u pick up by the js assets based on main fields, will prefer `"module"` field instead of `"main"`
 
 Closes NEXT-1286
parent bcb63f32
...@@ -1941,6 +1941,10 @@ export default async function getBaseWebpackConfig( ...@@ -1941,6 +1941,10 @@ export default async function getBaseWebpackConfig(
], ],
}, },
resolve: { resolve: {
mainFields: isEdgeServer
? mainFieldsPerCompiler[COMPILER_NAMES.edgeServer]
: // Prefer module fields over main fields for isomorphic packages on server layer
['module', 'main'],
conditionNames: reactServerCondition, conditionNames: reactServerCondition,
alias: { alias: {
// 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
......
...@@ -88,24 +88,23 @@ createNextDescribe( ...@@ -88,24 +88,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. // Prefer `module` field for isomorphic packages.
expect(result).toContain('Server subpath: subpath.react-server') expect($('#main-field').text()).toContain('server-module-field:module')
expect(result).toContain('Client subpath: subpath.default')
})
}) })
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>
) )
} }
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