Skip to content

Commit

Permalink
process only pages and imported modules
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Apr 7, 2022
1 parent 1f3af2d commit 2012286
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
11 changes: 9 additions & 2 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,12 @@ export default async function getBaseWebpackConfig(
},
}

const rscCodeCondition = {
test: codeCondition.test,
// only apply to the pages as the begin process of rsc loaders
include: [dir, /next[\\/]dist[\\/]pages/],
}

let webpackConfig: webpack.Configuration = {
parallelism: Number(process.env.NEXT_WEBPACK_PARALLELISM) || undefined,
externals: targetWeb
Expand Down Expand Up @@ -1202,7 +1208,8 @@ export default async function getBaseWebpackConfig(
? [
// RSC server compilation loaders
{
...codeCondition,
...rscCodeCondition,
test: serverComponentsRegex,
use: {
loader: 'next-flight-server-loader',
options: {
Expand All @@ -1214,7 +1221,7 @@ export default async function getBaseWebpackConfig(
: [
// RSC client compilation loaders
{
...codeCondition,
...rscCodeCondition,
test: serverComponentsRegex,
use: {
loader: 'next-flight-server-loader',
Expand Down
29 changes: 6 additions & 23 deletions packages/next/build/webpack/loaders/next-flight-server-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,16 @@ async function parseModuleInfo({
)
transformedSource += importDeclarations
transformedSource += JSON.stringify(serverImportSource)
imports.push(importSource)
}
} else {
// For the client compilation, we skip all modules imports but
// always keep client components in the bundle. All client components
// always keep client/shared components in the bundle. All client components
// have to be imported from either server or client components.
if (
!(
isClientComponent(importSource) || isServerComponent(importSource)
)
isServerComponent(importSource) ||
hasFlightLoader(importSource, 'server')
) {
// Keep the relative imports but not the absolute imports
// Treated absolute imports as externals
if (
!hasFlightLoader(importSource, 'server') &&
!hasFlightLoader(importSource, 'client')
) {
imports.push(importSource)
}
continue
}

Expand Down Expand Up @@ -173,7 +165,6 @@ export default async function transformSource(
throw new Error('Expected source to have been transformed to a string.')
}

const isProcessed = source.includes('__next_rsc__')
const isServerComponent = createServerComponentFilter(extensions)
const isClientComponent = createClientComponentFilter(extensions)
const hasAppliedFlightServerLoader = this.loaders.some((loader: any) => {
Expand Down Expand Up @@ -221,11 +212,7 @@ export default async function transformSource(
_: () => {
${imports.map((source) => `require('${source}');`).join('\n')}
},
server: ${
isServerComponent(resourcePath) || hasAppliedFlightServerLoader
? 'true'
: 'false'
}
server: ${isServerComponent(resourcePath) ? 'true' : 'false'}
}`,
}

Expand All @@ -246,10 +233,6 @@ export default async function transformSource(
}
}

const output =
transformedSource +
'\n' +
(isProcessed ? '' : buildExports(rscExports, isEsm))

const output = transformedSource + '\n' + buildExports(rscExports, isEsm)
return output
}

0 comments on commit 2012286

Please sign in to comment.