elsassph / haxe-modular

Haxe-JS code splitting, NPM dependencies bundling, hot-reload
116 stars 14 forks source link

Usage with hxtsdgen #91

Open fullofcaffeine opened 5 years ago

fullofcaffeine commented 5 years ago

I currently have a fork of hxgenjs that integrates hxdtsgen (plan to submit a PR soon) to generate more complete d.ts files for each of the generated js classes. I'm using those to consume Haxe classes from TS. See: https://community.haxe.org/t/gist-sample-ts-haxe-bridge-fullstack-app-using-hxgenjs/1704 and https://github.com/haxe-boilerplate/pwa-ts-haxe-sample.

1) I'm wondering if haxe-modular would work well with hxdtsgen. I assume it would fail as the post-processor would not be aware of the main.d.ts file?

What I'd like to do is to experiment using haxe-modular with hxtsdgen, maybe through haxe-webpack-loader, and be able to do something like this:

import * as hx from "app.hxml"; 

// Have ts type hints here
var classFoo = new hx.package.ClassFoo()
var classBar = new hx.anotherpackage.ClassBar()

EDIT: 2) Also, as a side-question, hxgenjs allows me to transparently target nodejs and the browser, in fact, hxgenjs doesn't care about that, it just outputs the js files, and I separate them per package (client and server), see here: https://github.com/haxe-boilerplate/pwa-ts-haxe-sample/blob/master/build.hxml. Webpack then processes the client files and typescript does its job with the server files (ts and js files).

Would that be feasible to reproduce when using haxe-modular or is it only geared towards the browser only?

Any insights appreciated :)

Thanks in advance!

elsassph commented 5 years ago

Modular supports node - either through Webpack with the Haxe loader, or using -lib modular -lib hxnodejs.

I'll need to think about the Typescript definitions question... It will be problematic with the Webpack Haxe loader because the JS is in memory so I don't know how the TS compiler would find the d.ts. Unless you're hacking quite deeply in tsc it requires physical js/dts on disk.

elsassph commented 5 years ago

hxtsdgen should work with Modular, that is the non-Webpack splitting.

I don't believe it can work with the Webpack Haxe Loader, because Haxe compilation happens dynamically when Webpack "loads" hxmls. The TypeScript compiler isn't aware of what Webpack loaders do. It would require pretty deep access to the TS compiler API to maybe implement a custom module resolver, but that isn't exposed in usual TS loaders.

fullofcaffeine commented 5 years ago

Couldn't we just generate the d.ts for the given hxml output file, akin to what https://github.com/seek-oss/css-modules-typescript-loader does?

elsassph commented 5 years ago

Er... would a .hxml.d.ts be picked by Typescript? This is something I didn't consider...