Skip to content

Commit

Permalink
Reduce memory/cache overhead from over loader processing #62005
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Mar 2, 2024
1 parent 1ad6548 commit 4518c1e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
3 changes: 2 additions & 1 deletion packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ if (parseInt(React.version) < 18) {
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[\\/])?client/,
/next[\\/]dist[\\/](esm[\\/])?pages/,
Expand Down Expand Up @@ -455,6 +455,7 @@ export default async function getBaseWebpackConfig(
jsConfig,
supportedBrowsers,
swcCacheDir: path.join(dir, config?.distDir ?? '.next', 'cache', 'swc'),
transpilePackages: config.transpilePackages,
...extraOptions,
} satisfies SWCLoaderOptions,
}
Expand Down
45 changes: 45 additions & 0 deletions packages/next/src/build/webpack/loaders/next-swc-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ import type { WebpackLayerName } from '../../../lib/constants'
import { isWasm, transform } from '../../swc'
import { getLoaderSWCOptions } from '../../swc/options'
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 {
rootDir: string
Expand All @@ -46,8 +62,14 @@ export interface SWCLoaderOptions {
serverComponents?: boolean
bundleLayer?: WebpackLayerName
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(
this: any,
parentTrace: any,
Expand All @@ -58,6 +80,20 @@ async function loaderTransform(
const filename = this.resourcePath

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 {
isServer,
Expand Down Expand Up @@ -150,8 +186,17 @@ const EXCLUDED_PATHS =

export function pitch(this: any) {
const callback = this.async()
let loaderOptions: SWCLoaderOptions = this.getOptions() || {}

const shouldMaybeExclude = maybeExclude(
this.resourcePath,
loaderOptions.transpilePackages || []
)

;(async () => {
if (
// if it might be excluded/no-op we can't use pitch loader
!shouldMaybeExclude &&
// TODO: investigate swc file reading in PnP mode?
!process.versions.pnp &&
!EXCLUDED_PATHS.test(this.resourcePath) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,6 @@ export class TerserPlugin {
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

// Skip double minimize assets from child compilation
Expand Down

0 comments on commit 4518c1e

Please sign in to comment.