overleaf/services/web/frontend/macros/invalidate-babel-cache-if-needed.js
Eric Mc Sween 4a8a811cc1 Merge pull request #15957 from overleaf/jpa-invalidate-babel-cache
[web] after changing settings invalidate babel cache on webpack startup

GitOrigin-RevId: 3d83c56d119bcbbd91ea71b0a85ad8e0a767b679
2024-01-05 09:03:17 +00:00

24 lines
815 B
JavaScript

const fs = require('fs')
const Path = require('path')
const logger = require('@overleaf/logger')
const Settings = require('@overleaf/settings')
module.exports = function invalidateBabelCacheIfNeeded() {
const cachePath = Path.join(__dirname, '../../node_modules/.cache')
const statePath = Path.join(cachePath, 'last-overleafModuleImports.json')
let lastState = ''
try {
lastState = fs.readFileSync(statePath, { encoding: 'utf-8' })
} catch (e) {}
const newState = JSON.stringify(Settings.overleafModuleImports)
if (lastState !== newState) {
logger.warn(
{},
'Detected change in overleafModuleImports, purging babel cache!'
)
fs.rmSync(cachePath, { recursive: true, force: true, maxRetries: 5 })
fs.mkdirSync(cachePath)
fs.writeFileSync(statePath, newState)
}
}