invalidate-babel-cache-if-needed.js 998 B

1234567891011121314151617181920212223242526272829
  1. const fs = require('fs')
  2. const Path = require('path')
  3. const Settings = require('@overleaf/settings')
  4. module.exports = function invalidateBabelCacheIfNeeded() {
  5. const cacheDir = Path.join(__dirname, '../../.cache')
  6. const cachePath = Path.join(cacheDir, 'babel-loader')
  7. const statePath = Path.join(cacheDir, 'last-overleafModuleImports.json')
  8. let lastState = ''
  9. try {
  10. lastState = fs.readFileSync(statePath, { encoding: 'utf-8' })
  11. } catch (e) {}
  12. const newState = JSON.stringify(Settings.overleafModuleImports)
  13. if (lastState !== newState) {
  14. // eslint-disable-next-line no-console
  15. console.warn(
  16. 'Detected change in overleafModuleImports, purging babel cache!'
  17. )
  18. // Gracefully handle cache mount in Server Pro build, only purge nested folder and keep .cache/ folder.
  19. fs.mkdirSync(cacheDir, { recursive: true })
  20. fs.rmSync(cachePath, {
  21. recursive: true,
  22. force: true,
  23. maxRetries: 5,
  24. })
  25. fs.writeFileSync(statePath, newState)
  26. }
  27. }