zaach / jison

Bison in JavaScript.
http://jison.org
4.34k stars 448 forks source link

Cannot resolve module 'fs' when using webpack #320

Closed ppadovani closed 8 years ago

ppadovani commented 8 years ago

Have been trying to get the parser running in a webpack env...

Without using the jison-loader, this code: var jison = require('jison');

var bnf = require("raw!./queryLang.jison"); var parser = new jison.Generator(bnf, {type:"slr",noDefaultResolve:true});

produces these errors: ERROR in ./~/jison/lib/jison.js Module not found: Error: Cannot resolve module 'fs' in /Users/ppadovani/oss/kibana/node_modules/jison/lib @ ./~/jison/lib/jison.js 1283:17-30

ERROR in ./~/jison/~/lex-parser/lex-parser.js
Module not found: Error: Cannot resolve module 'fs' in /Users/ppadovani/oss/kibana/node_modules/jison/node_modules/lex-parser
 @ ./~/jison/~/lex-parser/lex-parser.js 845:17-30

The jison-loader found here: https://github.com/joelburget/jison-loader

var parser = require("jison!./queryLang.jison");

Gives me a parse error: ERROR in ./~/jison-loader!./src/ui/public/parse_query/lib/queryLang.jison Module build failed: Error: Parse error on line 1: ...cular error) }*/var parser = (functi --------------------^ Expecting '%%', 'START', 'LEX_BLOCK', 'ACTION', 'OPTIONS', 'PARSE_PARAM', 'LEFT', 'RIGHT', 'NONASSOC', got 'ID' at Object.parseError (/Users/ppadovani/oss/kibana/node_modules/jison/node_modules/ebnf-parser/parser.js:211:15) at Object.parse (/Users/ppadovani/oss/kibana/node_modules/jison/node_modules/ebnf-parser/parser.js:269:22)

If I cut and paste my BNF into the online Jison tool, it works perfectly...

Any thoughts on what I might be doing wrong?

UPDATE: Just having the require('jison') is enough to cause the 'fs' missing errors above.

ppadovani commented 8 years ago

Wouldn't you know it, I figure it out after I open an issue...

Solution:

1) Add this to the webpack config: node: {fs: "empty"},

2) This is the code I use to gen the parser: var jison = require('jison');

var bnf = require("raw!./queryLang.jison"); var parser = new jison.Parser(bnf, {type:"slr",noDefaultResolve:true,moduleType:"js"}); parser.yy = require('ui/parse_query/lib/queryAdapter');