UMD parsers: Make bin/pegjs generate UMD parsers

Part of work on #362.
redux
David Majda 8 years ago
parent b87268ade6
commit a0a57cd22d

@ -73,10 +73,10 @@ file but with “.js” extension. You can also specify the output file explicit
If you omit both input and output file, standard input and output are used. If you omit both input and output file, standard input and output are used.
By default, the parser object is assigned to `module.exports`, which makes the The generated parser is in the [UMD](https://github.com/umdjs/umd) format, which
output a Node.js module. You can assign it to another variable by passing a means it works as a Node.js or AMD module. You can also use the
variable name using the `-e`/`--export-var` option. This may be helpful if you `-e`/`--export-var` option to define a global variable into which the parser
want to use the parser in browser environment. object is assigned to when no module loader is detected.
You can tweak the generated parser with several options: You can tweak the generated parser with several options:

@ -25,9 +25,9 @@ function printHelp() {
console.log("omitted, standard input and output are used."); console.log("omitted, standard input and output are used.");
console.log(""); console.log("");
console.log("Options:"); console.log("Options:");
console.log(" -e, --export-var <variable> name of the variable where the parser"); console.log(" -e, --export-var <variable> name of a global variable into which the");
console.log(" object will be stored (default:"); console.log(" parser object is assigned to when no module");
console.log(" \"module.exports\")"); console.log(" loader is detected (default: \"\")");
console.log(" --cache make generated parser cache results"); console.log(" --cache make generated parser cache results");
console.log(" --allowed-start-rules <rules> comma-separated list of rules the generated"); console.log(" --allowed-start-rules <rules> comma-separated list of rules the generated");
console.log(" parser will be allowed to start parsing"); console.log(" parser will be allowed to start parsing");
@ -110,14 +110,14 @@ function readStream(inputStream, callback) {
/* Main */ /* Main */
/* This makes the generated parser a CommonJS module by default. */
var exportVar = "module.exports";
var options = { var options = {
cache: false, cache: false,
output: "source", output: "source",
optimize: "speed", format: "umd",
trace: false, exportVar: null,
plugins: [] optimize: "speed",
trace: false,
plugins: []
}; };
while (args.length > 0 && isOption(args[0])) { while (args.length > 0 && isOption(args[0])) {
@ -128,7 +128,7 @@ while (args.length > 0 && isOption(args[0])) {
if (args.length === 0) { if (args.length === 0) {
abort("Missing parameter of the -e/--export-var option."); abort("Missing parameter of the -e/--export-var option.");
} }
exportVar = args[0]; options.exportVar = args[0];
break; break;
case "--cache": case "--cache":
@ -266,9 +266,7 @@ readStream(inputStream, function(input) {
} }
} }
outputStream.write( outputStream.write(source);
exportVar !== "" ? exportVar + " = " + source + ";\n" : source + "\n"
);
if (outputStream !== process.stdout) { if (outputStream !== process.stdout) {
outputStream.end(); outputStream.end();
} }

Loading…
Cancel
Save