Skip to content

Commit

Permalink
tweak rules
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Oct 11, 2022
1 parent 4ff2cb7 commit 91ce27c
Showing 1 changed file with 62 additions and 42 deletions.
104 changes: 62 additions & 42 deletions packages/next/build/webpack-config.ts
Expand Up @@ -877,7 +877,6 @@ export default async function getBaseWebpackConfig(
'react-dom/server$': 'next/dist/compiled/react-dom/server',
'react-dom/server.browser$':
'next/dist/compiled/react-dom/server.browser',
react$: 'next/dist/compiled/react',
}
: undefined),

Expand Down Expand Up @@ -1042,7 +1041,7 @@ export default async function getBaseWebpackConfig(
// Special internal modules that must be bundled for Server Components.
if (layer === WEBPACK_LAYERS.server) {
if (
request.startsWith('next/dist/compiled/react') ||
request.includes('next/dist/compiled/react') ||
request === 'react' ||
request === 'react/jsx-runtime' ||
request ===
Expand Down Expand Up @@ -1315,17 +1314,16 @@ export default async function getBaseWebpackConfig(
// For the edge runtime, we have to bundle all dependencies inside without dynamic `require`s.
// To make some dependencies like `react` to be shared between entrypoints, we use a special
// cache group here even under dev mode.
const edgeRSCCacheGroups = undefined
// hasServerComponents
// ? {
// rscDeps: {
// enforce: true,
// name: 'rsc-runtime-deps',
// filename: 'rsc-runtime-deps.js',
// test: rscSharedRegex,
// },
// }
// : undefined
const edgeRSCCacheGroups = hasServerComponents // undefined
? {
rscDeps: {
enforce: true,
name: 'rsc-runtime-deps',
filename: 'rsc-runtime-deps.js',
test: rscSharedRegex,
},
}
: undefined
if (isEdgeServer && edgeRSCCacheGroups) {
return {
cacheGroups: edgeRSCCacheGroups,
Expand Down Expand Up @@ -1542,19 +1540,23 @@ export default async function getBaseWebpackConfig(
},
module: {
rules: [
...(hasAppDir && !isClient && !isEdgeServer
...(hasAppDir && !isClient
? [
{
/*{
issuerLayer: WEBPACK_LAYERS.server,
test: (req: string) => {
// If it's not a source code file, or has been opted out of
// bundling, don't resolve it.
if (req.includes('react')) {
console.log('req', req)
}
if (
!codeCondition.test.test(req) ||
isResourceInPackages(
req,
config.experimental.serverComponentsExternalPackages
)
) || req.includes('next/dist/compiled/react')
) {
return false
}
Expand All @@ -1573,20 +1575,20 @@ export default async function getBaseWebpackConfig(
// :
{
conditionNames: ['react-server', 'node', 'require'],
alias: {
react: require.resolve(
'next/dist/compiled/react/react.shared-subset'
),
// 'react-dom': 'next/dist/compiled/react-dom',
},
// alias: {
// react: require.resolve(
// 'next/dist/compiled/react/react.shared-subset'
// ),
// // 'react-dom': 'next/dist/compiled/react-dom',
// },
// // If missing the alias override here, the default alias will be used which aliases
// // react to the direct file path, not the package name. In that case the condition
// // will be ignored completely.
// react: 'react',
// 'react-dom': 'react-dom',
// },
},
},
},*/
]
: []),

Expand Down Expand Up @@ -1618,14 +1620,31 @@ export default async function getBaseWebpackConfig(
},
},
{
test: codeCondition.test,
// include: [appDir, dir, NEXT_PROJECT_ROOT_DIST, /node_modules/],
resolve: {
alias: {
'react-dom': 'next/dist/compiled/react-dom',
react: 'next/dist/compiled/react',
oneOf: [
{
test(f: string) {
if (f.includes('react')) console.log('server-layer', f)
return codeCondition.test.test(f)
},
issuerLayer: WEBPACK_LAYERS.server,
resolve: {
conditionNames: ['react-server', 'node', 'require'],
alias: {
['react']: require.resolve(
'next/dist/compiled/react/react.shared-subset'
),
},
},
},
},
{
test: codeCondition.test,
resolve: {
alias: {
['react']: require.resolve('next/dist/compiled/react'),
},
},
},
],
},
]
: []),
Expand All @@ -1635,9 +1654,9 @@ export default async function getBaseWebpackConfig(
{
test: codeCondition.test,
include: [
dir,
appDir,
// To let the internal client components passing through flight loader
NEXT_PROJECT_ROOT_DIST,
// NEXT_PROJECT_ROOT_DIST,
],
issuerLayer: WEBPACK_LAYERS.server,
use: {
Expand All @@ -1646,16 +1665,17 @@ export default async function getBaseWebpackConfig(
},
]
: []),
// ...(hasServerComponents && isEdgeServer
// ? [
// // Move shared dependencies from sc_server and sc_client into the
// // same layer.
// {
// test: rscSharedRegex,
// layer: WEBPACK_LAYERS.rscShared,
// },
// ]
// : []),
...(hasServerComponents && isEdgeServer
? [
// Move shared dependencies from sc_server and sc_client into the
// same layer.
{
test: codeCondition.test,
include: [NEXT_PROJECT_ROOT_DIST],
layer: WEBPACK_LAYERS.rscShared,
},
]
: []),
{
test: /\.(js|cjs|mjs)$/,
issuerLayer: WEBPACK_LAYERS.api,
Expand Down

0 comments on commit 91ce27c

Please sign in to comment.