Unverified Commit 51d7153e authored by Shu Ding's avatar Shu Ding Committed by GitHub

Add test case for module resolution in node_modules from different contexts (#35978)

The same module should be created as 2 instances if it's imported from different contexts (server or client).

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
parent 345f5cc3
...@@ -87,9 +87,11 @@ async function parseModuleInfo( ...@@ -87,9 +87,11 @@ async function parseModuleInfo(
} = node } = node
// exports.xxx = xxx // exports.xxx = xxx
if ( if (
left &&
left.object &&
left.type === 'MemberExpression' && left.type === 'MemberExpression' &&
left?.object.type === 'Identifier' && left.object.type === 'Identifier' &&
left.object?.value === 'exports' left.object.value === 'exports'
) { ) {
addExportNames(names, left.property) addExportNames(names, left.property)
} }
......
import { random } from 'random-module-instance'
export default function () {
return `node_modules instance from .client.js:${random}`
}
import ClientFromDirect from '../components/client.client' import ClientFromDirect from '../components/client.client'
import ClientFromShared from '../components/shared' import ClientFromShared from '../components/shared'
import SharedFromClient from '../components/shared.client' import SharedFromClient from '../components/shared.client'
import Random from '../components/random-module-instance.client'
import { random } from 'random-module-instance'
export default function Page() { export default function Page() {
// All three client components should be rendered correctly, but only // All three client components should be rendered correctly, but only
// shared component is a server component, and another is a client component. // shared component is a server component, and another is a client component.
// These two shared components should be created as two module instances. // These two shared components should be created as two module instances.
// It's expected to have hydration mismatch here.
return ( return (
<div id="main"> <div id="main" suppressHydrationWarning>
<Random />
<br />
{`node_modules instance from .server.js:` + random}
<br />
<ClientFromDirect /> <ClientFromDirect />
<br /> <br />
<ClientFromShared /> <ClientFromShared />
......
...@@ -64,6 +64,15 @@ export default function (context, { runtime, env }) { ...@@ -64,6 +64,15 @@ export default function (context, { runtime, env }) {
expect(sharedServerModule[0][1]).toBe(sharedServerModule[1][1]) expect(sharedServerModule[0][1]).toBe(sharedServerModule[1][1])
expect(sharedClientModule[0][1]).toBe(sharedClientModule[1][1]) expect(sharedClientModule[0][1]).toBe(sharedClientModule[1][1])
expect(sharedServerModule[0][1]).not.toBe(sharedClientModule[0][1]) expect(sharedServerModule[0][1]).not.toBe(sharedClientModule[0][1])
// Should import 2 module instances for node_modules too.
const modFromClient = main.match(
/node_modules instance from \.client\.js:(\d+)/
)
const modFromServer = main.match(
/node_modules instance from \.server\.js:(\d+)/
)
expect(modFromClient[1]).not.toBe(modFromServer[1])
}) })
it('should support next/link in server components', async () => { it('should support next/link in server components', async () => {
......
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