2022-06-23 08:19:07 +00:00
|
|
|
const fs = require('fs')
|
2022-07-01 08:41:33 +00:00
|
|
|
const path = require('path')
|
2023-04-13 08:21:25 +00:00
|
|
|
const modulePath = path.resolve(__dirname, '../scripts/lezer-latex/generate.js')
|
2022-06-23 08:19:07 +00:00
|
|
|
|
2022-07-01 08:41:33 +00:00
|
|
|
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()
|
2022-06-23 08:19:07 +00:00
|
|
|
}
|
2022-07-01 08:41:33 +00:00
|
|
|
fs.stat(options.grammarPath, (err, grammarStat) => {
|
2022-06-23 08:19:07 +00:00
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
2022-07-01 08:41:33 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
})
|
2022-06-23 08:19:07 +00:00
|
|
|
})
|
2022-07-01 08:41:33 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = { LezerGrammarCompilerPlugin }
|
|
|
|
} catch {
|
|
|
|
class NoOpPlugin {
|
|
|
|
apply() {
|
|
|
|
console.log('lezer-latex module not present, skipping compile')
|
|
|
|
}
|
2022-06-23 08:19:07 +00:00
|
|
|
}
|
2022-07-01 08:41:33 +00:00
|
|
|
module.exports = { LezerGrammarCompilerPlugin: NoOpPlugin }
|
2022-06-23 08:19:07 +00:00
|
|
|
}
|