[test] Demonstrate per-layer module resolution via package.json imports

Adds an e2e fixture under `test/e2e/app-dir/universal-module/` that
shows a single import identifier (`#universal`) resolving to different
files depending on the React layer of the importer, via a `react-server`
subpath-import condition:

- React Server layer: `./app/_universal/server.ts` (async, inline
  `'use cache'`)
- elsewhere: `./app/_universal/client.ts` (`'use client'`, async)

The page renders four reads — server component direct, server component
via an indirect shared re-export, client component direct, client
component via the same shared re-export — confirming the resolution is
driven by the importer's layer, including for universal helper modules
that get duplicated across layers.
parent defac920
'use client'
export async function getValue() {
return 'value from the CLIENT module'
}
import { cacheLife } from 'next/cache'
export async function getValue() {
'use cache'
cacheLife('hours')
return 'value from the SERVER module'
}
'use client'
import { use } from 'react'
import { getValue } from '#universal'
import { sharedGetValue } from './shared'
export function ClientView() {
return (
<section>
<h2>Client component</h2>
<p id="client-direct">direct: {use(getValue())}</p>
<p id="client-shared">via shared: {use(sharedGetValue())}</p>
</section>
)
}
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { getValue } from '#universal'
import { sharedGetValue } from './shared'
import { ClientView } from './client-view'
export default async function Page() {
return (
<main>
<section>
<h2>Server component</h2>
<p id="server-direct">direct: {await getValue()}</p>
<p id="server-shared">via shared: {await sharedGetValue()}</p>
</section>
<ClientView />
</main>
)
}
export { getValue as sharedGetValue } from '#universal'
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
cacheComponents: true,
}
module.exports = nextConfig
{
"name": "universal-module",
"private": true,
"imports": {
"#universal": {
"react-server": "./app/_universal/server.ts",
"default": "./app/_universal/client.ts"
}
}
}
import { nextTestSetup } from 'e2e-utils'
import packageJson from './package.json'
describe('universal-module', () => {
const { next } = nextTestSetup({
files: __dirname,
packageJson,
})
it('resolves #universal to the SERVER module in server components and the CLIENT module in client components', async () => {
const browser = await next.browser('/')
expect(await browser.elementById('server-direct').text()).toBe(
'direct: value from the SERVER module'
)
expect(await browser.elementById('server-shared').text()).toBe(
'via shared: value from the SERVER module'
)
expect(await browser.elementById('client-direct').text()).toBe(
'direct: value from the CLIENT module'
)
expect(await browser.elementById('client-shared').text()).toBe(
'via shared: value from the CLIENT module'
)
})
})
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