benmerckx / genes

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

Es6 modules in Npm repos: Possible to export to "npm module root" (corresponding to js.Node.module.export ...) ? #44

Closed cambiata closed 3 years ago

cambiata commented 3 years ago

I'm experimenting with creating npm packages through Haxe, and creating vanilla Haxe js libraries and exporting entities to the "npm module root" useing js.Node.module.export works fine - they entities then become importable using commonJs require. This is how I do it the commonJs way:

import js.Node.module;
function main() {
    module.exports.TestPlayer = player.TestPlayer;
    ...

As it's nowadays possible to use es6 modules in npm packages - is the corresponding somehow possible when using genes?

If not, could something like the following be an idea..?

// player.TestPlayer.hx
package player;

@rootExport // <-- Metadata to generate module root export as seen below
class TestPlayer {
    public function new() {}
    ...
}

would generate in the haxe compiled boostrap js file:

// index.js
import { Register } from "./genes/Register.js"
import { Main_Fields_ } from "./Main.js"
Main_Fields_.main()

// import and export to make TestPlayer reachable from the npm module root:
import { TestPlayer } from "./player/TestPlayer.js"
export { TestPlayer };

Best! / Jonas

benmerckx commented 3 years ago

Right now you can use a custom js/ts entrypoint to achieve that. Having some metadata to re-export from the main file sounds like a good feature to have though. Maybe we could use @:expose for that purpose.

benmerckx commented 3 years ago

I've just added support for @:expose which works pretty much the way you described it. Classes/enums marked with @:expose will be exported from the entrypoint.

Also note that you don't necessarily have to define a --main class for this purpose if you're not using it. You can include the needed classes or packages directly in your hxml or through a macro. haxe -js lib/index.js -lib genes player

cambiata commented 3 years ago

Ah. thanks!