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

Fixes #31240: Adding a recursive addPackagePath function in webpack-config #31264

Merged
merged 21 commits into from Feb 6, 2022
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
6b5b5c8
adding try-catch block to getPackagePath function
neeraj3029 Nov 10, 2021
431d4ff
Merge branch 'canary' into neeraj3029try-catch-getPackagePath
neeraj3029 Nov 16, 2021
08cbd15
adding-dep-paths-recursively
neeraj3029 Nov 19, 2021
47d2a44
adding-types-to-addPackagePath
neeraj3029 Nov 19, 2021
c19053a
removing-extra-line
neeraj3029 Nov 19, 2021
52fa4e0
replacing-_path-with-path
neeraj3029 Nov 19, 2021
735a855
Update webpack-config.ts
neeraj3029 Nov 19, 2021
e8fcd80
fixing-azure-build-error
neeraj3029 Nov 19, 2021
4dfaeab
adding-type-to-set
neeraj3029 Nov 19, 2021
3fee738
Merge branch 'canary' into neeraj3029try-catch-getPackagePath
neeraj3029 Nov 19, 2021
58a4dc0
Merge branch 'canary' into neeraj3029try-catch-getPackagePath
kodiakhq[bot] Nov 25, 2021
fbb3fed
Merge branch 'canary' into neeraj3029try-catch-getPackagePath
sokra Nov 25, 2021
4e56e56
lint-fix
neeraj3029 Nov 27, 2021
2efbb4c
adding-set-to-avoid-multiple-failed-resolution-attempts
neeraj3029 Nov 27, 2021
0fd45f3
Merge branch 'canary' into neeraj3029try-catch-getPackagePath
neeraj3029 Nov 27, 2021
6143b67
Merge branch 'canary' into neeraj3029try-catch-getPackagePath
neeraj3029 Dec 5, 2021
1e4d737
Merge branch 'canary' into neeraj3029try-catch-getPackagePath
neeraj3029 Dec 30, 2021
9bd64a0
Merge branch 'canary' into neeraj3029try-catch-getPackagePath
neeraj3029 Jan 24, 2022
321fef6
Merge branch 'canary' into neeraj3029try-catch-getPackagePath
ijjk Feb 6, 2022
9f4db56
simplify a bit and remove next from paths to match previous
ijjk Feb 6, 2022
700135d
remove un-needed Array.from
ijjk Feb 6, 2022
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
64 changes: 38 additions & 26 deletions packages/next/build/webpack-config.ts
Expand Up @@ -685,35 +685,47 @@ export default async function getBaseWebpackConfig(
)
}

const getPackagePath = (name: string, relativeToPath: string) => {
const packageJsonPath = require.resolve(`${name}/package.json`, {
paths: [relativeToPath],
})
// Include a trailing slash so that a `.startsWith(packagePath)` check avoids false positives
// when one package name starts with the full name of a different package.
// For example:
// "node_modules/react-slider".startsWith("node_modules/react") // true
// "node_modules/react-slider".startsWith("node_modules/react/") // false
return path.join(packageJsonPath, '../')
}

// Packages which will be split into the 'framework' chunk.
// Only top-level packages are included, e.g. nested copies like
// 'node_modules/meow/node_modules/object-assign' are not included.
const topLevelFrameworkPaths = [
getPackagePath('react', dir),
getPackagePath('react-dom', dir),
getPackagePath('scheduler', require.resolve('react-dom', { paths: [dir] })),
getPackagePath('object-assign', require.resolve('react', { paths: [dir] })),
getPackagePath(
'object-assign',
require.resolve('react-dom', { paths: [dir] })
),
getPackagePath(
'use-subscription',
require.resolve('next', { paths: [dir] })
),
]
const topLevelFrameworkPaths: 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],
})

// Include a trailing slash so that a `.startsWith(packagePath)` check avoids false positives
// when one package name starts with the full name of a different package.
// For example:
// "node_modules/react-slider".startsWith("node_modules/react") // true
// "node_modules/react-slider".startsWith("node_modules/react/") // false
const directory = path.join(packageJsonPath, '../')

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

const dependencies = require(packageJsonPath).dependencies || {}
for (const name of Object.keys(dependencies)) {
addPackagePath(name, directory)
}
} catch (_) {
// don't error on failing to resolve framework packages
}
}

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

// Select appropriate SplitChunksPlugin config for this build
const splitChunksConfig: webpack.Options.SplitChunksOptions | false = dev
Expand Down