From 6282423fd7adfac6bd06cd430afcd276f490eaa9 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Mon, 17 Jun 2024 18:00:23 +0200 Subject: [PATCH] Remove `worker` condition name when resolving files in the Edge runtime (#66808) --- .../next-swc/crates/next-core/src/util.rs | 2 +- .../src/build/webpack-config-rules/resolve.ts | 7 +--- .../import-conditions.test.ts | 40 +++++++++---------- 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/packages/next-swc/crates/next-core/src/util.rs b/packages/next-swc/crates/next-core/src/util.rs index 40841e28cf8fa..58bf8b5837d1b 100644 --- a/packages/next-swc/crates/next-core/src/util.rs +++ b/packages/next-swc/crates/next-core/src/util.rs @@ -150,7 +150,7 @@ impl NextRuntime { pub fn conditions(&self) -> &'static [&'static str] { match self { NextRuntime::NodeJs => &["node"], - NextRuntime::Edge => &["edge-light", "worker"], + NextRuntime::Edge => &["edge-light"], } } } diff --git a/packages/next/src/build/webpack-config-rules/resolve.ts b/packages/next/src/build/webpack-config-rules/resolve.ts index 35f9f56969ef4..b9c546bc6ad78 100644 --- a/packages/next/src/build/webpack-config-rules/resolve.ts +++ b/packages/next/src/build/webpack-config-rules/resolve.ts @@ -6,19 +6,14 @@ import { // exports. export const edgeConditionNames = [ 'edge-light', - 'worker', // inherits the default conditions '...', ] -const mainFieldsPerCompiler: Record< - CompilerNameValues | 'server-esm', - string[] -> = { +const mainFieldsPerCompiler = { // For default case, prefer CJS over ESM on server side. e.g. pages dir SSR [COMPILER_NAMES.server]: ['main', 'module'], [COMPILER_NAMES.client]: ['browser', 'module', 'main'], - [COMPILER_NAMES.edgeServer]: edgeConditionNames, // For bundling-all strategy, prefer ESM over CJS 'server-esm': ['module', 'main'], } diff --git a/test/e2e/import-conditions/import-conditions.test.ts b/test/e2e/import-conditions/import-conditions.test.ts index a6dc66f27c7c3..53c44c9ada2c3 100644 --- a/test/e2e/import-conditions/import-conditions.test.ts +++ b/test/e2e/import-conditions/import-conditions.test.ts @@ -22,8 +22,8 @@ describe('react version', () => { } expect(middlewareHeaders).toEqual({ react: 'react-server', - serverFavoringBrowser: 'worker', - serverFavoringEdge: 'worker', + serverFavoringBrowser: 'browser', + serverFavoringEdge: 'edge-light', }) }) @@ -34,8 +34,8 @@ describe('react version', () => { expect(JSON.parse(json)).toEqual({ server: { react: 'default', - serverFavoringBrowser: 'worker', - serverFavoringEdge: 'worker', + serverFavoringBrowser: 'browser', + serverFavoringEdge: 'edge-light', }, client: { react: 'default', @@ -59,8 +59,8 @@ describe('react version', () => { } expect(middlewareHeaders).toEqual({ react: 'react-server', - serverFavoringBrowser: 'worker', - serverFavoringEdge: 'worker', + serverFavoringBrowser: 'browser', + serverFavoringEdge: 'edge-light', }) }) @@ -96,8 +96,8 @@ describe('react version', () => { } expect(middlewareHeaders).toEqual({ react: 'react-server', - serverFavoringBrowser: 'worker', - serverFavoringEdge: 'worker', + serverFavoringBrowser: 'browser', + serverFavoringEdge: 'edge-light', }) }) @@ -111,8 +111,8 @@ describe('react version', () => { expect(JSON.parse(json)).toEqual({ server: { react: 'react-server', - serverFavoringBrowser: 'worker', - serverFavoringEdge: 'worker', + serverFavoringBrowser: 'browser', + serverFavoringEdge: 'edge-light', }, client: { react: 'default', @@ -121,8 +121,8 @@ describe('react version', () => { }, action: { react: 'react-server', - serverFavoringBrowser: 'worker', - serverFavoringEdge: 'worker', + serverFavoringBrowser: 'browser', + serverFavoringEdge: 'edge-light', }, }) }) @@ -141,8 +141,8 @@ describe('react version', () => { } expect(middlewareHeaders).toEqual({ react: 'react-server', - serverFavoringBrowser: 'worker', - serverFavoringEdge: 'worker', + serverFavoringBrowser: 'browser', + serverFavoringEdge: 'edge-light', }) }) @@ -188,8 +188,8 @@ describe('react version', () => { expect({ middlewareHeaders, data }).toEqual({ middlewareHeaders: { react: 'react-server', - serverFavoringBrowser: 'worker', - serverFavoringEdge: 'worker', + serverFavoringBrowser: 'browser', + serverFavoringEdge: 'edge-light', }, data: { react: 'react-server', @@ -215,13 +215,13 @@ describe('react version', () => { expect({ middlewareHeaders, data }).toEqual({ middlewareHeaders: { react: 'react-server', - serverFavoringBrowser: 'worker', - serverFavoringEdge: 'worker', + serverFavoringBrowser: 'browser', + serverFavoringEdge: 'edge-light', }, data: { react: 'react-server', - serverFavoringBrowser: 'worker', - serverFavoringEdge: 'worker', + serverFavoringBrowser: 'browser', + serverFavoringEdge: 'edge-light', }, }) })