Răsfoiți Sursa

Merge pull request #8584 from overleaf/jk-fix-ce-build

Fix CE build by conditionally loading webpack plugin.

GitOrigin-RevId: bfbfa6ae0130d21b5e1fa43a90cf247eb5b5c8fa
June Kelly 4 ani în urmă
părinte
comite
58fe248968
1 a modificat fișierele cu 50 adăugiri și 40 ștergeri
  1. 50 40
      services/web/webpack-plugins/lezer-grammar-compiler.js

+ 50 - 40
services/web/webpack-plugins/lezer-grammar-compiler.js

@@ -1,49 +1,59 @@
-const {
-  compile,
-  options,
-} = require('../modules/source-editor/scripts/lezer-latex/generate')
 const fs = require('fs')
+const path = require('path')
+const modulePath = path.resolve(
+  __dirname,
+  '../modules/source-editor/scripts/lezer-latex/generate.js'
+)
 
-const PLUGIN_NAME = 'lezer-grammar-compiler'
-
-class LezerGrammarCompilerPlugin {
-  apply(compiler) {
-    compiler.hooks.make.tap(PLUGIN_NAME, compilation => {
-      // Add the grammar file to the file paths watched by webpack
-      compilation.fileDependencies.add(options.grammarPath)
-    })
-    compiler.hooks.beforeCompile.tapAsync(
-      PLUGIN_NAME,
-      (_compilation, callback) => {
-        // Check timestamps on grammar and parser files, and re-compile if needed.
-        // (Note: the compiled parser file is watched by webpack, and so will trigger
-        //  a second compilation immediately after. This seems harmless.)
-        if (
-          !fs.existsSync(options.parserOutputPath) ||
-          !fs.existsSync(options.termsOutputPath)
-        ) {
-          console.log('Parser does not exist, compiling')
-          compile()
-          return callback()
-        }
-        fs.stat(options.grammarPath, (err, grammarStat) => {
-          if (err) {
-            return callback(err)
+try {
+  fs.accessSync(modulePath, fs.constants.W_OK)
+  const { compile, options } = require(modulePath)
+  const PLUGIN_NAME = 'lezer-grammar-compiler'
+  class LezerGrammarCompilerPlugin {
+    apply(compiler) {
+      compiler.hooks.make.tap(PLUGIN_NAME, compilation => {
+        // Add the grammar file to the file paths watched by webpack
+        compilation.fileDependencies.add(options.grammarPath)
+      })
+      compiler.hooks.beforeCompile.tapAsync(
+        PLUGIN_NAME,
+        (_compilation, callback) => {
+          // Check timestamps on grammar and parser files, and re-compile if needed.
+          // (Note: the compiled parser file is watched by webpack, and so will trigger
+          //  a second compilation immediately after. This seems harmless.)
+          if (
+            !fs.existsSync(options.parserOutputPath) ||
+            !fs.existsSync(options.termsOutputPath)
+          ) {
+            console.log('Parser does not exist, compiling')
+            compile()
+            return callback()
           }
-          fs.stat(options.parserOutputPath, (err, parserStat) => {
+          fs.stat(options.grammarPath, (err, grammarStat) => {
             if (err) {
               return callback(err)
             }
-            callback()
-            if (grammarStat.mtime > parserStat.mtime) {
-              console.log('Grammar file newer than parser file, re-compiling')
-              compile()
-            }
+            fs.stat(options.parserOutputPath, (err, parserStat) => {
+              if (err) {
+                return callback(err)
+              }
+              callback()
+              if (grammarStat.mtime > parserStat.mtime) {
+                console.log('Grammar file newer than parser file, re-compiling')
+                compile()
+              }
+            })
           })
-        })
-      }
-    )
+        }
+      )
+    }
+  }
+  module.exports = { LezerGrammarCompilerPlugin }
+} catch {
+  class NoOpPlugin {
+    apply() {
+      console.log('lezer-latex module not present, skipping compile')
+    }
   }
+  module.exports = { LezerGrammarCompilerPlugin: NoOpPlugin }
 }
-
-module.exports = { LezerGrammarCompilerPlugin }