Commit 4518c1ec authored by JJ Kasper's avatar JJ Kasper

Reduce memory/cache overhead from over loader processing #62005

parent 1ad65483
...@@ -103,7 +103,7 @@ if (parseInt(React.version) < 18) { ...@@ -103,7 +103,7 @@ if (parseInt(React.version) < 18) {
throw new Error('Next.js requires react >= 18.2.0 to be installed.') throw new Error('Next.js requires react >= 18.2.0 to be installed.')
} }
const babelIncludeRegexes: RegExp[] = [ export const babelIncludeRegexes: RegExp[] = [
/next[\\/]dist[\\/](esm[\\/])?shared[\\/]lib/, /next[\\/]dist[\\/](esm[\\/])?shared[\\/]lib/,
/next[\\/]dist[\\/](esm[\\/])?client/, /next[\\/]dist[\\/](esm[\\/])?client/,
/next[\\/]dist[\\/](esm[\\/])?pages/, /next[\\/]dist[\\/](esm[\\/])?pages/,
...@@ -455,6 +455,7 @@ export default async function getBaseWebpackConfig( ...@@ -455,6 +455,7 @@ export default async function getBaseWebpackConfig(
jsConfig, jsConfig,
supportedBrowsers, supportedBrowsers,
swcCacheDir: path.join(dir, config?.distDir ?? '.next', 'cache', 'swc'), swcCacheDir: path.join(dir, config?.distDir ?? '.next', 'cache', 'swc'),
transpilePackages: config.transpilePackages,
...extraOptions, ...extraOptions,
} satisfies SWCLoaderOptions, } satisfies SWCLoaderOptions,
} }
......
...@@ -31,6 +31,22 @@ import type { WebpackLayerName } from '../../../lib/constants' ...@@ -31,6 +31,22 @@ import type { WebpackLayerName } from '../../../lib/constants'
import { isWasm, transform } from '../../swc' import { isWasm, transform } from '../../swc'
import { getLoaderSWCOptions } from '../../swc/options' import { getLoaderSWCOptions } from '../../swc/options'
import path, { isAbsolute } from 'path' import path, { isAbsolute } from 'path'
import { babelIncludeRegexes } from '../../webpack-config'
import { isResourceInPackages } from '../../handle-externals'
const maybeExclude = (
excludePath: string,
transpilePackages: string[]
): boolean => {
if (babelIncludeRegexes.some((r) => r.test(excludePath))) {
return false
}
const shouldBeBundled = isResourceInPackages(excludePath, transpilePackages)
if (shouldBeBundled) return false
return excludePath.includes('node_modules')
}
export interface SWCLoaderOptions { export interface SWCLoaderOptions {
rootDir: string rootDir: string
...@@ -46,8 +62,14 @@ export interface SWCLoaderOptions { ...@@ -46,8 +62,14 @@ export interface SWCLoaderOptions {
serverComponents?: boolean serverComponents?: boolean
bundleLayer?: WebpackLayerName bundleLayer?: WebpackLayerName
esm?: boolean esm?: boolean
transpilePackages?: string[]
} }
// these are exact code conditions checked
// for to force transpiling a `node_module`
const FORCE_TRANSPILE_CONDITIONS =
/(next\/font|next\/dynamic|use server|use client)/
async function loaderTransform( async function loaderTransform(
this: any, this: any,
parentTrace: any, parentTrace: any,
...@@ -58,6 +80,20 @@ async function loaderTransform( ...@@ -58,6 +80,20 @@ async function loaderTransform(
const filename = this.resourcePath const filename = this.resourcePath
let loaderOptions: SWCLoaderOptions = this.getOptions() || {} let loaderOptions: SWCLoaderOptions = this.getOptions() || {}
const shouldMaybeExclude = maybeExclude(
filename,
loaderOptions.transpilePackages || []
)
if (shouldMaybeExclude) {
if (!source) {
throw new Error(`Invariant might be excluded but missing source`)
}
if (!FORCE_TRANSPILE_CONDITIONS.test(source)) {
return [source, inputSourceMap]
}
}
const { const {
isServer, isServer,
...@@ -150,8 +186,17 @@ const EXCLUDED_PATHS = ...@@ -150,8 +186,17 @@ const EXCLUDED_PATHS =
export function pitch(this: any) { export function pitch(this: any) {
const callback = this.async() const callback = this.async()
let loaderOptions: SWCLoaderOptions = this.getOptions() || {}
const shouldMaybeExclude = maybeExclude(
this.resourcePath,
loaderOptions.transpilePackages || []
)
;(async () => { ;(async () => {
if ( if (
// if it might be excluded/no-op we can't use pitch loader
!shouldMaybeExclude &&
// TODO: investigate swc file reading in PnP mode? // TODO: investigate swc file reading in PnP mode?
!process.versions.pnp && !process.versions.pnp &&
!EXCLUDED_PATHS.test(this.resourcePath) && !EXCLUDED_PATHS.test(this.resourcePath) &&
......
...@@ -98,16 +98,6 @@ export class TerserPlugin { ...@@ -98,16 +98,6 @@ export class TerserPlugin {
return false return false
} }
// don't minify _middleware as it can break in some cases
// and doesn't provide too much of a benefit as it's server-side
if (
name.match(
/(edge-runtime-webpack\.js|edge-chunks|middleware\.js$)/
)
) {
return false
}
const { info } = res const { info } = res
// Skip double minimize assets from child compilation // Skip double minimize assets from child compilation
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment