Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js] ESM import syntax support #10615

Open
haxiomic opened this issue Mar 8, 2022 · 7 comments
Open

[js] ESM import syntax support #10615

haxiomic opened this issue Mar 8, 2022 · 7 comments
Milestone

Comments

@haxiomic
Copy link
Member

haxiomic commented Mar 8, 2022

JavaScript's native import syntax is gaining support and usage – the latest version of three.js no longer supports require() for some modules and so presently cannot be used with haxe haxiomic/dts2hx#110

Haxe currently has @:jsRequire which when added to externs, generates require() calls for module imports

I think we could add @:jsImport (or @:js.import) which would generate import statements instead. For example:

@:jsRequire("three/examples/jsm/controls/OrbitControls", "OrbitControls") extern class OrbitControls {
// generates
var three_examples_jsm_controls_orbitcontrols_OrbitControls = require("three/examples/jsm/controls/OrbitControls.js").OrbitControls;

This would become

@:js.import("three/examples/jsm/controls/OrbitControls.js", "OrbitControls") extern class OrbitControls {
// generates *at top of file*
import { OrbitControls as three_examples_jsm_controls_orbitcontrols_OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";

Or as Simn suggests, this may be better as

@:js.import("OrbitControls", "three/examples/jsm/controls/OrbitControls.js") 

To mirror the js import syntax

@haxiomic haxiomic changed the title ESM Module import support [js] ESM import support Mar 8, 2022
@haxiomic haxiomic changed the title [js] ESM import support [js] ESM import syntax support Mar 8, 2022
@Simn
Copy link
Member

Simn commented Mar 8, 2022

Shouldn't the order of arguments follow JS here?

Also, I hate JS.

@back2dos
Copy link
Member

back2dos commented Mar 8, 2022

FWIW here's a macro based implementation: https://github.com/back2dos/jsImport#jsimport---use-es6-imports-in-haxe

Never actually used it in production, so I can't vouch for it, but I think it highlights the three different cases rather well (as for the corresponding Haxe syntax, I'll leave you to judge its suitability).

@haxiomic
Copy link
Member Author

haxiomic commented Mar 8, 2022

@back2dos thanks for the link! Can confirm it works well, I had to add a variant where you specify the exportName: back2dos/jsImport#1 but otherwise it's perfect

@sonygod: try this:

Install @back2dos's jsImport

haxelib git jsImport https://github.com/haxiomic/jsImport

Add it to your build arguments

--library jsImport

And replace @:jsRequire with @:js.import, for orbit controls:

@:js.import("three/examples/jsm/controls/OrbitControls.js", "OrbitControls") extern class OrbitControls {

Notice we've also added .js to the file path

@Simn
Copy link
Member

Simn commented Mar 26, 2022

If everyone agrees that the jsImport approach is good, I can port that to the compiler. I don't think we have to reinvent any wheels here.

@Simn Simn added this to the Later milestone Mar 24, 2023
@9Morello
Copy link

@Simn is this planned for a Haxe 4 release?

@kLabz
Copy link
Contributor

kLabz commented May 18, 2024

Haxe 4, no

Haxe 5, who knows? 😅
Might be something to consider indeed, but we haven't talked about js target things yet

@haxiomic
Copy link
Member Author

haxiomic commented Aug 27, 2024

I'd make a case for this to be bumped into a milestone. Almost everyone is on ESM modules in js world these days and those libraries currently can't be used from haxe (including the big ones like three.js for a few years) and I think implementation difficulty is relatively small compared to the impact

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

No branches or pull requests

5 participants