Skip to content

Added option to specify variable name in output code. #4

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ paths =
gulp.task "peg:compile", ->
gulp
.src( paths.scripts.peg )
.pipe( peg( ).on( "error", gutil.log ) )
.pipe( peg( {exportVar: ''} ) )
.on( "error", gutil.log )
.pipe( gulp.dest( paths.build ) )
```

Expand All @@ -39,3 +40,8 @@ Plugin redirects passed options directly to PEG, so read [its documentation](htt

This option is inspired by [grunt-peg](https://github.com/dvberkel/grunt-peg) plugin, and defines variable to which the generated parser will be assigned in the output file. Default value is `module.exports`.

`exportVar` | Behavior
---------- | ---------
`null` | **default** Will set variable to `module.exports`
function(filename){} | should return the desired variable name
`''` (empty string) | convenience value to set variable to '*filename*parser'
20 changes: 15 additions & 5 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ through = require "through2"
processFile = ( file, opts ) ->
grammar = file.contents.toString "utf8"
parser = PEG.buildParser grammar, opts
source = util.format "%s = %s;", opts.exportVar, parser

if typeof opts.exportVar is 'function'
variable = opts.exportVar file.relative
else
if opts.exportVar isnt ''
variable = opts.exportVar
else
variable = file.relative.split('.')[0] + 'parser'

source = util.format "%s = %s;", variable, parser

file.path = gutil.replaceExtension file.path, ".js"
file.contents = new Buffer source
Expand Down