-
Notifications
You must be signed in to change notification settings - Fork 27.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pick esm main fields and condition names first for RSC server layer (#…
…50548) For RSC server layer so far we bundle all dependencies, ESM format is the better one rather than CJS to analyze and tree-shake out the unused parts. This PR changes pick the condition names that are in ESM format first for server layer. Also fixes the misorder of condition names of edge runtime, `conditionNames` should only contain either ESM or CJS, previously the main fields are mixed with conditon names which is not expected for webpack, we separate them now. Since we're picking ESM instead CJS now, the error of require `exports * from` doesn't exist anymore, but if you're using a CJS dependency which require a ESM package, it will error. This is the existing behavior for our webpack configuration but could happen on server layer bundling Other related changes: * Imports are hoisted in ESM, so migrate`enhanceGlobals` to a imported module * Use `...` to pick the proper imports by import expression, and prefer the `react-server` / `edge-light` condition names for corresponding cases * Remove edge SSR duplicated `middleware` export checking
- Loading branch information
Showing
29 changed files
with
989 additions
and
789 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
5 changes: 2 additions & 3 deletions
5
packages/next-swc/crates/next-core/js/src/entry/edge-bootstrap.ts
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
32 changes: 16 additions & 16 deletions
32
packages/next/src/compiled/babel-packages/packages-bundle.js
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
declare const _ENTRIES: any | ||
|
||
async function registerInstrumentation() { | ||
if ( | ||
'_ENTRIES' in globalThis && | ||
_ENTRIES.middleware_instrumentation && | ||
_ENTRIES.middleware_instrumentation.register | ||
) { | ||
try { | ||
await _ENTRIES.middleware_instrumentation.register() | ||
} catch (err: any) { | ||
err.message = `An error occurred while loading instrumentation hook: ${err.message}` | ||
throw err | ||
} | ||
} | ||
} | ||
|
||
let registerInstrumentationPromise: Promise<void> | null = null | ||
export function ensureInstrumentationRegistered() { | ||
if (!registerInstrumentationPromise) { | ||
registerInstrumentationPromise = registerInstrumentation() | ||
} | ||
return registerInstrumentationPromise | ||
} | ||
|
||
function getUnsupportedModuleErrorMessage(module: string) { | ||
// warning: if you change these messages, you must adjust how react-dev-overlay's middleware detects modules not found | ||
return `The edge runtime does not support Node.js '${module}' module. | ||
Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime` | ||
} | ||
|
||
function __import_unsupported(moduleName: string) { | ||
const proxy: any = new Proxy(function () {}, { | ||
get(_obj, prop) { | ||
if (prop === 'then') { | ||
return {} | ||
} | ||
throw new Error(getUnsupportedModuleErrorMessage(moduleName)) | ||
}, | ||
construct() { | ||
throw new Error(getUnsupportedModuleErrorMessage(moduleName)) | ||
}, | ||
apply(_target, _this, args) { | ||
if (typeof args[0] === 'function') { | ||
return args[0](proxy) | ||
} | ||
throw new Error(getUnsupportedModuleErrorMessage(moduleName)) | ||
}, | ||
}) | ||
return new Proxy({}, { get: () => proxy }) | ||
} | ||
|
||
function enhanceGlobals() { | ||
// The condition is true when the "process" module is provided | ||
if (process !== global.process) { | ||
// prefer local process but global.process has correct "env" | ||
process.env = global.process.env | ||
global.process = process | ||
} | ||
|
||
// to allow building code that import but does not use node.js modules, | ||
// webpack will expect this function to exist in global scope | ||
Object.defineProperty(globalThis, '__import_unsupported', { | ||
value: __import_unsupported, | ||
enumerable: false, | ||
configurable: false, | ||
}) | ||
|
||
// Eagerly fire instrumentation hook to make the startup faster. | ||
void ensureInstrumentationRegistered() | ||
} | ||
|
||
enhanceGlobals() |
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
Oops, something went wrong.