Giter VIP home page Giter VIP logo

Comments (13)

opyate avatar opyate commented on May 31, 2024 1

The line

-js bin/lib.js # <- Compile the library, in this case to JavaScript

If this is a library, why specify a compilation target? Isn't it up to the library user to choose a target?

from haxe.org-comments.

limenleap avatar limenleap commented on May 31, 2024 1

I am summarizing what I have learnt here. This could help others. I am using Haxe 4.1.1

What is a package?
A package name is actually the name of a sub-folder within the folder given by -cp parameter in the .hxml file

So if ./src is the folder you defined the class path to be, and "foo" is the name of the package then you must have this folder inside your computer (not in the build.hxml file), thus

./src/foo

The buid.hxml is now this. If you note you have to give -cp folder as src and not src/foo !

-cp src

--dce no

foo.BarLib

-js bin/lib.js

If you now want to place the class BarLib defined in your haxe source code; then the sample code shown above on this page should be placed inside a file called BarLib.hx and that file must be in src/foo folder

I have specified the dead code elimination directive to be "no" -- this can create a very verbose file; but you are sure that whatever you wrote in the BarLib.hx would get compiled

Also, ensure that right at the top of that file (BarLIb.hx) should be the special keyword that define it to be the package. The package name should be same as the package folder name

package foo;

Then just above the BarLib class definition; write this:

@:expose

If that is not given, haxe will build the library; but you may not be able to access that class you want

Now compile using haxe as before:

haxe build.hxml

The file lib.js would be created in the bin folder. To get the exposed class; your additional code that you will use, would access your BarLib class as shown below: (in javascript. Other targets, will have similar but may not be exactly same strategies of using a namespace)

foo.BarLib

from haxe.org-comments.

Aurel300 avatar Aurel300 commented on May 31, 2024

@opyate You are thinking of a Haxe library (like the packages on haxelib), which is not what this article describes.

A Haxe library can be used with -lib when compiling, and will generally result in more classes and packages being available to use from within your Haxe code. These are then all compiled alongside with your code into whatever the target is.

On the other hand, almost all of Haxe targets have a concept of a library as well. To continue with the JavaScript example presented in the article, you can compile some Haxe code to a JavaScript library (making sure to @:expose the classes you want), then you can write regular JavaScript code making use of the compiled library. This is important to allow more types of interop between Haxe code and its targets.

from haxe.org-comments.

projectkit avatar projectkit commented on May 31, 2024

Can I expose Haxe classes to a Jar library to use it in Java code?

from haxe.org-comments.

RealyUniqueName avatar RealyUniqueName commented on May 31, 2024

I think Haxe-generated classes should be available by the same package/name they use in Haxe.

from haxe.org-comments.

limenleap avatar limenleap commented on May 31, 2024

I seem to have followed your instructions along with your example but the Haxe compiler INSISTS on not working.

It ALWAYS gives this error :-)

Type not found: foo.BarLib

I tried the following:

I wrote your code into a file called foo.hx

What do I do? I need this feature!

from haxe.org-comments.

limenleap avatar limenleap commented on May 31, 2024

The file foo.hx was placed in the src sub-folder (i.e. folder where haxe executable is present) I even tried capitalizing it as Foo.hx but NOTHING works. It always keep giving the error

Type not found: foo.BarLib

I even tried placing @keep above the BarLib class. Even that did not work

from haxe.org-comments.

limenleap avatar limenleap commented on May 31, 2024

I mean @:keep (sorry for my typo)

I tried the file name where you code was written, as per the following list and placed the file in the src folder as explained earlier. None of them worked. They ALL generated the same "Type not found" error

foo
Foo
Foo.hx
Foo.ds
foo.ds
Foo.pack
foo.pack

I even ensured that there is ONLY one source code file (with your code) in the src folder. But even then it did not work

I am using Haxe 4.1.1

from haxe.org-comments.

RealyUniqueName avatar RealyUniqueName commented on May 31, 2024

Each package denotes a directory.
You need BarLib.hx file to be placed in foo directory.

from haxe.org-comments.

limenleap avatar limenleap commented on May 31, 2024

Ah, thank you! How I wish this was properly documented! I wish there was some more info on this. Is there any page in the documentation I may have missed?

from haxe.org-comments.

RealyUniqueName avatar RealyUniqueName commented on May 31, 2024

Looks like we're missing a page about packages in the manual.

from haxe.org-comments.

limenleap avatar limenleap commented on May 31, 2024

Thanks for all the help. Now how do I actually use the BarLib class ? It seems to add foo_ before it; but since the entire lib.js is actually one big IIFE, and there are no global variables; can you give a small example where the javacript generated can actually be used? Thanks, I am grateful

from haxe.org-comments.

danielo515 avatar danielo515 commented on May 31, 2024

And here we are, two years later, and this blog post is impossible to understand and follow without the comments of @limenleap. Thank you mate for sharing your findings, I wasted a lot of time on this, but at least now I am able to run it.

from haxe.org-comments.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.