Skip to content

Conversation

@RblSb
Copy link
Member

@RblSb RblSb commented Oct 9, 2025

Not sure if this covers all cases. Maybe @haxiomic knows

Closes #10615

@RblSb
Copy link
Member Author

RblSb commented Dec 12, 2025

I tested this branch on real pixijs with minimal externs and now generate imports like that:

import { Application as pixi_PixiApplication } from "./pixi.js";
var app = new pixi_Application({...});

Also added test that pixi_Application local will doesn't conflict with import.
apixi.zip

The only problem i see is that if we will generate es6 externs like

@:js.import("./pixi.js")
extern class Application {}

I'm not sure that "./pixi.js" part is universal for all web building methods with webpack/npm stuff.

@haxiomic do you have some idea how dts2hx would generate externs there?

If there is no universal way, i think it will make sense to also add this syntax:

@:js.import(pixijs_esm)

And parse -D pixijs_esm="./pixijs" re-defined by user after installing some externs with this define by default (in extraParams.hxml?)

@haxiomic
Copy link
Member

This looks good at first read. @RblSb I'm not certain I fully follow the question regarding pixijs_esm but you're right about the import behavior fragmentation so we do want to add some global -D flag control

Let's get a version of this merged as is a critical feature! I imagine we lost a lot of js users while we could not use modern js libs

@RblSb
Copy link
Member Author

RblSb commented Dec 20, 2025

I added @:js.import(my_define) syntax options to make extern libraries more useful.
This is how i see it:

  • User installs some generated es6 externs with @:js.import(threejs_path) class Model {...} types
  • (optional) Library can set default library file path in extraParams.hxml as -D threejs_path="./three.js"
  • The user will be able to override this define depending on their build methods.
  • If there is no default define provided by the library, a Define threejs_path with js library path is not specified compiler error is thrown when a class with @:js.import(threejs_path) is used.

Let me know if things can be improved.

@haxiomic
Copy link
Member

haxiomic commented Jan 25, 2026

CC @Simn, this is a massive feature for js integration, pretty much all new library use js modules which haxe currently cannot use

@Simn
Copy link
Member

Simn commented Jan 25, 2026

I don't know if this is useful or not, but if everyone says so I'm happy to merge it. Let's update the branch for green CI though.

@RblSb
Copy link
Member Author

RblSb commented Jan 25, 2026

@back2dos do you think this approach with defines is fine for haxe libs?

Copy link
Member

@haxiomic haxiomic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spent some time thinking about all the different possibilities in the wild and I'm satisfied this PR covers everything we'd need in practise. Great work @RblSb !

Just need to fix the failing test, looks like missing "type": "module" in a package.json in the test

When this is in haxe I'll add support to dts2hx

@tobil4sk
Copy link
Member

Shouldn't the tests go into tests/misc/js/projects? I think tests/misc/project is for eval.

@back2dos
Copy link
Member

back2dos commented Jan 28, 2026

@back2dos do you think this approach with defines is fine for haxe libs?

Hmm, I would say the "typical" approach is to install JS libraries via npm and import from there, with some bundler then doing the remaining work. I'm not sure what one would use relative paths for, unless the libs are checked into the repo or something.

But the cool part about this is that one could actually use the defines to point to CDN versions, e.g. -D pixijs_esm=https://esm.sh/[email protected] and skip the bundler altogether and let the browser deal with it. So yeah, I think it's fine ;)

@Simn Simn merged commit 4a4105f into HaxeFoundation:development Jan 28, 2026
49 checks passed
@RblSb RblSb deleted the js-import branch January 28, 2026 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[js] ESM import syntax support

5 participants