-
Notifications
You must be signed in to change notification settings - Fork 67
Add ES6 download button #509
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
Conversation
I was expecting to see a |
Not needed. Just need to export the function as default. It will probably work either way though. |
I have been using this and also generating typescript bindings, so that generate method will all get updated when I get around to it. |
Hey @hildjj I took your advice and tried this, and it worked well. $( "#parser-download-es6" )
.click(function(){
// If this button was enabled, the source was already validated by 'rebuildGrammar'
const esSource = peggy.generate(editor.getValue(), {
cache: $("#option-cache").is(":checked"),
output: "source",
format: 'es'
})
var blob = new Blob([esSource], {type: "application/javascript"});
window.saveAs(blob, "parser.js");
}); |
No longer a default export though.... So this wont work.
This will
|
Default export isn't really right, since you also sometimes need access to the SyntaxError class. import {parse} from './src/parser.js' |
I just added a try/catch block to be safe. |
Yes, the named export is best for tree shaking to. I updated the first comment with the usage. |
OK, this looks good to me, ready to merge after rebase. Did you test with an invalid grammar to make sure the UI is adequate? |
I downloaded the generated es6 and ran it in node. I needed to rename to .mjs... I am not sure if we should change the extension though, as it will work in bundler or if module is defined in project. |
I don't know why the rebase is so messy, I am going to force push the branch. |
I agree that the default name being |
Also note: none of this will go live on peggyjs.org until we do a release. It's theoretically possible to cherry-pick over to the |
Want me to change the extension to mjs? |
Yes, please. I think changing to .mjs is a good idea. |
Thanks! |
LOL Do you know how many times I tried to change that text box with "module.exports" over the past couple of years. 😆 |
Open source is all about scratching your itch. :) |
I added an ES6 export button. This will export the function as the default. I also clarified that the named export text box is only for CommonJS.
USAGE
or