Unverified Commit 06207d84 authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

babel-loader: Avoid calling expensive `isReactCompilerRequired` check when we…

babel-loader: Avoid calling expensive `isReactCompilerRequired` check when we must run Babel anyways (#84103)

This check uses SWC, so it's expensive.

This can be worth it if it lets us avoid running Babel entirely, but if you've got a custom babel config, we're forced to run Babel anyways, so it's probably not worth it in that case.
parent d6125822
...@@ -64,6 +64,18 @@ interface CharacteristicsGermaneToCaching { ...@@ -64,6 +64,18 @@ interface CharacteristicsGermaneToCaching {
configFilePath: string | undefined configFilePath: string | undefined
} }
function shouldSkipBabel(
transformMode: 'standalone' | 'default',
configFilePath: string | undefined,
hasReactCompiler: boolean
) {
return (
transformMode === 'standalone' &&
configFilePath == null &&
!hasReactCompiler
)
}
const fileExtensionRegex = /\.([a-z]+)$/ const fileExtensionRegex = /\.([a-z]+)$/
async function getCacheCharacteristics( async function getCacheCharacteristics(
loaderOptions: NextBabelLoaderOptions, loaderOptions: NextBabelLoaderOptions,
...@@ -91,19 +103,39 @@ async function getCacheCharacteristics( ...@@ -91,19 +103,39 @@ async function getCacheCharacteristics(
const hasModuleExports = source.indexOf('module.exports') !== -1 const hasModuleExports = source.indexOf('module.exports') !== -1
const fileExt = fileExtensionRegex.exec(filename)?.[1] || 'unknown' const fileExt = fileExtensionRegex.exec(filename)?.[1] || 'unknown'
let {
reactCompilerPlugins,
reactCompilerExclude,
configFile: configFilePath,
transformMode,
} = loaderOptions
// Compute `hasReactCompiler` as part of the cache characteristics / key, // Compute `hasReactCompiler` as part of the cache characteristics / key,
// rather than inside of `getFreshConfig`: // rather than inside of `getFreshConfig`:
// - `isReactCompilerRequired` depends on the file contents // - `isReactCompilerRequired` depends on the file contents
// - `node_modules` and `reactCompilerExclude` depend on the file path, which // - `node_modules` and `reactCompilerExclude` depend on the file path, which
// isn't part of the cache characteristics // isn't part of the cache characteristics
let { reactCompilerPlugins, reactCompilerExclude } = loaderOptions let hasReactCompiler =
reactCompilerPlugins ??= [] reactCompilerPlugins != null &&
const hasReactCompiler =
reactCompilerPlugins.length !== 0 && reactCompilerPlugins.length !== 0 &&
!loaderOptions.isServer && !loaderOptions.isServer &&
!/[/\\]node_modules[/\\]/.test(filename) && !/[/\\]node_modules[/\\]/.test(filename) &&
!reactCompilerExclude?.(filename) && // Assumption: `reactCompilerExclude` is cheap because it should only
(await isReactCompilerRequired(filename)) // operate on the file path and *not* the file contents (it's sync)
!reactCompilerExclude?.(filename)
// `isReactCompilerRequired` is expensive to run (parses/visits with SWC), so
// only run it if there's a good chance we might be able to skip calling Babel
// entirely (speculatively call `shouldSkipBabel`).
//
// Otherwise, we can let react compiler handle this logic for us. It should
// behave equivalently.
if (
hasReactCompiler &&
shouldSkipBabel(transformMode, configFilePath, /*hasReactCompiler*/ false)
) {
hasReactCompiler &&= await isReactCompilerRequired(filename)
}
return { return {
isStandalone, isStandalone,
...@@ -113,7 +145,7 @@ async function getCacheCharacteristics( ...@@ -113,7 +145,7 @@ async function getCacheCharacteristics(
hasModuleExports, hasModuleExports,
hasReactCompiler, hasReactCompiler,
fileExt, fileExt,
configFilePath: loaderOptions.configFile, configFilePath,
} }
} }
...@@ -336,8 +368,9 @@ async function getFreshConfig( ...@@ -336,8 +368,9 @@ async function getFreshConfig(
const { hasReactCompiler, configFilePath, fileExt } = cacheCharacteristics const { hasReactCompiler, configFilePath, fileExt } = cacheCharacteristics
let customConfig = configFilePath && getCustomBabelConfig(configFilePath) let customConfig = configFilePath && getCustomBabelConfig(configFilePath)
if (transformMode === 'standalone' && !customConfig && !hasReactCompiler) { if (shouldSkipBabel(transformMode, configFilePath, hasReactCompiler)) {
// Optimization: There's nothing useful to do, bail out and skip babel on this file // Optimization: There's nothing useful to do, bail out and skip babel on
// this file
return null return null
} }
......
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