From 99baaf68f765f940ab604a24d04beb2969240fac Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 28 May 2024 23:06:45 +0200 Subject: [PATCH 01/12] Transform client reference in middleware layer --- packages/next/src/build/webpack-config.ts | 2 ++ test/e2e/module-layer/instrumentation.js | 7 +++++++ test/e2e/module-layer/lib/mixed-lib/shared-module.js | 4 ++++ test/e2e/module-layer/middleware.js | 4 +++- test/e2e/module-layer/module-layer.test.ts | 3 ++- test/e2e/module-layer/next.config.js | 5 +++++ 6 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 test/e2e/module-layer/instrumentation.js create mode 100644 test/e2e/module-layer/lib/mixed-lib/shared-module.js create mode 100644 test/e2e/module-layer/next.config.js diff --git a/packages/next/src/build/webpack-config.ts b/packages/next/src/build/webpack-config.ts index 4b1b92e4992ec..2032bba9bacde 100644 --- a/packages/next/src/build/webpack-config.ts +++ b/packages/next/src/build/webpack-config.ts @@ -529,6 +529,7 @@ export default async function getBaseWebpackConfig( : [] const instrumentLayerLoaders = [ + 'next-flight-loader', // When using Babel, we will have to add the SWC loader // as an additional pass to handle RSC correctly. // This will cause some performance overhead but @@ -538,6 +539,7 @@ export default async function getBaseWebpackConfig( ].filter(Boolean) const middlewareLayerLoaders = [ + 'next-flight-loader', // When using Babel, we will have to use SWC to do the optimization // for middleware to tree shake the unused default optimized imports like "next/server". // This will cause some performance overhead but diff --git a/test/e2e/module-layer/instrumentation.js b/test/e2e/module-layer/instrumentation.js new file mode 100644 index 0000000000000..e746bc7104bd4 --- /dev/null +++ b/test/e2e/module-layer/instrumentation.js @@ -0,0 +1,7 @@ +import { textValue } from './lib/mixed-lib/shared-module' + +export async function register() { + if (typeof textValue !== 'string') { + throw new Error('textValue is not a string') + } +} diff --git a/test/e2e/module-layer/lib/mixed-lib/shared-module.js b/test/e2e/module-layer/lib/mixed-lib/shared-module.js new file mode 100644 index 0000000000000..89966a446a748 --- /dev/null +++ b/test/e2e/module-layer/lib/mixed-lib/shared-module.js @@ -0,0 +1,4 @@ +import Link from 'next/link' + +export const textValue = 'text-value' +export const TestLink = Link diff --git a/test/e2e/module-layer/middleware.js b/test/e2e/module-layer/middleware.js index cd3fc531ccbe4..0a1dd469a2312 100644 --- a/test/e2e/module-layer/middleware.js +++ b/test/e2e/module-layer/middleware.js @@ -1,6 +1,7 @@ import 'server-only' import * as React from 'react' import { NextResponse } from 'next/server' +import { textValue } from './lib/mixed-lib/shared-module' // import './lib/mixed-lib' export function middleware(request) { @@ -10,9 +11,10 @@ export function middleware(request) { throw new Error('React.useState should not be defined in server layer') } - if (request.nextUrl.pathname === '/react-version') { + if (request.nextUrl.pathname === '/middleware') { return Response.json({ React: Object.keys(ReactObject), + textValue, }) } diff --git a/test/e2e/module-layer/module-layer.test.ts b/test/e2e/module-layer/module-layer.test.ts index 7d90ea9fef659..d6a2eda208346 100644 --- a/test/e2e/module-layer/module-layer.test.ts +++ b/test/e2e/module-layer/module-layer.test.ts @@ -37,9 +37,10 @@ describe('module layer', () => { } it('should render installed react-server condition for middleware', async () => { - const json = await next.fetch('/react-version').then((res) => res.json()) + const json = await next.fetch('/middleware').then((res) => res.json()) expect(json.React).toContain('version') // basic react-server export expect(json.React).not.toContain('useEffect') // no client api export + expect(json.textValue).toBe('text-value') }) // This is for backward compatibility, don't change react usage in existing pages/api diff --git a/test/e2e/module-layer/next.config.js b/test/e2e/module-layer/next.config.js new file mode 100644 index 0000000000000..c4cf84a76553b --- /dev/null +++ b/test/e2e/module-layer/next.config.js @@ -0,0 +1,5 @@ +module.exports = { + experimental: { + instrumentationHook: true, + }, +} From 687dbbb5a3dad9a98874593a40334fe110e42aa7 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 28 May 2024 23:21:15 +0200 Subject: [PATCH 02/12] update alias --- .../next/src/build/create-compiler-aliases.ts | 15 +++++++++++---- packages/next/src/build/webpack-config.ts | 5 +++++ test/e2e/module-layer/instrumentation.js | 1 + test/e2e/module-layer/module-layer.test.ts | 5 +++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/packages/next/src/build/create-compiler-aliases.ts b/packages/next/src/build/create-compiler-aliases.ts index 283041e286189..381864f5e40e8 100644 --- a/packages/next/src/build/create-compiler-aliases.ts +++ b/packages/next/src/build/create-compiler-aliases.ts @@ -235,6 +235,16 @@ export function createAppRouterApiAliases(isServerOnlyLayer: boolean) { return aliasMap } +export function createRSCRendererAliases(bundledReactChannel: string) { + return { + // react-server-dom-webpack alias + 'react-server-dom-webpack/client$': `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/client`, + 'react-server-dom-webpack/client.edge$': `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/client.edge`, + 'react-server-dom-webpack/server.edge$': `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/server.edge`, + 'react-server-dom-webpack/server.node$': `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/server.node`, + } +} + export function createRSCAliases( bundledReactChannel: string, { @@ -262,10 +272,7 @@ export function createRSCAliases( 'react-dom/server.edge$': `next/dist/compiled/react-dom${bundledReactChannel}/server.edge`, 'react-dom/server.browser$': `next/dist/compiled/react-dom${bundledReactChannel}/server.browser`, // react-server-dom-webpack alias - 'react-server-dom-webpack/client$': `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/client`, - 'react-server-dom-webpack/client.edge$': `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/client.edge`, - 'react-server-dom-webpack/server.edge$': `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/server.edge`, - 'react-server-dom-webpack/server.node$': `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/server.node`, + ...createRSCRendererAliases(bundledReactChannel), } if (!isEdgeServer) { diff --git a/packages/next/src/build/webpack-config.ts b/packages/next/src/build/webpack-config.ts index 2032bba9bacde..53515a3f283ee 100644 --- a/packages/next/src/build/webpack-config.ts +++ b/packages/next/src/build/webpack-config.ts @@ -82,6 +82,7 @@ import { createRSCAliases, createNextApiEsmAliases, createAppRouterApiAliases, + createRSCRendererAliases, } from './create-compiler-aliases' import { hasCustomExportOutput } from '../export/utils' import { CssChunkingPlugin } from './webpack/plugins/css-chunking-plugin' @@ -1472,6 +1473,8 @@ export default async function getBaseWebpackConfig( use: middlewareLayerLoaders, resolve: { conditionNames: reactServerCondition, + // Always use default channels when use installed react + alias: createRSCRendererAliases(''), }, }, { @@ -1480,6 +1483,8 @@ export default async function getBaseWebpackConfig( use: instrumentLayerLoaders, resolve: { conditionNames: reactServerCondition, + // Always use default channels when use installed react + alias: createRSCRendererAliases(''), }, }, ...(hasAppDir diff --git a/test/e2e/module-layer/instrumentation.js b/test/e2e/module-layer/instrumentation.js index e746bc7104bd4..6decffb39aca0 100644 --- a/test/e2e/module-layer/instrumentation.js +++ b/test/e2e/module-layer/instrumentation.js @@ -4,4 +4,5 @@ export async function register() { if (typeof textValue !== 'string') { throw new Error('textValue is not a string') } + console.log('instrumentation:register') } diff --git a/test/e2e/module-layer/module-layer.test.ts b/test/e2e/module-layer/module-layer.test.ts index d6a2eda208346..bc101e99dafb4 100644 --- a/test/e2e/module-layer/module-layer.test.ts +++ b/test/e2e/module-layer/module-layer.test.ts @@ -43,6 +43,11 @@ describe('module layer', () => { expect(json.textValue).toBe('text-value') }) + it('should call instrumentation hook without errors', async () => { + const output = next.cliOutput + expect(output).toContain('instrumentation:register') + }) + // This is for backward compatibility, don't change react usage in existing pages/api it('should contain client react exports for pages api', async () => { async function verifyReactExports(route, isEdge) { From ee00e9739ae4eaadc69a40b3c134a0b802d7ae4b Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 28 May 2024 23:57:34 +0200 Subject: [PATCH 03/12] turbopack context --- packages/next-swc/crates/next-core/src/next_shared/resolve.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/next-swc/crates/next-core/src/next_shared/resolve.rs b/packages/next-swc/crates/next-core/src/next_shared/resolve.rs index bd71f29e3d3dd..6b52e8ae86912 100644 --- a/packages/next-swc/crates/next-core/src/next_shared/resolve.rs +++ b/packages/next-swc/crates/next-core/src/next_shared/resolve.rs @@ -362,7 +362,8 @@ impl AfterResolvePlugin for NextNodeSharedRuntimeResolvePlugin { "next/dist/server/future/route-modules/{}/vendored/contexts/{}.js", match self.context { ServerContextType::Pages { .. } => "pages", - ServerContextType::AppRoute { .. } => "app-route", + ServerContextType::AppRoute { .. } | ServerContextType::Instrumentation { .. } => + "app-route", ServerContextType::AppSSR { .. } | ServerContextType::AppRSC { .. } => "app-page", _ => "unknown", }, From 3237ca43234e520d748a1e57b5cefc08dda9dff4 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Wed, 29 May 2024 00:55:05 +0200 Subject: [PATCH 04/12] change turbopack transform for middleware and instrument --- .../crates/next-core/src/next_server/transforms.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/next-swc/crates/next-core/src/next_server/transforms.rs b/packages/next-swc/crates/next-core/src/next_server/transforms.rs index dc8382f6233fe..99696233c4a1d 100644 --- a/packages/next-swc/crates/next-core/src/next_server/transforms.rs +++ b/packages/next-swc/crates/next-core/src/next_server/transforms.rs @@ -87,7 +87,13 @@ pub async fn get_next_server_transforms_rules( false } - ServerContextType::AppRSC { + ServerContextType::Middleware { + client_transition, .. + } + | ServerContextType::Instrumentation { + client_transition, .. + } + | ServerContextType::AppRSC { client_transition, .. } => { rules.push(get_server_actions_transform_rule( @@ -103,7 +109,6 @@ pub async fn get_next_server_transforms_rules( true } ServerContextType::AppRoute { .. } => false, - ServerContextType::Middleware { .. } | ServerContextType::Instrumentation { .. } => false, }; if !foreign_code { From 7a1703dcaca8876b19384dcd3b1afbdba5b409e9 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Wed, 29 May 2024 10:32:34 +0200 Subject: [PATCH 05/12] revert --- .../crates/next-core/src/next_server/transforms.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/packages/next-swc/crates/next-core/src/next_server/transforms.rs b/packages/next-swc/crates/next-core/src/next_server/transforms.rs index 99696233c4a1d..dc8382f6233fe 100644 --- a/packages/next-swc/crates/next-core/src/next_server/transforms.rs +++ b/packages/next-swc/crates/next-core/src/next_server/transforms.rs @@ -87,13 +87,7 @@ pub async fn get_next_server_transforms_rules( false } - ServerContextType::Middleware { - client_transition, .. - } - | ServerContextType::Instrumentation { - client_transition, .. - } - | ServerContextType::AppRSC { + ServerContextType::AppRSC { client_transition, .. } => { rules.push(get_server_actions_transform_rule( @@ -109,6 +103,7 @@ pub async fn get_next_server_transforms_rules( true } ServerContextType::AppRoute { .. } => false, + ServerContextType::Middleware { .. } | ServerContextType::Instrumentation { .. } => false, }; if !foreign_code { From be955bb338aff056da2ed7e9c9c768cecf33c213 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Wed, 29 May 2024 10:49:40 +0200 Subject: [PATCH 06/12] update test --- test/e2e/module-layer/instrumentation.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/e2e/module-layer/instrumentation.js b/test/e2e/module-layer/instrumentation.js index 6decffb39aca0..60c6ef96913c6 100644 --- a/test/e2e/module-layer/instrumentation.js +++ b/test/e2e/module-layer/instrumentation.js @@ -1,8 +1,10 @@ -import { textValue } from './lib/mixed-lib/shared-module' +import React from 'react' +// TODO: support react client transform in turbopack +// import { textValue } from './lib/mixed-lib/shared-module' export async function register() { - if (typeof textValue !== 'string') { - throw new Error('textValue is not a string') + if (Object(React).useState) { + throw new Error('instrumentation is not working correctly in server layer') } console.log('instrumentation:register') } From 55d903d111b3cf7c8c711b3983d20382d194fd22 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Wed, 29 May 2024 12:45:08 +0200 Subject: [PATCH 07/12] reorganize tests --- test/e2e/module-layer/middleware.js | 2 -- test/e2e/module-layer/module-layer.test.ts | 10 ---------- test/e2e/rsc-layers-transform/app/layout.js | 7 +++++++ test/e2e/rsc-layers-transform/app/page.js | 3 +++ .../instrumentation.js | 0 .../rsc-layers-transform/lib/shared-module.js | 4 ++++ test/e2e/rsc-layers-transform/middleware.js | 13 +++++++++++++ .../next.config.js | 0 .../rsc-layers-transform.test.ts | 19 +++++++++++++++++++ 9 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 test/e2e/rsc-layers-transform/app/layout.js create mode 100644 test/e2e/rsc-layers-transform/app/page.js rename test/e2e/{module-layer => rsc-layers-transform}/instrumentation.js (100%) create mode 100644 test/e2e/rsc-layers-transform/lib/shared-module.js create mode 100644 test/e2e/rsc-layers-transform/middleware.js rename test/e2e/{module-layer => rsc-layers-transform}/next.config.js (100%) create mode 100644 test/e2e/rsc-layers-transform/rsc-layers-transform.test.ts diff --git a/test/e2e/module-layer/middleware.js b/test/e2e/module-layer/middleware.js index 0a1dd469a2312..51bfdfc909298 100644 --- a/test/e2e/module-layer/middleware.js +++ b/test/e2e/module-layer/middleware.js @@ -1,7 +1,6 @@ import 'server-only' import * as React from 'react' import { NextResponse } from 'next/server' -import { textValue } from './lib/mixed-lib/shared-module' // import './lib/mixed-lib' export function middleware(request) { @@ -14,7 +13,6 @@ export function middleware(request) { if (request.nextUrl.pathname === '/middleware') { return Response.json({ React: Object.keys(ReactObject), - textValue, }) } diff --git a/test/e2e/module-layer/module-layer.test.ts b/test/e2e/module-layer/module-layer.test.ts index bc101e99dafb4..aa8e39e1ad1f0 100644 --- a/test/e2e/module-layer/module-layer.test.ts +++ b/test/e2e/module-layer/module-layer.test.ts @@ -4,10 +4,6 @@ import { getRedboxSource, hasRedbox, retry } from 'next-test-utils' describe('module layer', () => { const { next, isNextStart, isNextDev, isTurbopack } = nextTestSetup({ files: __dirname, - dependencies: { - react: '19.0.0-rc-915b914b3a-20240515', - 'react-dom': '19.0.0-rc-915b914b3a-20240515', - }, }) function runTests() { @@ -40,12 +36,6 @@ describe('module layer', () => { const json = await next.fetch('/middleware').then((res) => res.json()) expect(json.React).toContain('version') // basic react-server export expect(json.React).not.toContain('useEffect') // no client api export - expect(json.textValue).toBe('text-value') - }) - - it('should call instrumentation hook without errors', async () => { - const output = next.cliOutput - expect(output).toContain('instrumentation:register') }) // This is for backward compatibility, don't change react usage in existing pages/api diff --git a/test/e2e/rsc-layers-transform/app/layout.js b/test/e2e/rsc-layers-transform/app/layout.js new file mode 100644 index 0000000000000..750eb927b1980 --- /dev/null +++ b/test/e2e/rsc-layers-transform/app/layout.js @@ -0,0 +1,7 @@ +export default function Layout({ children }) { + return ( + + {children} + + ) +} diff --git a/test/e2e/rsc-layers-transform/app/page.js b/test/e2e/rsc-layers-transform/app/page.js new file mode 100644 index 0000000000000..9c4a21008113f --- /dev/null +++ b/test/e2e/rsc-layers-transform/app/page.js @@ -0,0 +1,3 @@ +export default function Page() { + return 'page' +} diff --git a/test/e2e/module-layer/instrumentation.js b/test/e2e/rsc-layers-transform/instrumentation.js similarity index 100% rename from test/e2e/module-layer/instrumentation.js rename to test/e2e/rsc-layers-transform/instrumentation.js diff --git a/test/e2e/rsc-layers-transform/lib/shared-module.js b/test/e2e/rsc-layers-transform/lib/shared-module.js new file mode 100644 index 0000000000000..89966a446a748 --- /dev/null +++ b/test/e2e/rsc-layers-transform/lib/shared-module.js @@ -0,0 +1,4 @@ +import Link from 'next/link' + +export const textValue = 'text-value' +export const TestLink = Link diff --git a/test/e2e/rsc-layers-transform/middleware.js b/test/e2e/rsc-layers-transform/middleware.js new file mode 100644 index 0000000000000..0b5f5434cafe9 --- /dev/null +++ b/test/e2e/rsc-layers-transform/middleware.js @@ -0,0 +1,13 @@ +import { NextResponse } from 'next/server' +import { textValue, TestLink } from './lib/shared-module' + +export function middleware(request) { + if (request.nextUrl.pathname === '/middleware-transform') { + return Response.json({ + clientReference: TestLink.$$typeof.toString(), + textValue, + }) + } + + return NextResponse.next() +} diff --git a/test/e2e/module-layer/next.config.js b/test/e2e/rsc-layers-transform/next.config.js similarity index 100% rename from test/e2e/module-layer/next.config.js rename to test/e2e/rsc-layers-transform/next.config.js diff --git a/test/e2e/rsc-layers-transform/rsc-layers-transform.test.ts b/test/e2e/rsc-layers-transform/rsc-layers-transform.test.ts new file mode 100644 index 0000000000000..552d8c6e98deb --- /dev/null +++ b/test/e2e/rsc-layers-transform/rsc-layers-transform.test.ts @@ -0,0 +1,19 @@ +import { nextTestSetup } from 'e2e-utils' + +describe('rsc layers transform', () => { + const { next } = nextTestSetup({ + files: __dirname, + }) + + it('should render installed react-server condition for middleware', async () => { + const json = await next + .fetch('/middleware-transform') + .then((res) => res.json()) + expect(json.textValue).toBe('text-value') + }) + + it('should call instrumentation hook without errors', async () => { + const output = next.cliOutput + expect(output).toContain('instrumentation:register') + }) +}) From 9fad38bfa21db8ef7b7270cb30be6c2b0efa6327 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Wed, 29 May 2024 14:38:16 +0200 Subject: [PATCH 08/12] update import map --- packages/next-swc/crates/next-core/src/next_import_map.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/next-swc/crates/next-core/src/next_import_map.rs b/packages/next-swc/crates/next-core/src/next_import_map.rs index e21ca1e6d82d6..65ea4117a51c7 100644 --- a/packages/next-swc/crates/next-core/src/next_import_map.rs +++ b/packages/next-swc/crates/next-core/src/next_import_map.rs @@ -583,10 +583,9 @@ async fn insert_next_server_special_aliases( rsc_aliases(import_map, project_path, ty, runtime, next_config).await?; } - ServerContextType::Middleware => { + ServerContextType::Middleware | ServerContextType::Instrumentation => { rsc_aliases(import_map, project_path, ty, runtime, next_config).await?; } - ServerContextType::Instrumentation => {} } // see https://github.com/vercel/next.js/blob/8013ef7372fc545d49dbd060461224ceb563b454/packages/next/src/build/webpack-config.ts#L1449-L1531 From 71bb5228baa87a2ba6542c1447f21011d1199084 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Wed, 29 May 2024 14:41:58 +0200 Subject: [PATCH 09/12] update test --- test/e2e/rsc-layers-transform/middleware.js | 2 +- .../rsc-layers-transform/rsc-layers-transform.test.ts | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/test/e2e/rsc-layers-transform/middleware.js b/test/e2e/rsc-layers-transform/middleware.js index 0b5f5434cafe9..4321dd93d9ef1 100644 --- a/test/e2e/rsc-layers-transform/middleware.js +++ b/test/e2e/rsc-layers-transform/middleware.js @@ -2,7 +2,7 @@ import { NextResponse } from 'next/server' import { textValue, TestLink } from './lib/shared-module' export function middleware(request) { - if (request.nextUrl.pathname === '/middleware-transform') { + if (request.nextUrl.pathname === '/middleware') { return Response.json({ clientReference: TestLink.$$typeof.toString(), textValue, diff --git a/test/e2e/rsc-layers-transform/rsc-layers-transform.test.ts b/test/e2e/rsc-layers-transform/rsc-layers-transform.test.ts index 552d8c6e98deb..db53f4e6d4ae9 100644 --- a/test/e2e/rsc-layers-transform/rsc-layers-transform.test.ts +++ b/test/e2e/rsc-layers-transform/rsc-layers-transform.test.ts @@ -6,10 +6,12 @@ describe('rsc layers transform', () => { }) it('should render installed react-server condition for middleware', async () => { - const json = await next - .fetch('/middleware-transform') - .then((res) => res.json()) - expect(json.textValue).toBe('text-value') + const json = await next.fetch('/middleware').then((res) => res.json()) + + expect(json).toEqual({ + textValue: 'text-value', + clientReference: 'Symbol(react.client.reference)', + }) }) it('should call instrumentation hook without errors', async () => { From 0cb58ba9694807449be25afd26dd8d3504526327 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Wed, 29 May 2024 15:25:23 +0200 Subject: [PATCH 10/12] update server ctx --- packages/next-swc/crates/next-core/src/next_server/context.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/next-swc/crates/next-core/src/next_server/context.rs b/packages/next-swc/crates/next-core/src/next_server/context.rs index c934abde0daba..ed6bec83df5f1 100644 --- a/packages/next-swc/crates/next-core/src/next_server/context.rs +++ b/packages/next-swc/crates/next-core/src/next_server/context.rs @@ -108,6 +108,7 @@ impl ServerContextType { | ServerContextType::AppRoute { .. } | ServerContextType::PagesApi { .. } | ServerContextType::Middleware { .. } + | ServerContextType::Instrumentation { .. } ) } } From 19b6689376da43c1dada46fbcd29b7ec0961c6bd Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Wed, 29 May 2024 15:39:49 +0200 Subject: [PATCH 11/12] disable turbopack for instrumentation --- test/e2e/rsc-layers-transform/instrumentation.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/e2e/rsc-layers-transform/instrumentation.js b/test/e2e/rsc-layers-transform/instrumentation.js index 60c6ef96913c6..1d3347e1b8a1f 100644 --- a/test/e2e/rsc-layers-transform/instrumentation.js +++ b/test/e2e/rsc-layers-transform/instrumentation.js @@ -3,7 +3,8 @@ import React from 'react' // import { textValue } from './lib/mixed-lib/shared-module' export async function register() { - if (Object(React).useState) { + // TODO: support react-server condition for instrumentation hook in turbopack + if (!process.env.TURBOPACK && Object(React).useState) { throw new Error('instrumentation is not working correctly in server layer') } console.log('instrumentation:register') From a68e1ee3328492a9705d139ddb5b4b8491ea7bcc Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Thu, 30 May 2024 09:52:24 +0200 Subject: [PATCH 12/12] update test --- .../rsc-layers-transform/instrumentation.js | 4 +-- .../rsc-layers-transform.test.ts | 35 +++++++++++-------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/test/e2e/rsc-layers-transform/instrumentation.js b/test/e2e/rsc-layers-transform/instrumentation.js index 1d3347e1b8a1f..84ee3ca718965 100644 --- a/test/e2e/rsc-layers-transform/instrumentation.js +++ b/test/e2e/rsc-layers-transform/instrumentation.js @@ -1,6 +1,5 @@ import React from 'react' -// TODO: support react client transform in turbopack -// import { textValue } from './lib/mixed-lib/shared-module' +import { textValue } from './lib/shared-module' export async function register() { // TODO: support react-server condition for instrumentation hook in turbopack @@ -8,4 +7,5 @@ export async function register() { throw new Error('instrumentation is not working correctly in server layer') } console.log('instrumentation:register') + console.log('instrumentation:text:' + textValue) } diff --git a/test/e2e/rsc-layers-transform/rsc-layers-transform.test.ts b/test/e2e/rsc-layers-transform/rsc-layers-transform.test.ts index db53f4e6d4ae9..91b4a8dfcbd24 100644 --- a/test/e2e/rsc-layers-transform/rsc-layers-transform.test.ts +++ b/test/e2e/rsc-layers-transform/rsc-layers-transform.test.ts @@ -1,21 +1,26 @@ import { nextTestSetup } from 'e2e-utils' -describe('rsc layers transform', () => { - const { next } = nextTestSetup({ - files: __dirname, - }) +// TODO: support react-server condition for instrumentation hook in turbopack +;(process.env.TURBOPACK ? describe.skip : describe)( + 'rsc layers transform', + () => { + const { next } = nextTestSetup({ + files: __dirname, + }) - it('should render installed react-server condition for middleware', async () => { - const json = await next.fetch('/middleware').then((res) => res.json()) + it('should render installed react-server condition for middleware', async () => { + const json = await next.fetch('/middleware').then((res) => res.json()) - expect(json).toEqual({ - textValue: 'text-value', - clientReference: 'Symbol(react.client.reference)', + expect(json).toEqual({ + textValue: 'text-value', + clientReference: 'Symbol(react.client.reference)', + }) }) - }) - it('should call instrumentation hook without errors', async () => { - const output = next.cliOutput - expect(output).toContain('instrumentation:register') - }) -}) + it('should call instrumentation hook without errors', async () => { + const output = next.cliOutput + expect(output).toContain('instrumentation:register') + expect(output).toContain('instrumentation:text:text-value') + }) + } +)