使用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
}
Run Code Online (Sandbox Code Playgroud)
考虑一下
//Haxenode.hx
class Haxenode {
@:expose("hello")
public static function hello(){
return "hello";
}
}
Run Code Online (Sandbox Code Playgroud)
@:expose("hello")部分是把东西放进去module.exports.
现在推出
haxe -js haxenode.js -dce no Haxenode
Run Code Online (Sandbox Code Playgroud)
现在您可以haxenode.js在nodejs中使用
var haxenode = require('./haxenode.js');
var hello = haxenode.hello;
Run Code Online (Sandbox Code Playgroud)
所以,这个结合在一起就是你问题的答案:
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'));
});
}
Run Code Online (Sandbox Code Playgroud)
请注意输出文件名是存根.
但是不要这样做 - 最好将haxe编译为构建步骤(使用所有必需的编译选项),然后require在运行时使用常规.
| 归档时间: |
|
| 查看次数: |
817 次 |
| 最近记录: |