Skip to content

Commit

Permalink
Use require.resolve to detect "framework" packages
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottsj committed Jan 17, 2021
1 parent cfa9326 commit 3740547
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions packages/next/build/webpack-config.ts
Expand Up @@ -426,6 +426,27 @@ export default async function getBaseWebpackConfig(
)
}

const getPackagePath = (name: string, relativeToPath: string) => {
const packageJsonPath = require.resolve(`${name}/package.json`, {
paths: [relativeToPath],
})
return path.dirname(packageJsonPath)
}

// Packages which will be split into the 'framework' chunk.
// Only top-level packages are included, e.g. nested copies like
// 'node_modules/react-scroll-parallax/node_modules/prop-types' are not included.
const topLevelFrameworkPaths = [
getPackagePath('react', dir),
getPackagePath('react-dom', dir),
getPackagePath('scheduler', require.resolve('react-dom', { paths: [dir] })),
getPackagePath('prop-types', require.resolve('next', { paths: [dir] })),
getPackagePath(
'use-subscription',
require.resolve('next', { paths: [dir] })
),
]

// Contains various versions of the Webpack SplitChunksPlugin used in different build types
const splitChunksConfigs: {
[propName: string]: webpack.Options.SplitChunksOptions
Expand All @@ -448,10 +469,14 @@ export default async function getBaseWebpackConfig(
framework: {
chunks: 'all',
name: 'framework',
// This regex ignores nested copies of framework libraries so they're
// bundled with their issuer.
// https://github.com/vercel/next.js/pull/9012
test: /(?<!node_modules.*)[\\/]node_modules[\\/](react|react-dom|scheduler|prop-types|use-subscription)[\\/]/,
test(module) {
if (!module.resource) {
return false
}
return topLevelFrameworkPaths.some((packagePath) =>
module.resource.startsWith(packagePath)
)
},
priority: 40,
// Don't let webpack eliminate this chunk (prevents this chunk from
// becoming a part of the commons chunk)
Expand Down

0 comments on commit 3740547

Please sign in to comment.