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

Common JS to ES6/7 modules - WIP #3427

Closed
wants to merge 9 commits into from
Closed

Common JS to ES6/7 modules - WIP #3427

wants to merge 9 commits into from

Conversation

Cmdv
Copy link
Contributor

@Cmdv Cmdv commented Sep 8, 2018

This is a continuation of #3379, don't have creds to re-open PR as I closed it.

This does currently work but it's not getting full benefits because any FFI usage still uses CJS so any Dead Code Elimination can't be used through out the whole codebase so this is still a WIP.

For the ability to have ES6/7 FFI we need to add the new ES6/7 import & export specs to language-javascript

To do:

@Cmdv
Copy link
Contributor Author

Cmdv commented Nov 21, 2018

Thanks to @gasi Export has been added to language-javascript. erikd/language-javascript#80

All this would now need is imports , tests and removal of trailling semicolon.

@hdgarrood
Copy link
Contributor

@Cmdv I've managed to get import statements working with language-javascript, see erikd/language-javascript#82. With that in mind, do you think you're likely to pick this back up again soon?

@hdgarrood hdgarrood changed the title Common JS to AS6/7 modules - WIP Common JS to ES6/7 modules - WIP Feb 15, 2019
@Cmdv
Copy link
Contributor Author

Cmdv commented Feb 15, 2019

@hdgarrood ah cool that's ace, had totally forgotten about this! I think I had a funny trailing ; from memory and the rest should be ok. Will investigate in next couple of days.

@gasi
Copy link

gasi commented Feb 15, 2019

I also have some WIP for generating ES2015 (ES6). I think supporting import in language-javascript might be one of the missing pieces for supporting ES2015 FFI: gasi#1 Feel free to use any of my work if it helps.

@purcell
Copy link

purcell commented Mar 12, 2019

Is it the case, then, that if @erikd releases a new language-javascript, then bumping the dependency version here in purescript would be enough to get support for granular imports? If so, even that would be extremely useful, as it would allow external JS bundlers like Parcel to tree-shake the Javascript imports, which is currently impossible.

@erikd
Copy link

erikd commented Mar 12, 2019

@purcell If and when you want a new release of language-javascript, let me know.

@hdgarrood
Copy link
Contributor

No, there's still a lot more work which would need doing after a new version of language-javascript gets released to be able to ship ES modules in the PureScript compiler. It's also a big breaking change with the risk of causing a lot of pain throughout the ecosystem if executed poorly, so it's definitely not something we want to rush.

@erikd I'd suggest waiting until we have complete support for import and export before making another release of language-javascript, because filling the current gaps will require adding constructors to data types such as JSImportDeclaration, and of course adding constructors to an existing type which is already part of a package's public API is a breaking change, whereas providing a new type which wasn't previously part of a package's public API is only a minor-level change.

@purcell
Copy link

purcell commented Mar 13, 2019

No, there's still a lot more work which would need doing after a new version of language-javascript gets released to be able to ship ES modules in the PureScript compiler. It's also a big breaking change with the risk of causing a lot of pain throughout the ecosystem if executed poorly, so it's definitely not something we want to rush.

Cool, thanks for clarifying, and that makes total sense. I'm not familiar with the chain of dependencies, so I'll see if I can bring myself up to speed and find a way to help out.

@Cmdv
Copy link
Contributor Author

Cmdv commented Mar 13, 2019

I'm terribly sorry, I've not had the chance to get this completed.
I feel that I've probably been holding things back now, so if anyone would like to take over please feel free. 😄

@megamaddu
Copy link

I'm really interested in this. Maybe I'll get some time next week to learn how it works.

@hdgarrood
Copy link
Contributor

I think probably the best way forward with this is to start by contributing more complete support for ES2015+ export in language-javascript, as currently it only has partial support; see https://github.com/erikd/language-javascript/blob/d061b16146dcddb48ff9bb8627f2399832fad855/src/Language/JavaScript/Parser/Grammar7.y#L1243-L1251

@gasi
Copy link

gasi commented Mar 21, 2019

@hdgarrood: I think probably the best way forward with this is to start by contributing more complete support for ES2015+ export in language-javascript, as currently it only has partial support…

I don’t think that’s necessary. To move faster on this, we could decide to only support the minimal subset of export declarations that people would need to get stuff done in their FFI files as well as what PS would need during ES2015+ code-gen. The implemented subset should do just that:

export const 5;
export const foo = () => {...}

Beyond that, what I would propose for a gradual rollout would be:

  • PSC ES2015+ code-gen should be opt-in via CLI flag or similar.
  • FFI support for ES2015+ should use .mjs file extension, just like Node.js. This will allow PSC to choose which parse mode it should use for FFI files. IIRC, ES2015+ distinguishes between parsing a Script vs Module. A Module allows import / export only at the top-level. Update: This would also allow libraries to gradually migrate and offer both ES3 / ES2015+ FFI files during the transition.
  • Support minimal, useful subset of import + export syntax for code-gen + FFI.
  • Generating code with import + export instead of require should allow third-party tools to do proper tree shaking / dead code elimination (DCE).

WIP

image
https://github.com/gasi/purescript/pull/1/files#diff-e9b7df41e811a696213b5bf4b2cdd662

@hdgarrood
Copy link
Contributor

hdgarrood commented Apr 28, 2019

Beyond that, what I would propose for a gradual rollout would be: [...]

I also have some thoughts on this, but a PR probably isn't the best place to discuss. We have a few issues covering ES modules but they are fairly old and have a lot of discussion on them, so I think it's time to close them as well, and make a new issue for this.

edit: see #3613

@hdgarrood hdgarrood closed this Apr 28, 2019
@hdgarrood
Copy link
Contributor

and make a new issue for this

(for the avoidance of doubt I'm in the process of doing this).

@hdgarrood hdgarrood mentioned this pull request Apr 28, 2019
@Cmdv
Copy link
Contributor Author

Cmdv commented Apr 29, 2019

cheers @hdgarrood for taking this on, just couldn't ever find the time to complete it!! 👍

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

6 participants