Unverified Commit 82001f2d authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Share resolve logic for trace and externals (#30499)

Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 79026168
...@@ -221,6 +221,93 @@ let TSCONFIG_WARNED = false ...@@ -221,6 +221,93 @@ let TSCONFIG_WARNED = false
export const nextImageLoaderRegex = export const nextImageLoaderRegex =
/\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$/i /\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$/i
export async function resolveExternal(
appDir: string,
esmExternalsConfig: NextConfigComplete['experimental']['esmExternals'],
context: string,
request: string,
isEsmRequested: boolean,
getResolve: (
options: any
) => (
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
let preferEsmOptions =
esmExternals && isEsmRequested ? [true, false] : [false]
for (const preferEsm of preferEsmOptions) {
const resolve = getResolve(
preferEsm ? esmResolveOptions : nodeResolveOptions
)
// Resolve the import with the webpack provided context, this
// ensures we're resolving the correct version when multiple
// exist.
try {
;[res, isEsm] = await resolve(context, request)
} catch (err) {
res = null
}
if (!res) {
continue
}
// ESM externals can only be imported (and not required).
// Make an exception in loose mode.
if (!isEsmRequested && isEsm && !looseEsmExternals) {
continue
}
if (isLocalCallback) {
return { localRes: isLocalCallback(res) }
}
// Bundled Node.js code is relocated without its node_modules tree.
// This means we need to make sure its request resolves to the same
// package that'll be available at runtime. If it's not identical,
// we need to bundle the code (even if it _should_ be external).
if (baseResolveCheck) {
let baseRes: string | null
let baseIsEsm: boolean
try {
const baseResolve = getResolve(
isEsm ? baseEsmResolveOptions : baseResolveOptions
)
;[baseRes, baseIsEsm] = await baseResolve(appDir, request)
} catch (err) {
baseRes = null
baseIsEsm = false
}
// Same as above: if the package, when required from the root,
// would be different from what the real resolution would use, we
// cannot externalize it.
// if request is pointing to a symlink it could point to the the same file,
// the resolver will resolve symlinks so this is handled
if (baseRes !== res || isEsm !== baseIsEsm) {
res = null
continue
}
}
break
}
return { res, isEsm }
}
export default async function getBaseWebpackConfig( export default async function getBaseWebpackConfig(
dir: string, dir: string,
{ {
...@@ -695,8 +782,6 @@ export default async function getBaseWebpackConfig( ...@@ -695,8 +782,6 @@ export default async function getBaseWebpackConfig(
} }
const crossOrigin = config.crossOrigin const crossOrigin = config.crossOrigin
const esmExternals = !!config.experimental?.esmExternals
const looseEsmExternals = config.experimental?.esmExternals === 'loose' const looseEsmExternals = config.experimental?.esmExternals === 'loose'
async function handleExternals( async function handleExternals(
...@@ -712,7 +797,6 @@ export default async function getBaseWebpackConfig( ...@@ -712,7 +797,6 @@ export default async function getBaseWebpackConfig(
) { ) {
// We need to externalize internal requests for files intended to // We need to externalize internal requests for files intended to
// not be bundled. // not be bundled.
const isLocal: boolean = const isLocal: boolean =
request.startsWith('.') || request.startsWith('.') ||
// Always check for unix-style path, as webpack sometimes // Always check for unix-style path, as webpack sometimes
...@@ -742,42 +826,13 @@ export default async function getBaseWebpackConfig( ...@@ -742,42 +826,13 @@ export default async function getBaseWebpackConfig(
// ESM resolving options. // ESM resolving options.
const isEsmRequested = dependencyType === 'esm' const isEsmRequested = dependencyType === 'esm'
let res: string | null = null const isLocalCallback = (localRes: string) => {
let isEsm: boolean = false
let preferEsmOptions =
esmExternals && isEsmRequested ? [true, false] : [false]
for (const preferEsm of preferEsmOptions) {
const resolve = getResolve(
preferEsm ? NODE_ESM_RESOLVE_OPTIONS : NODE_RESOLVE_OPTIONS
)
// Resolve the import with the webpack provided context, this
// ensures we're resolving the correct version when multiple
// exist.
try {
;[res, isEsm] = await resolve(context, request)
} catch (err) {
res = null
}
if (!res) {
continue
}
// ESM externals can only be imported (and not required).
// Make an exception in loose mode.
if (!isEsmRequested && isEsm && !looseEsmExternals) {
continue
}
if (isLocal) {
// Makes sure dist/shared and dist/server are not bundled // Makes sure dist/shared and dist/server are not bundled
// we need to process shared `router/router` and `dynamic`, // we need to process shared `router/router` and `dynamic`,
// so that the DefinePlugin can inject process.env values // so that the DefinePlugin can inject process.env values
const isNextExternal = const isNextExternal =
/next[/\\]dist[/\\](shared|server)[/\\](?!lib[/\\](router[/\\]router|dynamic))/.test( /next[/\\]dist[/\\](shared|server)[/\\](?!lib[/\\](router[/\\]router|dynamic))/.test(
res localRes
) )
if (isNextExternal) { if (isNextExternal) {
...@@ -789,7 +844,7 @@ export default async function getBaseWebpackConfig( ...@@ -789,7 +844,7 @@ export default async function getBaseWebpackConfig(
.relative( .relative(
// Root of Next.js package: // Root of Next.js package:
path.join(__dirname, '..'), path.join(__dirname, '..'),
res localRes
) )
// Windows path normalization // Windows path normalization
.replace(/\\/g, '/') .replace(/\\/g, '/')
...@@ -802,34 +857,20 @@ export default async function getBaseWebpackConfig( ...@@ -802,34 +857,20 @@ export default async function getBaseWebpackConfig(
} }
} }
// Bundled Node.js code is relocated without its node_modules tree. const resolveResult = await resolveExternal(
// This means we need to make sure its request resolves to the same dir,
// package that'll be available at runtime. If it's not identical, config.experimental.esmExternals,
// we need to bundle the code (even if it _should_ be external). context,
let baseRes: string | null request,
let baseIsEsm: boolean isEsmRequested,
try { getResolve,
const baseResolve = getResolve( isLocal ? isLocalCallback : undefined
isEsm ? NODE_BASE_ESM_RESOLVE_OPTIONS : NODE_BASE_RESOLVE_OPTIONS
) )
;[baseRes, baseIsEsm] = await baseResolve(dir, request)
} catch (err) {
baseRes = null
baseIsEsm = false
}
// Same as above: if the package, when required from the root, if ('localRes' in resolveResult) {
// would be different from what the real resolution would use, we return resolveResult.localRes
// cannot externalize it.
// if request is pointing to a symlink it could point to the the same file,
// the resolver will resolve symlinks so this is handled
if (baseRes !== res || isEsm !== baseIsEsm) {
res = null
continue
}
break
} }
const { res, isEsm } = resolveResult
// If the request cannot be resolved we need to have // If the request cannot be resolved we need to have
// webpack "bundle" it so it surfaces the not found error. // webpack "bundle" it so it surfaces the not found error.
......
...@@ -13,6 +13,7 @@ import { ...@@ -13,6 +13,7 @@ import {
nextImageLoaderRegex, nextImageLoaderRegex,
NODE_ESM_RESOLVE_OPTIONS, NODE_ESM_RESOLVE_OPTIONS,
NODE_RESOLVE_OPTIONS, NODE_RESOLVE_OPTIONS,
resolveExternal,
} from '../../webpack-config' } from '../../webpack-config'
import { NextConfigComplete } from '../../../server/config-shared' import { NextConfigComplete } from '../../../server/config-shared'
...@@ -407,7 +408,7 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance { ...@@ -407,7 +408,7 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance {
request: string, request: string,
job: import('@vercel/nft/out/node-file-trace').Job job: import('@vercel/nft/out/node-file-trace').Job
) => ) =>
new Promise<string>((resolve, reject) => { new Promise<[string, boolean]>((resolve, reject) => {
const context = nodePath.dirname(parent) const context = nodePath.dirname(parent)
curResolver.resolve( curResolver.resolve(
...@@ -419,7 +420,7 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance { ...@@ -419,7 +420,7 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance {
missingDependencies: compilation.missingDependencies, missingDependencies: compilation.missingDependencies,
contextDependencies: compilation.contextDependencies, contextDependencies: compilation.contextDependencies,
}, },
async (err: any, result?: string | false, resContext?: any) => { async (err: any, result?, resContext?) => {
if (err) return reject(err) if (err) return reject(err)
if (!result) { if (!result) {
...@@ -472,7 +473,7 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance { ...@@ -472,7 +473,7 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance {
// we failed to resolve the package.json boundary, // we failed to resolve the package.json boundary,
// we don't block emitting the initial asset from this // we don't block emitting the initial asset from this
} }
resolve(result) resolve([result, options.dependencyType === 'esm'])
} }
) )
}) })
...@@ -480,14 +481,24 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance { ...@@ -480,14 +481,24 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance {
const CJS_RESOLVE_OPTIONS = { const CJS_RESOLVE_OPTIONS = {
...NODE_RESOLVE_OPTIONS, ...NODE_RESOLVE_OPTIONS,
fullySpecified: undefined,
modules: undefined, modules: undefined,
extensions: undefined, extensions: undefined,
} }
const BASE_CJS_RESOLVE_OPTIONS = {
...CJS_RESOLVE_OPTIONS,
alias: false,
}
const ESM_RESOLVE_OPTIONS = { const ESM_RESOLVE_OPTIONS = {
...NODE_ESM_RESOLVE_OPTIONS, ...NODE_ESM_RESOLVE_OPTIONS,
fullySpecified: undefined,
modules: undefined, modules: undefined,
extensions: undefined, extensions: undefined,
} }
const BASE_ESM_RESOLVE_OPTIONS = {
...ESM_RESOLVE_OPTIONS,
alias: false,
}
const doResolve = async ( const doResolve = async (
request: string, request: string,
...@@ -500,30 +511,25 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance { ...@@ -500,30 +511,25 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance {
`not resolving ${request} as this is handled by next-image-loader` `not resolving ${request} as this is handled by next-image-loader`
) )
} }
const context = nodePath.dirname(parent)
// When in esm externals mode, and using import, we resolve with // When in esm externals mode, and using import, we resolve with
// ESM resolving options. // ESM resolving options.
const esmExternals = this.esmExternals const { res } = await resolveExternal(
const looseEsmExternals = this.esmExternals === 'loose' this.appDir,
const preferEsm = esmExternals && isEsmRequested this.esmExternals,
const resolve = getResolve( context,
preferEsm ? ESM_RESOLVE_OPTIONS : CJS_RESOLVE_OPTIONS request,
) isEsmRequested,
// Resolve the import with the webpack provided context, this (options) => (_: string, resRequest: string) => {
// ensures we're resolving the correct version when multiple return getResolve(options)(parent, resRequest, job)
// exist. },
let res: string = '' undefined,
try { undefined,
res = await resolve(parent, request, job) ESM_RESOLVE_OPTIONS,
} catch (_) {} CJS_RESOLVE_OPTIONS,
BASE_ESM_RESOLVE_OPTIONS,
// If resolving fails, and we can use an alternative way BASE_CJS_RESOLVE_OPTIONS
// try the alternative resolving options.
if (!res && (isEsmRequested || looseEsmExternals)) {
const resolveAlternative = getResolve(
preferEsm ? CJS_RESOLVE_OPTIONS : ESM_RESOLVE_OPTIONS
) )
res = await resolveAlternative(parent, request, job)
}
if (!res) { if (!res) {
throw new Error(`failed to resolve ${request} from ${parent}`) throw new Error(`failed to resolve ${request} from ${parent}`)
......
...@@ -217,12 +217,24 @@ describe('Production Usage', () => { ...@@ -217,12 +217,24 @@ describe('Production Usage', () => {
expect(version).toBe(1) expect(version).toBe(1)
expect( expect(
check.tests.every((item) => files.some((file) => item.test(file))) check.tests.every((item) => {
if (files.some((file) => item.test(file))) {
return true
}
console.error(`Failed to find ${item} in`, files)
return false
})
).toBe(true) ).toBe(true)
if (sep === '/') { if (sep === '/') {
expect( expect(
check.notTests.some((item) => files.some((file) => item.test(file))) check.notTests.some((item) => {
if (files.some((file) => item.test(file))) {
console.error(`Found unexpected ${item} in`, files)
return true
}
return false
})
).toBe(false) ).toBe(false)
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment