Unverified Commit 704c1531 authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

babel-loader: Fix a few issues with config caching (#83973)

- `isReactCompilerRequired` needs to be called in `getCacheCharacteristics`, not in `getFreshConfig` because it depends on the file contents, so we don't want to cache the result of `isReactCompilerRequired`.
- Moving react compiler detection outside of the cache lets us avoid using the full file path for a cache key (which negates much of the benefit of the cache).
- `getFreshConfig` shouldn't use the real file path or source map, those should always be injected later.
- It doesn't look like we were updating `inputSourceMap` on cached config objects (!?!?)
parent a12283e5
...@@ -59,27 +59,26 @@ interface CharacteristicsGermaneToCaching { ...@@ -59,27 +59,26 @@ interface CharacteristicsGermaneToCaching {
isPageFile: boolean | undefined isPageFile: boolean | undefined
isNextDist: boolean isNextDist: boolean
hasModuleExports: boolean hasModuleExports: boolean
fileNameOrExt: string hasReactCompiler: boolean
fileExt: string
configFilePath: string | undefined configFilePath: string | undefined
} }
const fileExtensionRegex = /\.([a-z]+)$/ const fileExtensionRegex = /\.([a-z]+)$/
function getCacheCharacteristics( async function getCacheCharacteristics(
loaderOptions: NextBabelLoaderOptions, loaderOptions: NextBabelLoaderOptions,
source: string, source: string,
filename: string filename: string
): CharacteristicsGermaneToCaching { ): Promise<CharacteristicsGermaneToCaching> {
let isStandalone, isServer, pagesDir, fileNameOrExt let isStandalone, isServer, pagesDir
switch (loaderOptions.transformMode) { switch (loaderOptions.transformMode) {
case 'default': case 'default':
isStandalone = false isStandalone = false
isServer = loaderOptions.isServer isServer = loaderOptions.isServer
pagesDir = loaderOptions.pagesDir pagesDir = loaderOptions.pagesDir
fileNameOrExt = fileExtensionRegex.exec(filename)?.[1] || 'unknown'
break break
case 'standalone': case 'standalone':
isStandalone = true isStandalone = true
fileNameOrExt = filename
break break
default: default:
throw new Error( throw new Error(
...@@ -90,6 +89,20 @@ function getCacheCharacteristics( ...@@ -90,6 +89,20 @@ function getCacheCharacteristics(
const isPageFile = pagesDir != null && filename.startsWith(pagesDir) const isPageFile = pagesDir != null && filename.startsWith(pagesDir)
const isNextDist = nextDistPath.test(filename) const isNextDist = nextDistPath.test(filename)
const hasModuleExports = source.indexOf('module.exports') !== -1 const hasModuleExports = source.indexOf('module.exports') !== -1
const fileExt = fileExtensionRegex.exec(filename)?.[1] || 'unknown'
// Compute `hasReactCompiler` as part of the cache characteristics / key,
// rather than inside of `getFreshConfig`:
// - `isReactCompilerRequired` depends on the file contents
// - `node_modules` and `reactCompilerExclude` depend on the file path, which
// isn't part of the cache characteristics
let { reactCompilerPlugins, reactCompilerExclude } = loaderOptions
reactCompilerPlugins ??= []
const hasReactCompiler =
reactCompilerPlugins.length !== 0 &&
!/[/\\]node_modules[/\\]/.test(filename) &&
!reactCompilerExclude?.(filename) &&
(await isReactCompilerRequired(filename))
return { return {
isStandalone, isStandalone,
...@@ -97,7 +110,8 @@ function getCacheCharacteristics( ...@@ -97,7 +110,8 @@ function getCacheCharacteristics(
isPageFile, isPageFile,
isNextDist, isNextDist,
hasModuleExports, hasModuleExports,
fileNameOrExt, hasReactCompiler,
fileExt,
configFilePath: loaderOptions.configFile, configFilePath: loaderOptions.configFile,
} }
} }
...@@ -306,43 +320,21 @@ function checkCustomBabelConfigDeprecation( ...@@ -306,43 +320,21 @@ function checkCustomBabelConfigDeprecation(
/** /**
* Generate a new, flat Babel config, ready to be handed to Babel-traverse. * Generate a new, flat Babel config, ready to be handed to Babel-traverse.
* This config should have no unresolved overrides, presets, etc. * This config should have no unresolved overrides, presets, etc.
*
* The config returned by this function is cached, so the function should not
* depend on file-specific configuration or configuration that could change
* across invocations without a process restart.
*/ */
async function getFreshConfig( async function getFreshConfig(
ctx: NextJsLoaderContext, ctx: NextJsLoaderContext,
cacheCharacteristics: CharacteristicsGermaneToCaching, cacheCharacteristics: CharacteristicsGermaneToCaching,
loaderOptions: NextBabelLoaderOptions, loaderOptions: NextBabelLoaderOptions,
target: string, target: string
filename: string,
inputSourceMap?: SourceMap
): Promise<ResolvedBabelConfig | null> { ): Promise<ResolvedBabelConfig | null> {
const { const { transformMode } = loaderOptions
transformMode, const { hasReactCompiler, configFilePath, fileExt } = cacheCharacteristics
configFile,
reactCompilerPlugins,
reactCompilerExclude,
} = loaderOptions
const hasReactCompiler = await (async () => {
if (reactCompilerPlugins && reactCompilerPlugins.length === 0) {
return false
}
if (/[/\\]node_modules[/\\]/.test(filename)) { let customConfig = configFilePath && getCustomBabelConfig(configFilePath)
return false
}
if (reactCompilerExclude && reactCompilerExclude(filename)) {
return false
}
if (!(await isReactCompilerRequired(filename))) {
return false
}
return true
})()
let customConfig = configFile && getCustomBabelConfig(configFile)
if (transformMode === 'standalone' && !customConfig && !hasReactCompiler) { if (transformMode === 'standalone' && !customConfig && !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
...@@ -350,6 +342,9 @@ async function getFreshConfig( ...@@ -350,6 +342,9 @@ async function getFreshConfig(
checkCustomBabelConfigDeprecation(customConfig) checkCustomBabelConfigDeprecation(customConfig)
// We can assume that `reactCompilerPlugins` does not change without a process
// restart (it's safe to cache), as it's specified in the `next.config.js`,
// which always causes a full restart of `next dev` if changed.
const reactCompilerPluginsIfEnabled = hasReactCompiler const reactCompilerPluginsIfEnabled = hasReactCompiler
? (loaderOptions.reactCompilerPlugins ?? []) ? (loaderOptions.reactCompilerPlugins ?? [])
: [] : []
...@@ -365,13 +360,12 @@ async function getFreshConfig( ...@@ -365,13 +360,12 @@ async function getFreshConfig(
let options: BabelLoaderTransformOptions = { let options: BabelLoaderTransformOptions = {
babelrc: false, babelrc: false,
cloneInputAst: false, cloneInputAst: false,
filename,
inputSourceMap,
// Ensure that Webpack will get a full absolute path in the sourcemap // Use placeholder file info. `updateBabelConfigWithFileDetails` will
// so that it can properly map the module back to its internal cached // replace this after caching.
// modules. filename: `basename.${fileExt}`,
sourceFileName: filename, inputSourceMap: undefined,
sourceFileName: `basename.${fileExt}`,
// Set the default sourcemap behavior based on Webpack's mapping flag, // Set the default sourcemap behavior based on Webpack's mapping flag,
// but allow users to override if they want. // but allow users to override if they want.
...@@ -476,26 +470,56 @@ function getCacheKey(cacheCharacteristics: CharacteristicsGermaneToCaching) { ...@@ -476,26 +470,56 @@ function getCacheKey(cacheCharacteristics: CharacteristicsGermaneToCaching) {
isPageFile, isPageFile,
isNextDist, isNextDist,
hasModuleExports, hasModuleExports,
fileNameOrExt, hasReactCompiler,
fileExt,
configFilePath, configFilePath,
} = cacheCharacteristics } = cacheCharacteristics
const flags = const flags =
0 | 0 |
(isStandalone ? 0b00001 : 0) | (isStandalone ? 0b000001 : 0) |
(isServer ? 0b00010 : 0) | (isServer ? 0b000010 : 0) |
(isPageFile ? 0b00100 : 0) | (isPageFile ? 0b000100 : 0) |
(isNextDist ? 0b01000 : 0) | (isNextDist ? 0b001000 : 0) |
(hasModuleExports ? 0b10000 : 0) (hasModuleExports ? 0b010000 : 0) |
(hasReactCompiler ? 0b100000 : 0)
// separate strings will null bytes, assuming null bytes are not valid in file // separate strings will null bytes, assuming null bytes are not valid in file
// paths // paths
return `${configFilePath || ''}\x00${fileNameOrExt}\x00${flags}` return `${configFilePath || ''}\x00${fileExt}\x00${flags}`
} }
const configCache: Map<any, ResolvedBabelConfig | null> = new Map() const configCache: Map<any, ResolvedBabelConfig | null> = new Map()
const configFiles: Set<string> = new Set() const configFiles: Set<string> = new Set()
/**
* Applies file-specific values to a potentially-cached configuration object.
*/
function updateBabelConfigWithFileDetails(
cachedConfig: ResolvedBabelConfig | null | undefined,
loaderOptions: NextBabelLoaderOptions,
filename: string,
inputSourceMap: SourceMap | undefined
): ResolvedBabelConfig | null {
if (cachedConfig == null) {
return null
}
return {
...cachedConfig,
options: {
...cachedConfig.options,
cwd: loaderOptions.cwd,
root: loaderOptions.cwd,
filename,
inputSourceMap,
// Ensure that Webpack will get a full absolute path in the sourcemap
// so that it can properly map the module back to its internal cached
// modules.
sourceFileName: filename,
},
}
}
export default async function getConfig( export default async function getConfig(
ctx: NextJsLoaderContext, ctx: NextJsLoaderContext,
{ {
...@@ -512,7 +536,7 @@ export default async function getConfig( ...@@ -512,7 +536,7 @@ export default async function getConfig(
inputSourceMap?: SourceMap | undefined inputSourceMap?: SourceMap | undefined
} }
): Promise<ResolvedBabelConfig | null> { ): Promise<ResolvedBabelConfig | null> {
const cacheCharacteristics = getCacheCharacteristics( const cacheCharacteristics = await getCacheCharacteristics(
loaderOptions, loaderOptions,
source, source,
filename filename
...@@ -524,22 +548,14 @@ export default async function getConfig( ...@@ -524,22 +548,14 @@ export default async function getConfig(
} }
const cacheKey = getCacheKey(cacheCharacteristics) const cacheKey = getCacheKey(cacheCharacteristics)
if (configCache.has(cacheKey)) { const cachedConfig = configCache.get(cacheKey)
const cachedConfig = configCache.get(cacheKey) if (cachedConfig !== undefined) {
if (!cachedConfig) { return updateBabelConfigWithFileDetails(
return null cachedConfig,
} loaderOptions,
filename,
return { inputSourceMap
...cachedConfig, )
options: {
...cachedConfig.options,
cwd: loaderOptions.cwd,
root: loaderOptions.cwd,
filename,
sourceFileName: filename,
},
}
} }
if (loaderOptions.configFile && !configFiles.has(loaderOptions.configFile)) { if (loaderOptions.configFile && !configFiles.has(loaderOptions.configFile)) {
...@@ -553,12 +569,15 @@ export default async function getConfig( ...@@ -553,12 +569,15 @@ export default async function getConfig(
ctx, ctx,
cacheCharacteristics, cacheCharacteristics,
loaderOptions, loaderOptions,
target, target
filename,
inputSourceMap
) )
configCache.set(cacheKey, freshConfig) configCache.set(cacheKey, freshConfig)
return freshConfig return updateBabelConfigWithFileDetails(
freshConfig,
loaderOptions,
filename,
inputSourceMap
)
} }
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