Haxe JavaSrcipt module loading

222 views
Skip to first unread message

Balázs Zubák

unread,
Sep 18, 2015, 2:26:14 AM9/18/15
to Haxe
Hi all,

I'm just trying to put together a javascript project in Haxe. My goal is to use the well known requirejs system for all my project files.

So what i try to do now is to build my project files individually and separately into js files. With my build.hxml is simple:
-main Main
-js out/production/ExMachinaHaxe/debug/Main.js
-cp src


--next


-js out/production/ExMachinaHaxe/debug/t/Test.js
-cp src
t
.Test




So it works great, expect one thing:

When i need to use my t.Test class from the Main, i have to import it. But this way the Main.js output will contain the Test class too, instead of just refer to it like this:
var Test = require("./t/Test"); 

So i think there should be an option in haxe compiler to convert every import tag to a require loading. 

My final goal is to compile all of my project files into individual js files, where the files refer to each other through require(...). 
After I want to use a builder tool to concat the hundreds of js files into a few bigger modules, according my plans for load optimalization.

Hope you get my point and can recommend something :)

Thanks a lot!
Message has been deleted

Balázs Zubák

unread,
Sep 18, 2015, 5:49:47 AM9/18/15
to haxe...@googlegroups.com
For sure i saw it.

But it's for externs. I don't want to create extern file for all of my source files. It would be a big overhead. Because these files are not externs, these are my real sources, but want them to compile individually, but refer to each other through require(...). 
Or maybe I miss a point?

On Fri, Sep 18, 2015 at 11:38 AM, Marcin Kotz <er4...@gmail.com> wrote:

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Timothy Farrell

unread,
Sep 18, 2015, 10:20:21 AM9/18/15
to Haxe
Hi Balázs,

I've been working on exactly this problem here: https://github.com/explorigin/modular-js

It's not yet ready for prime-time but it might work for your project.  Give it a shot and give me some feedback.

-t

vroad

unread,
Sep 19, 2015, 12:28:20 AM9/19/15
to Haxe
I'm working on separate compilation for nodejs target of OpenFL.


This tool is fork of yar3333's haxe-codegen, which lets you create externs from your existing Haxe code.
The problem is that not all classes can be generated correctly with this tool. I manually modify such source code to be processed correctly, or exclude from extern generation and just use original one.
I'm trying this in these repositories.


compileSharedCode.bat will create externs in hxclasses automatically.

Balázs Zubák

unread,
Sep 19, 2015, 7:24:33 AM9/19/15
to Haxe
Hi!

Thanks a lot! I've installed the lib and added to my hxml, but still compiles the dependencies in my main file.
Can you explain a bit more about the usage?
How to reference the other classes?

Thanks!

Timothy Farrell

unread,
Sep 19, 2015, 1:54:30 PM9/19/15
to Haxe
I'm not sure.  I work with it just like the README states.  Can you post your hxml and the output of

haxelib list

I don't have any built-in support for the jsRequire metatag yet so it only works with Haxe classes so far.

Balázs Zubák

unread,
Sep 20, 2015, 4:40:34 AM9/20/15
to Haxe
I'm using the following hxml:
-lib modular-js
-main Main
-js out/production/ExMachinaHaxe/debug/Main.js
-cp src

--next
-lib modular-js
-js out/production/ExMachinaHaxe/debug/t/Test.js
-cp src
t.Test

And here is the haxelib list result:
d:\Programs\Haxe\haxe>haxelib list
modular-js: [0.8.0]

Balázs Zubák

unread,
Sep 20, 2015, 4:56:18 AM9/20/15
to Haxe
Okay now i fully simplified my hxml like this:
-lib modular-js
--macro modular.js.JsGenerator.use()
-js out.js
-cp src

Now it shows an error like this:
Error: (Warning) Joining packages Math and Std
Information:D:\Programs\Haxe\haxe\std/neko/_std/sys/io/File.hx:54: characters 32-84 : /Main.js: Invalid argument
Information:D:\Programs\Haxe\haxe\std/neko/_std/sys/io/File.hx:38: characters 10-21 : Called from
Information:D:\Programs\Haxe\haxe\lib\modular-js/0,8,0/src/modular/js/JsGenerator.hx:941: characters 3-55 : Called from
Information:D:\Programs\Haxe\haxe\lib\modular-js/0,8,0/src/modular/js/JsGenerator.hx:957: characters 46-77 : Called from

Any idea?

Balázs Zubák

unread,
Sep 20, 2015, 6:39:27 AM9/20/15
to Haxe
Finally it works, but a bit strange. 

1. My hxml looks like this now:
-lib modular-js
-js out.js
-cp src
Main
--macro modular.js.JsGenerator.use()

2. I had to modify in the script the following line:
JsGenerator.hx 940:
filePath += './$filename.js';
So i had to add the . (dot) to the path because it didn't like the absolute path.

3. Still don't know why, but it tries to generate some Std.js but i don't use it so i don't really understand why... I use the newest version of haxe.


Anyway now i can look into the generated code. My problem is now that it only supports AMD, which is not really used anymore. Everyone use Browserify that is based on CommonJS. 
So my feeling is that it won't be the real solution for what i am looking for. 

Balázs Zubák

unread,
Sep 20, 2015, 7:12:10 AM9/20/15
to Haxe
Hello vroad,

I've tried your generator. It looks good, generate the externs with the jsrequire tags. 
But after what to do? Should i refer to these files in my original class instead of the real ones? Or what is your idea?

Timothy Farrell

unread,
Sep 20, 2015, 2:20:54 PM9/20/15
to Haxe
Ah yes, I fixed that bug but forgot to update haxelib.  You might try it now.

vroad

unread,
Sep 21, 2015, 8:30:31 AM9/21/15
to Haxe
Hello Balázs, that should be correct. Haxe compiler will detect @:jsRequire meta data and generate require calls in your JavaScript automatically.
You should export classes manually from library like this for now, though.

https://github.com/vroad/lime/blob/be77441c7905dfc500fe946819cda7346397bda3/docs/ImportAll.hx#L228

Are you working on nodejs?
On nodejs, you need to create package.json so that node can find js file. Place js and json in node_modules with "npm install" or "npm link", whichever way you want.
I've never tried this on web browsers, but I know that Browserify offers similar functionalities as nodejs's require.
Reply all
Reply to author
Forward
0 new messages