Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include resolved external package dirs #41706

Merged
merged 3 commits into from Oct 24, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 39 additions & 6 deletions packages/next/build/webpack-config.ts
Expand Up @@ -120,11 +120,19 @@ function errorIfEnvConflicted(config: NextConfigComplete, key: string) {
}
}

function isResourceInPackages(resource: string, packageNames?: string[]) {
function isResourceInPackages(
resource: string,
packageNames?: string[],
packageDirMapping?: Map<string, string>
) {
return packageNames?.some((p: string) =>
resource.includes(
path.sep + pathJoin('node_modules', p.replace(/\//g, path.sep)) + path.sep
)
packageDirMapping && packageDirMapping.has(p)
? resource.startsWith(packageDirMapping.get(p)! + path.sep)
: resource.includes(
path.sep +
pathJoin('node_modules', p.replace(/\//g, path.sep)) +
path.sep
)
)
}

Expand Down Expand Up @@ -1028,6 +1036,8 @@ export default async function getBaseWebpackConfig(
...(config.experimental.serverComponentsExternalPackages || [])
)

let resolvedExternalPackageDirs: Map<string, string>

async function handleExternals(
context: string,
request: string,
Expand Down Expand Up @@ -1194,9 +1204,29 @@ export default async function getBaseWebpackConfig(

// If a package should be transpiled by Next.js, we skip making it external.
// It doesn't matter what the extension is, as we'll transpile it anyway.
if (config.experimental.transpilePackages && !resolvedExternalPackageDirs) {
resolvedExternalPackageDirs = new Map()
// We need to reoslve all the external package dirs initially.
for (const pkg of config.experimental.transpilePackages) {
const pkgRes = await resolveExternal(
dir,
config.experimental.esmExternals,
context,
pkg + '/package.json',
isEsmRequested,
getResolve,
isLocal ? isLocalCallback : undefined
)
if (pkgRes.res) {
resolvedExternalPackageDirs.set(pkg, path.dirname(pkgRes.res))
}
}
}

const shouldBeBundled = isResourceInPackages(
res,
config.experimental.transpilePackages
config.experimental.transpilePackages,
resolvedExternalPackageDirs
)

if (/node_modules[/\\].*\.[mc]?js$/.test(res)) {
Expand Down Expand Up @@ -1240,9 +1270,12 @@ export default async function getBaseWebpackConfig(
// Default behavior: bundle the code!
}

const shouldIncludeExternalDirs =
config.experimental.externalDir || !!config.experimental.transpilePackages

const codeCondition = {
test: /\.(tsx|ts|js|cjs|mjs|jsx)$/,
...(config.experimental.externalDir
...(shouldIncludeExternalDirs
? // Allowing importing TS/TSX files from outside of the root dir.
{}
: { include: [dir, ...babelIncludeRegexes] }),
Expand Down