benmerckx / genes

Generates split ES6 modules and Typescript definitions from Haxe modules.
44 stars 8 forks source link

[Discussion] Consider generating sub-types into separate files as well #18

Closed kevinresol closed 6 days ago

kevinresol commented 4 years ago
// Main.hx
import Foo;
class Main {
  static function main() {
    trace(Bar);
    dynamicImport(Foo -> trace(Foo);
  }
}

// Foo.hx
class Foo() {}
class Bar() {}

The above code will not enjoy code splitting because of the generated static import for sub-type Bar:

import {Bar} from './Foo';

Could this be improved by generating sub-types into separate files too? Are there any implications?

benmerckx commented 4 years ago

One of my use cases for genes is (eventually) publishing some generated js code. To have the best interop (and ease documentation) having the same structure is quite useful.

Your scenario I think is actually more of a limitation of javascript tree shaking. If the bundler was "smarter" it should be able to split these (but none do, I tested some similar things).

But, the way things currently work, it shouldn't be too hard to add a flag to force every type to get their own module, which would allow your use case too.

benmerckx commented 4 years ago

Another option is to produce a warning if you're using dynamicImport but a type from the same module is imported outside of the callback.

kevinresol commented 4 years ago

Information like that would be useful too :+1: