-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
47 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { COMPILER_NAMES, CompilerNameValues } from '../../shared/lib/constants' | ||
|
||
// exports.<conditionName> | ||
export const edgeConditionNames = [ | ||
'edge-light', | ||
'worker', | ||
// inherits the default conditions | ||
'...', | ||
] | ||
|
||
const mainFieldsPerCompiler: Record< | ||
CompilerNameValues | 'app-router-server', | ||
string[] | ||
> = { | ||
// 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 app router since everything is bundled, prefer ESM over CJS | ||
['app-router-server']: ['module', 'main'], | ||
} | ||
|
||
export function getMainField( | ||
pageType: 'app' | 'pages', | ||
compilerType: CompilerNameValues | ||
) { | ||
if (compilerType === COMPILER_NAMES.edgeServer) { | ||
return edgeConditionNames | ||
} else if (compilerType === COMPILER_NAMES.client) { | ||
return mainFieldsPerCompiler[COMPILER_NAMES.client] | ||
} | ||
|
||
// Prefer module fields over main fields for isomorphic packages on server layer | ||
return pageType === 'app' | ||
? mainFieldsPerCompiler['app-router-server'] | ||
: mainFieldsPerCompiler[COMPILER_NAMES.server] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters