cincheo / jsweet

A Java to JavaScript transpiler.
http://www.jsweet.org
Other
1.46k stars 161 forks source link

Generated AMD modules fail at load-time due to dependency cycle #169

Closed HeikkiAitakangas-Documill closed 7 years ago

HeikkiAitakangas-Documill commented 7 years ago

Java-side imports from parent packages cause a failure in the transpiled code at require-load time. Requirejs' loader runs the code for the deepest package first, which means the parent package has not yet populated it's exports object.

This can be partially worked around using fully qualified class names instead of imports, but that gets fairly unreadable with the usual Java package hierarchy depths.

A project illustrating the problem is at https://github.com/HeikkiAitakangas-Documill/jsweet-amd-bug

renaudpawlak commented 7 years ago

Wow. Great bug report, thanks!! :D Will check asap. Is it working with commonjs modules?

renaudpawlak commented 7 years ago

Ok. After double checking, it appears that this bug was already encountered while working on Angular 2 (see my blog post). After thinking more about the issue, it appeared that the way JSweet handled modules in version 1.1.x was not right for many reasons (the strategy was to created one module.ts file per package). Problems were:

So, all these taken into account, I have decided to completely rework the way JSweet generates modules in version 1.2. It is much simpler now. One Java file = TS file = one module. Requires are still automatically generated, but on a per-file strategy then. So, if you use 1.2.0-SNAPSHOT, you should not have any problems (I will send a PR to your project so that you can see the changes).

IMO, this new strategy is globally much better (and simpler). It has some small drawbacks (for instance when using fully-qualified names) that should be improved in the future. Also, in future versions, we may consider grouping several files within a file using an annotation for instance...

HeikkiAitakangas-Documill commented 7 years ago

Thanks, that does fix the loading.