kevinresol / hxgenjs

Extensible JS generator for Haxe
57 stars 16 forks source link

ES modules #35

Closed benmerckx closed 5 years ago

benmerckx commented 5 years ago

Did you look into supporting the es module syntax? Would haxe's static intialization make it hard or can it be attempted?

benmerckx commented 5 years ago

Looking into it, should generators that use RequireGenerator accept one (and the other generators) in their constructor so those can be defined in a custom config as well?

kevinresol commented 5 years ago

I don't quite get it, what is es module syntax and why?

benmerckx commented 5 years ago

Sorry, requiring stuff via import .. from ... and export ..., basically https://github.com/HaxeFoundation/haxe/issues/8033. Modules are currently the only way to do tree shaking for js. So releasing haxe generated js as a lib would benefit from this. Seems like it's already partly there in TSExternRequireGenerator, I'll look into it.

elsassph commented 5 years ago

ES tree-shaking is irrelevant to Haxe JS output: tree-shaking only work with function exports - classes are always kept as-is so you'll have to rely on Haxe DCE. Additionally tree-shaking only works for modules without side effects which would have problems with Haxe static initialisation.

elsassph commented 5 years ago

Aside from that hxgenjs produces JS files you should be able to use any way you like in JS.

benmerckx commented 5 years ago

ES tree-shaking is irrelevant to Haxe JS output:

It is. But if you were to release haxe generated js to be consumed via npm and bundlers you'd ideally want it generated as modules because it then enters the js ecosystem.

which would have problems with Haxe static initialisation

Right. It would impose quite a few restrictions on the generated code. Still I'm interested to see where it goes. Haxe currently functions as an endpoint and not a very good one at that (it doesn't have all the features of a js bundler and probably never will). So as I see it, it makes sense to optimize output to be consumed by existing js tooling.

elsassph commented 5 years ago

hxgenjs outputs Haxe classes individually, and I believe without an entry point if desired. You must use a JS toolchain bundler unless you're running with Node.

benmerckx commented 5 years ago

I do. All I'm saying is clean js output (modules, split per file) is much preferable for existing js tools. Whether you'd like to use a bundler for your project or export generated js as an npm library (to be ultimately consumed by another bundler).

elsassph commented 5 years ago

I think it may be useful to be more specific about what exactly you think should change in hxgenjs output 😅

benmerckx commented 5 years ago

Sorry, I realized this needed a different approach which was too big of a change to bring to hxgenjs, gave it a go here: https://github.com/benmerckx/genes