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

Add ES6 download button #509

Merged
merged 2 commits into from
Apr 4, 2024
Merged

Add ES6 download button #509

merged 2 commits into from
Apr 4, 2024

Conversation

Flamenco
Copy link
Contributor

@Flamenco Flamenco commented Apr 4, 2024

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

import {parse} from './theExport.js'
parse(...)

or

import * as Parser from './theExport.js'
Parser.parse(...)

@hildjj
Copy link
Contributor

hildjj commented Apr 4, 2024

I was expecting to see a format: 'es' in the call to generate()?

@Flamenco
Copy link
Contributor Author

Flamenco commented Apr 4, 2024

Not needed. Just need to export the function as default. It will probably work either way though.

@Flamenco
Copy link
Contributor Author

Flamenco commented Apr 4, 2024

I have been using this and also generating typescript bindings, so that generate method will all get updated when I get around to it.

@Flamenco
Copy link
Contributor Author

Flamenco commented Apr 4, 2024

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");

    });

@Flamenco
Copy link
Contributor Author

Flamenco commented Apr 4, 2024

No longer a default export though....

So this wont work.

import Parser from './theExport.js'
Parser.parse(...)

This will

import * as Parser from './theExport.js'
Parser.parse(...)

@hildjj
Copy link
Contributor

hildjj commented Apr 4, 2024

format: 'es' has a few other side-effects, most of which aren't likely to affect the output for the web (yet). Once we add support for libraries to the web, though, it's likely to matter.

Default export isn't really right, since you also sometimes need access to the SyntaxError class.

import {parse} from './src/parser.js'

@Flamenco
Copy link
Contributor Author

Flamenco commented Apr 4, 2024

I just added a try/catch block to be safe.

@Flamenco
Copy link
Contributor Author

Flamenco commented Apr 4, 2024

format: 'es' has a few other side-effects, most of which aren't likely to affect the output for the web (yet). Once we add support for libraries to the web, though, it's likely to matter.

Default export isn't really right, since you also sometimes need access to the SyntaxError class.

import {parse} from './src/parser.js'

Yes, the named export is best for tree shaking to. I updated the first comment with the usage.

@hildjj
Copy link
Contributor

hildjj commented Apr 4, 2024

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?

@Flamenco
Copy link
Contributor Author

Flamenco commented Apr 4, 2024

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.

@Flamenco
Copy link
Contributor Author

Flamenco commented Apr 4, 2024

I don't know why the rebase is so messy, I am going to force push the branch.

@hildjj
Copy link
Contributor

hildjj commented Apr 4, 2024

I agree that the default name being .mjs doesn't hurt anything and is more likely to work more often.

@hildjj
Copy link
Contributor

hildjj commented Apr 4, 2024

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 stable branch, but it causes such a headache it's usually not worth it.

@Flamenco
Copy link
Contributor Author

Flamenco commented Apr 4, 2024

Want me to change the extension to mjs?

@hildjj
Copy link
Contributor

hildjj commented Apr 4, 2024

Yes, please. I think changing to .mjs is a good idea.

@hildjj
Copy link
Contributor

hildjj commented Apr 4, 2024

Thanks!

@Flamenco
Copy link
Contributor Author

Flamenco commented Apr 4, 2024

LOL Do you know how many times I tried to change that text box with "module.exports" over the past couple of years. 😆

@hildjj hildjj merged commit d63d3a0 into peggyjs:main Apr 4, 2024
9 checks passed
@hildjj
Copy link
Contributor

hildjj commented Apr 4, 2024

Open source is all about scratching your itch. :)

@Flamenco Flamenco deleted the feat_add_es6 branch April 5, 2024 07:47
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

Successfully merging this pull request may close these issues.

None yet

2 participants