Skip to content

Instantly share code, notes, and snippets.

@jasononeil
Created September 6, 2013 02:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jasononeil/6458748 to your computer and use it in GitHub Desktop.
Save jasononeil/6458748 to your computer and use it in GitHub Desktop.
**Compile to separate JS Files in Haxe** In this example 1) SharedCode is compiled to sharedcode.js and must be loaded first 2) Both Module1 and Module2 reference SharedCode, but do not duplicate it's code. Look at the resulting Javascript to see the output. You would have to ensure sharedcode.js is loaded before either of the module javascript …
-main Module1
-js module1.js
--next
-main Module2
-js module2.js
--next
-D sharedcode
-js sharedcode.js
SharedCode
class Module1
{
static function main() {
SharedCode.greet("Jason");
}
}
class Module2
{
static function main() {
SharedCode.greet("Anna");
}
}

SharedCode.js

(function () { "use strict";
var SharedCode = function() { }
$hxExpose(SharedCode, "SharedCode");
SharedCode.greet = function(name) {
	console.log(name);
}
function $hxExpose(src, path) {
	var o = typeof window != "undefined" ? window : exports;
	var parts = path.split(".");
	for(var ii = 0; ii < parts.length-1; ++ii) {
		var p = parts[ii];
		if(typeof o[p] == "undefined") o[p] = {};
		o = o[p];
	}
	o[parts[parts.length-1]] = src;
}
})();

Module1.js

(function () { "use strict";
var Module1 = function() { }
Module1.main = function() {
	SharedCode.greet("Jason");
}
Module1.main();
})();

Module2.js

(function () { "use strict";
var Module2 = function() { }
Module2.main = function() {
	SharedCode.greet("Anna");
}
Module2.main();
})();
#if sharedcode
@:expose // Make sure this code is available to other JS files
#else
extern // Make sure this code isn't compiled otherwise
#end
class SharedCode
{
static function greet(name:String):Void {
trace (name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment