Skip to content

Commit

Permalink
perf(gatsby): cache babel partial config and babel custom options
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Dec 23, 2020
1 parent b9e328e commit 0428d03
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions packages/gatsby/src/utils/babel-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,24 @@ const { getBrowsersList } = require(`./browserslist`)
*
* You can find documentation for the custom loader here: https://babeljs.io/docs/en/next/babel-core.html#loadpartialconfig
*/

const customOptionsCache = new Map()
const configCache = new Map()

module.exports = babelLoader.custom(babel => {
const toReturn = {
return {
// Passed the loader options.
customOptions({
stage = `test`,
reactRuntime = `classic`,
rootDir = process.cwd(),
...options
}) {
return {
if (customOptionsCache.has(stage)) {
return customOptionsCache.get(stage)
}

const toReturn = {
custom: {
stage,
reactRuntime,
Expand All @@ -48,11 +56,27 @@ module.exports = babelLoader.custom(babel => {
...options,
},
}

customOptionsCache.set(stage, toReturn)

return toReturn
},

// Passed Babel's 'PartialConfig' object.
config(partialConfig, { customOptions }) {
let configCacheKey = customOptions.stage
if (partialConfig.hasFilesystemConfig()) {
// partialConfig.files is a Set that accumulates used config files (absolute paths)
partialConfig.files.forEach(configFilePath => {
configCacheKey += `_${configFilePath}`
})
}

let { options } = partialConfig
if (configCache.has(configCacheKey)) {
return { ...options, ...configCache.get(configCacheKey) }
}

const [
reduxPresets,
reduxPlugins,
Expand Down Expand Up @@ -97,9 +121,16 @@ module.exports = babelLoader.custom(babel => {
})
})

// cache just plugins and presets, because config also includes things like
// filenames - this is mostly to not call `mergeConfigItemOptions` for each file
// as that function call `babel.createConfigItem` and is quite expensive but also
// skips quite a few nested loops on top of that
configCache.set(configCacheKey, {
plugins: options.plugins,
presets: options.presets,
})

return options
},
}

return toReturn
})

0 comments on commit 0428d03

Please sign in to comment.