Просмотр исходного кода

Merge pull request #15957 from overleaf/jpa-invalidate-babel-cache

[web] after changing settings invalidate babel cache on webpack startup

GitOrigin-RevId: 3d83c56d119bcbbd91ea71b0a85ad8e0a767b679
Eric Mc Sween 2 лет назад
Родитель
Сommit
4a8a811cc1

+ 8 - 0
services/web/config/settings.defaults.js

@@ -2,6 +2,10 @@ const fs = require('fs')
 const Path = require('path')
 const Path = require('path')
 const { merge } = require('@overleaf/settings/merge')
 const { merge } = require('@overleaf/settings/merge')
 
 
+// Automatically detect module imports that are included in this version of the application (SaaS, Server-CE, Server Pro).
+// E.g. during a Server-CE build, we will not find imports for proprietary modules.
+//
+// Restart webpack after adding/removing modules.
 const MODULES_PATH = Path.join(__dirname, '../modules')
 const MODULES_PATH = Path.join(__dirname, '../modules')
 const entryPointsIde = []
 const entryPointsIde = []
 const entryPointsMain = []
 const entryPointsMain = []
@@ -837,6 +841,9 @@ module.exports = {
 
 
   overleafModuleImports: {
   overleafModuleImports: {
     // modules to import (an empty array for each set of modules)
     // modules to import (an empty array for each set of modules)
+    //
+    // Restart webpack after making changes.
+    //
     createFileModes: [],
     createFileModes: [],
     devToolbar: [],
     devToolbar: [],
     gitBridge: [],
     gitBridge: [],
@@ -863,6 +870,7 @@ module.exports = {
     managedGroupSubscriptionEnrollmentNotification: [],
     managedGroupSubscriptionEnrollmentNotification: [],
     managedGroupEnrollmentInvite: [],
     managedGroupEnrollmentInvite: [],
     ssoConfigurationModal: [],
     ssoConfigurationModal: [],
+    // See comment at the definition of these variables.
     entryPointsIde,
     entryPointsIde,
     entryPointsMain,
     entryPointsMain,
   },
   },

+ 3 - 0
services/web/frontend/macros/import-overleaf-module.macro.js

@@ -1,4 +1,7 @@
 const { createMacro, MacroError } = require('babel-plugin-macros')
 const { createMacro, MacroError } = require('babel-plugin-macros')
+
+// This copy of the settings will be taken when webpack starts.
+// Be sure to restart webpack after making changes to the settings.
 const Settings = require('@overleaf/settings')
 const Settings = require('@overleaf/settings')
 
 
 const macro = createMacro(importOverleafModuleMacro)
 const macro = createMacro(importOverleafModuleMacro)

+ 24 - 0
services/web/frontend/macros/invalidate-babel-cache-if-needed.js

@@ -0,0 +1,24 @@
+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)
+  }
+}

+ 4 - 0
services/web/webpack.config.js

@@ -9,6 +9,10 @@ const {
 } = require('./webpack-plugins/lezer-grammar-compiler')
 } = require('./webpack-plugins/lezer-grammar-compiler')
 
 
 const PackageVersions = require('./app/src/infrastructure/PackageVersions')
 const PackageVersions = require('./app/src/infrastructure/PackageVersions')
+const invalidateBabelCacheIfNeeded = require('./frontend/macros/invalidate-babel-cache-if-needed')
+
+// Make sure that babel-macros are re-evaluated after changing the modules config
+invalidateBabelCacheIfNeeded()
 
 
 // Generate a hash of entry points, including modules
 // Generate a hash of entry points, including modules
 const entryPoints = {
 const entryPoints = {