Skip to content

Commit eebbbfa

Browse files
committed
invalidate babel config cache when babelrc changes
1 parent cede740 commit eebbbfa

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

packages/gatsby/src/utils/babel-loader.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const { getBrowsersList } = require(`./browserslist`)
2626

2727
const customOptionsCache = new Map()
2828
const configCache = new Map()
29+
const babelrcFileToCacheKey = new Map()
2930

3031
module.exports = babelLoader.custom(babel => {
3132
return {
@@ -71,6 +72,18 @@ module.exports = babelLoader.custom(babel => {
7172
partialConfig.files.forEach(configFilePath => {
7273
configCacheKey += `_${configFilePath}`
7374
})
75+
76+
// after generating configCacheKey add link between babelrc files and cache keys that rely on it
77+
// so we can invalidate memoized configs when used babelrc file changes
78+
partialConfig.files.forEach(configFilePath => {
79+
let cacheKeysToInvalidate = babelrcFileToCacheKey.get(configFilePath)
80+
if (!cacheKeysToInvalidate) {
81+
cacheKeysToInvalidate = new Set()
82+
babelrcFileToCacheKey.set(configFilePath, cacheKeysToInvalidate)
83+
}
84+
85+
cacheKeysToInvalidate.add(configCacheKey)
86+
})
7487
}
7588

7689
let { options } = partialConfig
@@ -138,3 +151,22 @@ module.exports = babelLoader.custom(babel => {
138151
},
139152
}
140153
})
154+
155+
module.exports.BabelConfigItemsCacheInvalidatorPlugin = class BabelConfigItemsCacheInvalidatorPlugin {
156+
constructor() {
157+
this.name = `BabelConfigItemsCacheInvalidatorPlugin`
158+
}
159+
160+
apply(compiler) {
161+
compiler.hooks.invalid.tap(this.name, function (file) {
162+
const cacheKeysToInvalidate = babelrcFileToCacheKey.get(file)
163+
164+
if (cacheKeysToInvalidate) {
165+
for (const cacheKey of cacheKeysToInvalidate) {
166+
configCache.delete(cacheKey)
167+
}
168+
babelrcFileToCacheKey.delete(file)
169+
}
170+
})
171+
}
172+
}

packages/gatsby/src/utils/webpack.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { StaticQueryMapper } from "./webpack/static-query-mapper"
2121
import { ForceCssHMRForEdgeCases } from "./webpack/force-css-hmr-for-edge-cases"
2222
import { getBrowsersList } from "./browserslist"
2323
import { builtinModules } from "module"
24+
const { BabelConfigItemsCacheInvalidatorPlugin } = require(`./babel-loader`)
2425

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

@@ -211,6 +212,7 @@ module.exports = async (
211212
}),
212213

213214
plugins.virtualModules(),
215+
new BabelConfigItemsCacheInvalidatorPlugin(),
214216
]
215217

216218
switch (stage) {

0 commit comments

Comments
 (0)