@@ -47,6 +47,7 @@ export async function resolveExternal(
4747 context: string,
4848 request: string,
4949 isEsmRequested: boolean,
50+ optOutBundlingPackages: string[],
5051 getResolve: (
5152 options: any
5253 ) => (
@@ -66,8 +67,15 @@ export async function resolveExternal(
6667 let res: string | null = null
6768 let isEsm: boolean = false
6869
69- let preferEsmOptions =
70- esmExternals && isEsmRequested ? [true, false] : [false]
70+ const preferEsmOptions =
71+ esmExternals &&
72+ isEsmRequested &&
73+ // For package that marked as externals that should be not bundled,
74+ // we don't resolve them as ESM since it could be resolved as async module,
75+ // such as `import(external package)` in the bundle, valued as a `Promise`.
76+ !optOutBundlingPackages.some((optOut) => request.startsWith(optOut))
77+ ? [true, false]
78+ : [false]
7179
7280 for (const preferEsm of preferEsmOptions) {
7381 const resolve = getResolve(
@@ -131,10 +139,12 @@ export async function resolveExternal(
131139
132140export function makeExternalHandler({
133141 config,
142+ optOutBundlingPackages,
134143 optOutBundlingPackageRegex,
135144 dir,
136145}: {
137146 config: NextConfigComplete
147+ optOutBundlingPackages: string[]
138148 optOutBundlingPackageRegex: RegExp
139149 dir: string
140150}) {
@@ -289,6 +299,7 @@ export function makeExternalHandler({
289299 context,
290300 request,
291301 isEsmRequested,
302+ optOutBundlingPackages,
292303 getResolve,
293304 isLocal ? resolveNextExternal : undefined
294305 )
@@ -349,6 +360,7 @@ export function makeExternalHandler({
349360 context,
350361 pkg + '/package.json',
351362 isEsmRequested,
363+ optOutBundlingPackages,
352364 getResolve,
353365 isLocal ? resolveNextExternal : undefined
354366 )
0 commit comments