Skip to content

Commit

Permalink
simplify a bit and remove next from paths to match previous
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Feb 6, 2022
1 parent 321fef6 commit 9f4db56
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions packages/next/build/webpack-config.ts
Expand Up @@ -689,11 +689,16 @@ export default async function getBaseWebpackConfig(
// Only top-level packages are included, e.g. nested copies like
// 'node_modules/meow/node_modules/object-assign' are not included.
const topLevelFrameworkPaths = new Set<string>()
const unresolvedPackages = new Set<{ package: string; path: string }>()
const visitedFrameworkPackages = new Set<string>()

// Adds package-paths of dependencies recursively
const addPackagePath = (packageName: string, relativeToPath: string) => {
try {
if (visitedFrameworkPackages.has(packageName)) {
return
}
visitedFrameworkPackages.add(packageName)

const packageJsonPath = require.resolve(`${packageName}/package.json`, {
paths: [relativeToPath],
})
Expand All @@ -707,33 +712,18 @@ export default async function getBaseWebpackConfig(

// Returning from the function in case the directory has already been added and traversed
if (topLevelFrameworkPaths.has(directory)) return

topLevelFrameworkPaths.add(directory)

const dependencies = require(packageJsonPath).dependencies || {}
for (const name of Object.keys(dependencies)) {
// Avoiding attempts to resolve packages that couldn't be resolved relative to a path
if (
unresolvedPackages.has({
package: name,
path: directory,
})
) {
continue
}

addPackagePath(name, directory)
}
} catch {
// Remembering the packages that can't be resolved, to avoid future resolution attempts.
unresolvedPackages.add({
package: packageName,
path: relativeToPath,
})
} catch (_) {
// don't error on failing to resolve framework packages
}
}

for (const packageName of ['react', 'react-dom', 'next']) {
for (const packageName of ['react', 'react-dom']) {
addPackagePath(packageName, dir)
}

Expand Down

0 comments on commit 9f4db56

Please sign in to comment.