From a0a57cd22d2930123357781e2cfb70b2c40ab2bc Mon Sep 17 00:00:00 2001 From: David Majda Date: Mon, 2 May 2016 16:40:14 +0200 Subject: [PATCH] UMD parsers: Make bin/pegjs generate UMD parsers Part of work on #362. --- README.md | 8 ++++---- bin/pegjs | 26 ++++++++++++-------------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index a9c8d48..eaf625e 100644 --- a/README.md +++ b/README.md @@ -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. -By default, the parser object is assigned to `module.exports`, which makes the -output a Node.js module. You can assign it to another variable by passing a -variable name using the `-e`/`--export-var` option. This may be helpful if you -want to use the parser in browser environment. +The generated parser is in the [UMD](https://github.com/umdjs/umd) format, which +means it works as a Node.js or AMD module. You can also use the +`-e`/`--export-var` option to define a global variable into which the parser +object is assigned to when no module loader is detected. You can tweak the generated parser with several options: diff --git a/bin/pegjs b/bin/pegjs index f2f4799..3ecdc74 100755 --- a/bin/pegjs +++ b/bin/pegjs @@ -25,9 +25,9 @@ function printHelp() { console.log("omitted, standard input and output are used."); console.log(""); console.log("Options:"); - console.log(" -e, --export-var name of the variable where the parser"); - console.log(" object will be stored (default:"); - console.log(" \"module.exports\")"); + console.log(" -e, --export-var name of a global variable into which the"); + console.log(" parser object is assigned to when no module"); + console.log(" loader is detected (default: \"\")"); console.log(" --cache make generated parser cache results"); console.log(" --allowed-start-rules comma-separated list of rules the generated"); console.log(" parser will be allowed to start parsing"); @@ -110,14 +110,14 @@ function readStream(inputStream, callback) { /* Main */ -/* This makes the generated parser a CommonJS module by default. */ -var exportVar = "module.exports"; var options = { - cache: false, - output: "source", - optimize: "speed", - trace: false, - plugins: [] + cache: false, + output: "source", + format: "umd", + exportVar: null, + optimize: "speed", + trace: false, + plugins: [] }; while (args.length > 0 && isOption(args[0])) { @@ -128,7 +128,7 @@ while (args.length > 0 && isOption(args[0])) { if (args.length === 0) { abort("Missing parameter of the -e/--export-var option."); } - exportVar = args[0]; + options.exportVar = args[0]; break; case "--cache": @@ -266,9 +266,7 @@ readStream(inputStream, function(input) { } } - outputStream.write( - exportVar !== "" ? exportVar + " = " + source + ";\n" : source + "\n" - ); + outputStream.write(source); if (outputStream !== process.stdout) { outputStream.end(); }