Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails to browserify jison generated parsers #300

Open
nahidakbar opened this issue Sep 5, 2015 · 6 comments
Open

Fails to browserify jison generated parsers #300

nahidakbar opened this issue Sep 5, 2015 · 6 comments

Comments

@nahidakbar
Copy link

TypeError: Object # has no method 'apply'
at walk (/home/path/node_modules/brfs/node_modules/static-module/node_modules/static-eval/index.js:89:27)
at walk (/home/path/node_modules/brfs/node_modules/static-module/node_modules/static-eval/index.js:92:23)
at walk (/home/path/node_modules/brfs/node_modules/static-module/node_modules/static-eval/index.js:77:26)
at walk (/home/path/node_modules/brfs/node_modules/static-module/node_modules/static-eval/index.js:85:25)
at module.exports (/home/path/node_modules/brfs/node_modules/static-module/node_modules/static-eval/index.js:114:7)
at traverse (/home/path/node_modules/brfs/node_modules/static-module/index.js:256:23)
at walk (/home/path/node_modules/brfs/node_modules/static-module/index.js:208:13)
at walk (/home/path/node_modules/brfs/node_modules/static-module/node_modules/falafel/index.js:49:9)
at /home/path/node_modules/brfs/node_modules/static-module/node_modules/falafel/index.js:46:17
at forEach (/home/path/node_modules/brfs/node_modules/static-module/node_modules/falafel/node_modules/foreach/index.js:12:16)

I have one line of code: var formula = require("./formula");
and I am using the calculator example from the website.

@deathcap
Copy link

deathcap commented Jan 9, 2016

Just hit this issue myself, proposed this fix in static-eval:

browserify/static-eval#12 Fix exception instead of failure when parsing CallExpressions

The unbrowserifable (or more accurately, unbrfs-able, since static-eval chokes) code is in the generated command-line tool main function:

exports.main = function commonjsMain(args) {
    if (!args[1]) {
        console.log('Usage: '+args[0]+' FILE');
        process.exit(1);
    }
*   var source = require('fs').readFileSync(require('path').normalize(args[1]), "utf8");
    return exports.parser.parse(source);
};

if jison omits this code (probably not needed when running in the browser - override with opts.moduleMain?), or rewrites it as:

var path = require('path').normalize(args[1]);
var source = require('fs').readFileSync(path, "utf8");

or if static-eval integrates browserify/static-eval#12, then jison should be browserifable

@deathcap
Copy link

If using the jison API, then the unbrowserifiable code can be omitted by passing an alternate mainModule function to the generation options, for example:

var Parser = require('jison').Parser;
var fs = require('fs');
var path = require('path');

var options = {
  mainModule: function() {}
};

var grammar = fs.readFileSync(path.join(__dirname, 'grammar.jison'), 'utf8');
var parser = new Parser(grammar);
var parserSource = parser.generate(options);
fs.writeFileSync(path.join(__dirname, 'grammar.js'), parserSource, 'utf8');

As for the CLI, if #316 is merged then this will work:

jison grammar.jison --moduleMain 'function() {}'

deathcap added a commit to deathcap/node-mojangson that referenced this issue Jan 10, 2016
The jison parser generator by default will generate a command-line
interface built into the parser. node-mojangson only uses the parser
object and not the command-line interface, and has no way to access it,
so it can be safely disabled -- since it causes compatibility issues
with some browserify transforms (and is unused), see:

zaach/jison#300
browserify/static-eval#12
@lddubeau
Copy link

This is also a problem when using webpack to incorporate a jison-generated module into a build.

(By the way, moduleMain: function() {}, not mainModule: ....)

Ultimately, what I'd like to see is the capability to completely turn off the generation of code that pertains to calling a "main function". Perhaps setting moduleMain: null would could mean "I don't want a main" and the whole code, including the if test that calls exports.main, could be omitted from the generated module.

@twheys
Copy link

twheys commented Nov 16, 2018

I would be very happy with a compiler flag to just disable including the main function export

@twheys
Copy link

twheys commented Nov 16, 2018

After a bit of scanning through the source code, I found that running jison -m js parser.jison generated output that did not include the commonJS main function.

@andyvanee
Copy link

Although this may be a stale issue, I just wanted to add that I found a way around this by using a custom generator script which uses the js module type and exports the resulting variable which produces a valid es6 module.

import { readFileSync, writeFileSync } from "fs"
import jison from "jison"

const moduleName = "parser"

const source = new URL("../parser/grammar.jison", import.meta.url).pathname
const dest = new URL("../parser/grammar.js", import.meta.url).pathname

const grammar = readFileSync(source, "utf8")
const parser = new jison.Parser(grammar)

const parserSource = parser.generate({ moduleType: "js", moduleName })
writeFileSync(dest, `${parserSource}\nexport {${moduleName}}`)

I run this generator is run prior to bundling the whole thing with rollup using the following:

node --experimental-modules --no-warnings parser/generate.js

Hopefully this helps anyone stuck on this issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants