|
|
import type { WebpackLayerName } from '../lib/constants' |
|
|
import type { NextConfigComplete } from '../server/config-shared' |
|
|
import type { ResolveOptions } from 'webpack' |
|
|
import { defaultOverrides } from '../server/require-hook' |
|
|
import { BARREL_OPTIMIZATION_PREFIX } from '../shared/lib/constants' |
|
|
import path from '../shared/lib/isomorphic/path' |
|
|
import { |
|
|
NODE_BASE_ESM_RESOLVE_OPTIONS, |
|
|
NODE_BASE_RESOLVE_OPTIONS, |
|
|
NODE_ESM_RESOLVE_OPTIONS, |
|
|
NODE_RESOLVE_OPTIONS, |
|
|
} from './webpack-config' |
|
|
import { isWebpackBundledLayer, shouldUseReactServerCondition } from './utils' |
|
|
import { normalizePathSep } from '../shared/lib/page-path/normalize-path-sep' |
|
|
const reactPackagesRegex = /^(react|react-dom|react-server-dom-webpack)($|\/)/ |
|
|
|
|
|
const pathSeparators = '[/\\\\]' |
|
|
const optionalEsmPart = `((${pathSeparators}esm)?${pathSeparators})` |
|
|
const externalFileEnd = '(\\.external(\\.js)?)$' |
|
|
const nextDist = `next${pathSeparators}dist` |
|
|
|
|
|
const externalPattern = new RegExp( |
|
|
`${nextDist}${optionalEsmPart}.*${externalFileEnd}` |
|
|
) |
|
|
|
|
|
const nodeModulesRegex = /node_modules[/\\].*\.[mc]?js$/ |
|
|
|
|
|
export function isResourceInPackages( |
|
|
resource: string, |
|
|
packageNames?: string[], |
|
|
packageDirMapping?: Map<string, string> |
|
|
): boolean { |
|
|
if (!packageNames) return false |
|
|
return packageNames.some((p: string) => |
|
|
packageDirMapping && packageDirMapping.has(p) |
|
|
? resource.startsWith(packageDirMapping.get(p)! + path.sep) |
|
|
: resource.includes( |
|
|
path.sep + |
|
|
path.join('node_modules', p.replace(/\//g, path.sep)) + |
|
|
path.sep |
|
|
) |
|
|
) |
|
|
} |
|
|
|
|
|
export async function resolveExternal( |
|
|
dir: string, |
|
|
esmExternalsConfig: NextConfigComplete['experimental']['esmExternals'], |
|
|
context: string, |
|
|
request: string, |
|
|
isEsmRequested: boolean, |
|
|
getResolve: ( |
|
|
options: ResolveOptions |
|
|
) => ( |
|
|
resolveContext: string, |
|
|
resolveRequest: string |
|
|
) => Promise<[string | null, boolean]>, |
|
|
isLocalCallback?: (res: string) => any, |
|
|
baseResolveCheck = true, |
|
|
esmResolveOptions: any = NODE_ESM_RESOLVE_OPTIONS, |
|
|
nodeResolveOptions: any = NODE_RESOLVE_OPTIONS, |
|
|
baseEsmResolveOptions: any = NODE_BASE_ESM_RESOLVE_OPTIONS, |
|
|
baseResolveOptions: any = NODE_BASE_RESOLVE_OPTIONS |
|
|
) { |
|
|
const esmExternals = !!esmExternalsConfig |
|
|
const looseEsmExternals = esmExternalsConfig === 'loose' |
|
|
|
|
|
let res: string | null = null |
|
|
let isEsm: boolean = false |
|
|
|
|
|
const preferEsmOptions = |
|
|
esmExternals && isEsmRequested ? [true, false] : [false] |
|
|
|
|
|
for (const preferEsm of preferEsmOptions) { |
|
|
const resolveOptions = preferEsm ? esmResolveOptions : nodeResolveOptions |
|
|
|
|
|
const resolve = getResolve(resolveOptions) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
;[res, isEsm] = await resolve(context, request) |
|
|
} catch (err) { |
|
|
res = null |
|
|
} |
|
|
|
|
|
if (!res) { |
|
|
continue |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!isEsmRequested && isEsm && !looseEsmExternals) { |
|
|
continue |
|
|
} |
|
|
|
|
|
if (isLocalCallback) { |
|
|
return { localRes: isLocalCallback(res) } |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (baseResolveCheck) { |
|
|
let baseRes: string | null |
|
|
let baseIsEsm: boolean |
|
|
try { |
|
|
const baseResolve = getResolve( |
|
|
isEsm ? baseEsmResolveOptions : baseResolveOptions |
|
|
) |
|
|
;[baseRes, baseIsEsm] = await baseResolve(dir, request) |
|
|
} catch (err) { |
|
|
baseRes = null |
|
|
baseIsEsm = false |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (baseRes !== res || isEsm !== baseIsEsm) { |
|
|
res = null |
|
|
continue |
|
|
} |
|
|
} |
|
|
break |
|
|
} |
|
|
return { res, isEsm } |
|
|
} |
|
|
|
|
|
export function makeExternalHandler({ |
|
|
config, |
|
|
optOutBundlingPackageRegex, |
|
|
transpiledPackages, |
|
|
dir, |
|
|
}: { |
|
|
config: NextConfigComplete |
|
|
optOutBundlingPackageRegex: RegExp |
|
|
transpiledPackages: string[] |
|
|
dir: string |
|
|
}) { |
|
|
let resolvedExternalPackageDirs: Map<string, string> |
|
|
const looseEsmExternals = config.experimental?.esmExternals === 'loose' |
|
|
|
|
|
return async function handleExternals( |
|
|
context: string, |
|
|
request: string, |
|
|
dependencyType: string, |
|
|
layer: WebpackLayerName | null, |
|
|
getResolve: ( |
|
|
options: any |
|
|
) => ( |
|
|
resolveContext: string, |
|
|
resolveRequest: string |
|
|
) => Promise<[string | null, boolean]> |
|
|
) { |
|
|
|
|
|
|
|
|
const isLocal: boolean = |
|
|
request.startsWith('.') || |
|
|
|
|
|
|
|
|
path.posix.isAbsolute(request) || |
|
|
|
|
|
|
|
|
(process.platform === 'win32' && path.win32.isAbsolute(request)) |
|
|
|
|
|
|
|
|
|
|
|
if (request === 'next') { |
|
|
return `commonjs next/dist/lib/import-next-warning` |
|
|
} |
|
|
|
|
|
const isAppLayer = isWebpackBundledLayer(layer) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!isLocal) { |
|
|
if (/^next$/.test(request)) { |
|
|
return `commonjs ${request}` |
|
|
} |
|
|
|
|
|
if (reactPackagesRegex.test(request) && !isAppLayer) { |
|
|
return `commonjs ${request}` |
|
|
} |
|
|
|
|
|
const notExternalModules = |
|
|
/^(?:private-next-pages\/|next\/(?:dist\/pages\/|(?:app|cache|document|link|form|head|image|legacy\/image|constants|dynamic|script|navigation|headers|router|compat\/router|server)$)|string-hash|private-next-rsc-action-validate|private-next-rsc-action-client-wrapper|private-next-rsc-server-reference|private-next-rsc-cache-wrapper|private-next-rsc-track-dynamic-import$)/ |
|
|
if (notExternalModules.test(request)) { |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (request.includes('@swc/helpers')) { |
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (request.startsWith(BARREL_OPTIMIZATION_PREFIX)) { |
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const isEsmRequested = dependencyType === 'esm' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
|
shouldUseReactServerCondition(layer) && |
|
|
request === 'next/dist/compiled/@vercel/og/index.node.js' |
|
|
) { |
|
|
return `module ${request}` |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (request.startsWith('next/dist/')) { |
|
|
|
|
|
|
|
|
if (/^next[\\/]dist[\\/]shared[\\/]lib[\\/]image-loader/.test(request)) { |
|
|
return |
|
|
} |
|
|
|
|
|
if (/^next[\\/]dist[\\/]compiled[\\/]next-server/.test(request)) { |
|
|
return `commonjs ${request}` |
|
|
} |
|
|
|
|
|
if ( |
|
|
/^next[\\/]dist[\\/]shared[\\/](?!lib[\\/]router[\\/]router)/.test( |
|
|
request |
|
|
) || |
|
|
/^next[\\/]dist[\\/]compiled[\\/].*\.c?js$/.test(request) |
|
|
) { |
|
|
return `commonjs ${request}` |
|
|
} |
|
|
|
|
|
if ( |
|
|
/^next[\\/]dist[\\/]esm[\\/]shared[\\/](?!lib[\\/]router[\\/]router)/.test( |
|
|
request |
|
|
) || |
|
|
/^next[\\/]dist[\\/]compiled[\\/].*\.mjs$/.test(request) |
|
|
) { |
|
|
return `module ${request}` |
|
|
} |
|
|
|
|
|
return resolveNextExternal(request) |
|
|
} |
|
|
|
|
|
|
|
|
const resolveResult = await resolveExternal( |
|
|
dir, |
|
|
config.experimental.esmExternals, |
|
|
context, |
|
|
request, |
|
|
isEsmRequested, |
|
|
getResolve, |
|
|
isLocal ? resolveNextExternal : undefined |
|
|
) |
|
|
|
|
|
if ('localRes' in resolveResult) { |
|
|
return resolveResult.localRes |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (request === 'styled-jsx/style') { |
|
|
resolveResult.res = defaultOverrides['styled-jsx/style'] |
|
|
} |
|
|
|
|
|
const { res, isEsm } = resolveResult |
|
|
|
|
|
|
|
|
|
|
|
if (!res) { |
|
|
return |
|
|
} |
|
|
|
|
|
const isOptOutBundling = optOutBundlingPackageRegex.test(res) |
|
|
|
|
|
|
|
|
if (!isOptOutBundling && isAppLayer) { |
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!isEsmRequested && isEsm && !looseEsmExternals && !isLocal) { |
|
|
throw new Error( |
|
|
`ESM packages (${request}) need to be imported. Use 'import' to reference the package instead. https://nextjs.org/docs/messages/import-esm-externals` |
|
|
) |
|
|
} |
|
|
|
|
|
const externalType = isEsm ? 'module' : 'commonjs' |
|
|
|
|
|
|
|
|
if ( |
|
|
|
|
|
/node_modules[/\\]@babel[/\\]runtime[/\\]/.test(res) |
|
|
) { |
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
if ( |
|
|
/node_modules[/\\]webpack/.test(res) || |
|
|
/node_modules[/\\]css-loader/.test(res) |
|
|
) { |
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (transpiledPackages && !resolvedExternalPackageDirs) { |
|
|
resolvedExternalPackageDirs = new Map() |
|
|
|
|
|
for (const pkg of transpiledPackages) { |
|
|
const pkgRes = await resolveExternal( |
|
|
dir, |
|
|
config.experimental.esmExternals, |
|
|
context, |
|
|
pkg + '/package.json', |
|
|
isEsmRequested, |
|
|
getResolve, |
|
|
isLocal ? resolveNextExternal : undefined |
|
|
) |
|
|
if (pkgRes.res) { |
|
|
resolvedExternalPackageDirs.set(pkg, path.dirname(pkgRes.res)) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
const resolvedBundlingOptOutRes = resolveBundlingOptOutPackages({ |
|
|
resolvedRes: res, |
|
|
config, |
|
|
resolvedExternalPackageDirs, |
|
|
isAppLayer, |
|
|
externalType, |
|
|
isOptOutBundling, |
|
|
request, |
|
|
transpiledPackages, |
|
|
}) |
|
|
if (resolvedBundlingOptOutRes) { |
|
|
return resolvedBundlingOptOutRes |
|
|
} |
|
|
|
|
|
|
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
function resolveBundlingOptOutPackages({ |
|
|
resolvedRes, |
|
|
config, |
|
|
resolvedExternalPackageDirs, |
|
|
isAppLayer, |
|
|
externalType, |
|
|
isOptOutBundling, |
|
|
request, |
|
|
transpiledPackages, |
|
|
}: { |
|
|
resolvedRes: string |
|
|
config: NextConfigComplete |
|
|
resolvedExternalPackageDirs: Map<string, string> |
|
|
isAppLayer: boolean |
|
|
externalType: string |
|
|
isOptOutBundling: boolean |
|
|
request: string |
|
|
transpiledPackages: string[] |
|
|
}) { |
|
|
if (nodeModulesRegex.test(resolvedRes)) { |
|
|
const shouldBundlePages = |
|
|
!isAppLayer && config.bundlePagesRouterDependencies && !isOptOutBundling |
|
|
|
|
|
const shouldBeBundled = |
|
|
shouldBundlePages || |
|
|
isResourceInPackages( |
|
|
resolvedRes, |
|
|
transpiledPackages, |
|
|
resolvedExternalPackageDirs |
|
|
) |
|
|
|
|
|
if (!shouldBeBundled) { |
|
|
return `${externalType} ${request}` |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function resolveNextExternal(localRes: string) { |
|
|
const isExternal = externalPattern.test(localRes) |
|
|
|
|
|
|
|
|
|
|
|
if (isExternal) { |
|
|
|
|
|
|
|
|
return `commonjs ${normalizePathSep( |
|
|
localRes.replace(/.*?next[/\\]dist/, 'next/dist') |
|
|
)}` |
|
|
} |
|
|
} |
|
|
|