【问题标题】:PEGJS doesn't generation "module.exports" in the parser filePEGJS 不会在解析器文件中生成“module.exports”
【发布时间】:2019-12-23 08:22:17
【问题描述】:

我尝试使用下面的代码生成解析器文件,但最终结果文件 parser.js 不包含导出行,尽管我在选项中包含了exportVar:...

我想在文件顶部手动添加它吗?

const peg = require('pegjs');
const fs = require('fs');

const grammarAsString = fs.readFileSync('./grammar.pegjs').toString();

const option = {
  output: 'source',
  exportVar: 'module.exports',
};

const parserSourceCode = peg.generate(grammarAsString, option);
fs.writeFileSync('./parser.js', parserSourceCode);

【问题讨论】:

  • 尝试使用可能包含变量的format 选项。
  • 感谢@JoshVoigts,现在可以通过将格式设置为“commonjs”

标签: pegjs


【解决方案1】:

已通过将格式选项设置为“commonjs”将其传递给解析器来解决。

const option = {
  output: 'source',
  format: 'commonjs',
};
const parserSourceCode = peg.generate(grammarAsString, option);
fs.writeFileSync('./parser.js', parserSourceCode);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 2012-09-13
    • 1970-01-01
    相关资源
    最近更新 更多