-
Notifications
You must be signed in to change notification settings - Fork 27.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prefer module over main on main fields for app router server compiler (…
…#56532) This change is to pick the esm assets for RSC server layer and server rendering side, this is for production and development perf purpose, also to let them apply with the ESM related optimization such as tree-shaking, barrel files optimizations, etc. We found a few packages that can't be optimized easily in bundling because we're using "main" field so the packages are not able to be tree-shaked, ending up with large bundle in the dist. This will change a lot for the bundling improvements as some packages are only having "main" and "module" field. So switching from CJS to ESM means better bundling, more optimization, and faster code. #56501 was a precondition for this, as previously the bundling strategy was applied to some library but triggered the invalid hooks erroring. ### Other Monior Change Previously we'll prefer to resolve CJS as there're 2 versions of react, using CJS assets will help let them pass by require-hook to use canary react for app router bundling assets. But now we changed the approach to bundling nextjs runtime and react packages. Now we dropped the condition that prefered to resolve CJS exports for externals, since if you're putting them in `serverComponentsExternalPackages`, they're not using the built-in react, so could potentially having trouble if any dependency is using react but excluded in bundles. So far we didn't see any report to this. Closes NEXT-1286
- Loading branch information
Showing
14 changed files
with
99 additions
and
47 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
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,40 @@ | ||
import { | ||
COMPILER_NAMES, | ||
type 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
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
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
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
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
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
5 changes: 5 additions & 0 deletions
5
test/e2e/app-dir/app-external/node_modules_bak/conditional-exports-optout/react.js
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,5 @@ | ||
import React from 'react' | ||
|
||
export function getReactVersion() { | ||
return React.version | ||
} |
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
5 changes: 5 additions & 0 deletions
5
test/e2e/app-dir/app-external/node_modules_bak/conditional-exports/react.js
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,5 @@ | ||
import React from 'react' | ||
|
||
export function getReactVersion() { | ||
return React.version | ||
} |
1 change: 1 addition & 0 deletions
1
test/e2e/app-dir/app-external/node_modules_bak/server-module-field/index.cjs
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 @@ | ||
exports.name = 'server-module-field:main' |
1 change: 1 addition & 0 deletions
1
test/e2e/app-dir/app-external/node_modules_bak/server-module-field/index.esm.js
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 @@ | ||
export const name = 'server-module-field:module' |
4 changes: 4 additions & 0 deletions
4
test/e2e/app-dir/app-external/node_modules_bak/server-module-field/package.json
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,4 @@ | ||
{ | ||
"main": "./index.cjs", | ||
"module": "./index.esm.js" | ||
} |