Skip to content

Commit 74225d1

Browse files
authored
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
1 parent bcb63f3 commit 74225d1

File tree

6 files changed

+29
-17
lines changed

6 files changed

+29
-17
lines changed

packages/next/src/build/webpack-config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,6 +1941,10 @@ export default async function getBaseWebpackConfig(
19411941
],
19421942
},
19431943
resolve: {
1944+
mainFields: isEdgeServer
1945+
? mainFieldsPerCompiler[COMPILER_NAMES.edgeServer]
1946+
: // Prefer module fields over main fields for isomorphic packages on server layer
1947+
['module', 'main'],
19441948
conditionNames: reactServerCondition,
19451949
alias: {
19461950
// If missing the alias override here, the default alias will be used which aliases

test/e2e/app-dir/app-external/app-external.test.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,24 +88,23 @@ createNextDescribe(
8888
})
8989

9090
it('should resolve 3rd party package exports based on the react-server condition', async () => {
91-
await next
92-
.fetch('/react-server/3rd-party-package')
93-
.then(async (response) => {
94-
const result = await resolveStreamResponse(response)
95-
96-
// Package should be resolved based on the react-server condition,
97-
// as well as package's internal & external dependencies.
98-
expect(result).toContain(
99-
'Server: index.react-server:react.subset:dep.server'
100-
)
101-
expect(result).toContain(
102-
'Client: index.default:react.full:dep.default'
103-
)
91+
const $ = await next.render$('/react-server/3rd-party-package')
92+
93+
const result = $('body').text()
94+
95+
// Package should be resolved based on the react-server condition,
96+
// as well as package's internal & external dependencies.
97+
expect(result).toContain(
98+
'Server: index.react-server:react.subset:dep.server'
99+
)
100+
expect(result).toContain('Client: index.default:react.full:dep.default')
101+
102+
// Subpath exports should be resolved based on the condition too.
103+
expect(result).toContain('Server subpath: subpath.react-server')
104+
expect(result).toContain('Client subpath: subpath.default')
104105

105-
// Subpath exports should be resolved based on the condition too.
106-
expect(result).toContain('Server subpath: subpath.react-server')
107-
expect(result).toContain('Client subpath: subpath.default')
108-
})
106+
// Prefer `module` field for isomorphic packages.
107+
expect($('#main-field').text()).toContain('server-module-field:module')
109108
})
110109

111110
it('should correctly collect global css imports and mark them as side effects', async () => {

test/e2e/app-dir/app-external/app/react-server/3rd-party-package/page.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import v from 'conditional-exports'
22
import v1 from 'conditional-exports/subpath'
3+
import { name as serverFieldName } from 'server-module-field'
34

45
import Client from './client'
56

@@ -11,6 +12,8 @@ export default function Page() {
1112
{`Server subpath: ${v1}`}
1213
<br />
1314
<Client />
15+
<br />
16+
<div id="main-field">{`Server module field: ${serverFieldName}`}</div>
1417
</div>
1518
)
1619
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.name = 'server-module-field:main'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const name = 'server-module-field:module'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"main": "./index.cjs",
3+
"module": "./index.esm.js"
4+
}

0 commit comments

Comments
 (0)