Skip to content

Commit

Permalink
Fix resolving for esm package with only exports.import condition (#42767
Browse files Browse the repository at this point in the history
)

Should be able to resolve `exports.import` condition for esm packages only when import them in server components

Fixes: #42534

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`
  • Loading branch information
huozhi authored Nov 11, 2022
1 parent cb8fdc6 commit 028b8d0
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 15 deletions.
6 changes: 2 additions & 4 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1641,7 +1641,7 @@ export default async function getBaseWebpackConfig(
return true
},
resolve: {
conditionNames: ['react-server', 'node', 'require'],
conditionNames: ['react-server', 'node', 'import', 'require'],
alias: {
// 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
Expand Down Expand Up @@ -1983,9 +1983,7 @@ export default async function getBaseWebpackConfig(
new ReactLoadablePlugin({
filename: REACT_LOADABLE_MANIFEST,
pagesDir,
runtimeAsset: true
? `server/${MIDDLEWARE_REACT_LOADABLE_MANIFEST}.js`
: undefined,
runtimeAsset: `server/${MIDDLEWARE_REACT_LOADABLE_MANIFEST}.js`,
dev,
}),
(isClient || isEdgeServer) && new DropClientPage(),
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/app-dir/app-external.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ describe('app dir - external dependency', () => {
containClientContent(browserClientText)

// support esm module imports on server side, and indirect imports from shared components
expect(serverHtml).toContain('random-module-instance')
expect(serverHtml).toContain('pure-esm-module')
expect(sharedHtml).toContain(
'node_modules instance from client module random-module-instance'
'node_modules instance from client module pure-esm-module'
)
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { name } from 'random-module-instance'
import { name } from 'pure-esm-module'

export default function Page() {
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'

import { name } from 'random-module-instance'
import { name } from 'pure-esm-module'

export default function () {
return `node_modules instance from client module ${name}`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const random = ~~(Math.random() * 1e5)
export const name = 'random-module-instance'
export const name = 'pure-esm-module'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "pure-esm-module",
"type": "module",
"exports": {
"import": "./index.js"
}
}

This file was deleted.

1 change: 0 additions & 1 deletion test/e2e/app-dir/rsc-basic/app/shared/page.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import ClientFromDirect from '../../components/client'
import ClientFromShared from '../../components/shared'
import SharedFromClient from '../../components/shared-client'
// import Random from '../../components/random-module-instance'
import Bar from '../../components/bar'

export default function Page() {
Expand Down

0 comments on commit 028b8d0

Please sign in to comment.