将Haxe模块导入到node.js脚本中

使用node.js和Haxe,有没有办法编写一个函数,从Haxe文件生成一个node.js模块,然后返回生成的模块? 我开始使用Haxe编写node.js模块,而且我需要一种更容易导入模块的方法。

function requireHaxe(variableToRequire, haxeFileLocation){ //generate a JavaScript module from the Haxe file, and then return the generated JavaScript module } 

考虑一下

 //Haxenode.hx class Haxenode { @:expose("hello") public static function hello(){ return "hello"; } } 

@:expose("hello")部分是把一些东西放在module.exports

现在启动

 haxe -js haxenode.js -dce no Haxenode 

现在你可以在nodejs中使用haxenode.js

 var haxenode = require('./haxenode.js'); var hello = haxenode.hello; 

所以,这个结合在一起就是对你的问题的回答:

 var cp = require('child_process'); function requireHaxe(haxeClassPath,cb){ //generate a JavaScript module from the Haxe file, and then return the generated JavaScript module cp.exec('haxe -js haxenode.js -dce no ' + haxeClassPath,function(err){ if (err){ cb(err); return; } cb(null,require('./haxenode.js')); }); } 

注意输出文件名是一个存根。

但是不要这么做 – 最好是在编译步骤(使用所有必要的编译选项)编译haxe,然后在运行时使用常规require