Skip to content

Commit

Permalink
invalidate babel config cache when babelrc changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Apr 3, 2021
1 parent cede740 commit eebbbfa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
32 changes: 32 additions & 0 deletions packages/gatsby/src/utils/babel-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const { getBrowsersList } = require(`./browserslist`)

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

module.exports = babelLoader.custom(babel => {
return {
Expand Down Expand Up @@ -71,6 +72,18 @@ module.exports = babelLoader.custom(babel => {
partialConfig.files.forEach(configFilePath => {
configCacheKey += `_${configFilePath}`
})

// after generating configCacheKey add link between babelrc files and cache keys that rely on it
// so we can invalidate memoized configs when used babelrc file changes
partialConfig.files.forEach(configFilePath => {
let cacheKeysToInvalidate = babelrcFileToCacheKey.get(configFilePath)
if (!cacheKeysToInvalidate) {
cacheKeysToInvalidate = new Set()
babelrcFileToCacheKey.set(configFilePath, cacheKeysToInvalidate)
}

cacheKeysToInvalidate.add(configCacheKey)
})
}

let { options } = partialConfig
Expand Down Expand Up @@ -138,3 +151,22 @@ module.exports = babelLoader.custom(babel => {
},
}
})

module.exports.BabelConfigItemsCacheInvalidatorPlugin = class BabelConfigItemsCacheInvalidatorPlugin {
constructor() {
this.name = `BabelConfigItemsCacheInvalidatorPlugin`
}

apply(compiler) {
compiler.hooks.invalid.tap(this.name, function (file) {
const cacheKeysToInvalidate = babelrcFileToCacheKey.get(file)

if (cacheKeysToInvalidate) {
for (const cacheKey of cacheKeysToInvalidate) {
configCache.delete(cacheKey)
}
babelrcFileToCacheKey.delete(file)
}
})
}
}
2 changes: 2 additions & 0 deletions packages/gatsby/src/utils/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { StaticQueryMapper } from "./webpack/static-query-mapper"
import { ForceCssHMRForEdgeCases } from "./webpack/force-css-hmr-for-edge-cases"
import { getBrowsersList } from "./browserslist"
import { builtinModules } from "module"
const { BabelConfigItemsCacheInvalidatorPlugin } = require(`./babel-loader`)

const FRAMEWORK_BUNDLES = [`react`, `react-dom`, `scheduler`, `prop-types`]

Expand Down Expand Up @@ -211,6 +212,7 @@ module.exports = async (
}),

plugins.virtualModules(),
new BabelConfigItemsCacheInvalidatorPlugin(),
]

switch (stage) {
Expand Down

0 comments on commit eebbbfa

Please sign in to comment.