Comments (13)
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.
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.
@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.
Can I expose Haxe classes to a Jar library to use it in Java code?
from haxe.org-comments.
I think Haxe-generated classes should be available by the same package/name they use in Haxe.
from haxe.org-comments.
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.
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.
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.
Each package denotes a directory.
You need BarLib.hx
file to be placed in foo
directory.
from haxe.org-comments.
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.
Looks like we're missing a page about packages in the manual.
from haxe.org-comments.
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.
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)
- [code.haxe.org] Abstract types - Email address as abstract type HOT 1
- [code.haxe.org] Beginner - Pattern matching HOT 2
- [code.haxe.org] Abstract types - Pipe using Abstract Operator Overloading HOT 2
- [haxe.org/manual] Target Details HOT 1
- [haxe.org/manual] Generic HOT 2
- [haxe.org/manual] Null Safety HOT 2
- [haxe.org/manual] Blocks HOT 3
- [haxe.org/manual] Tail Recursion Elimination (TRE) HOT 1
- [haxe.org/manual] Module Sub-Types HOT 3
- [code.haxe.org] Macros - Working with compiler flags HOT 1
- [haxe.org/manual] Extractors HOT 2
- [haxe.org/manual] Array Access (Types - Abstract) HOT 2
- [haxe.org/manual] Overriding Methods HOT 1
- [haxe.org/manual] Variable capture HOT 2
- [haxe.org/manual] Tracing Types HOT 1
- [code.haxe.org] Beginner - Using numbers HOT 2
- [haxe.org/manual] Defines (Target Details - C++) HOT 7
- [haxe.org/manual] Import defaults / import.hx HOT 1
- [haxe.org/manual] Type Parameters (Type System) HOT 10
- [haxe.org/manual] Access Control HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from haxe.org-comments.