【问题标题】:Import Haxe modules into a node.js script将 Haxe 模块导入 node.js 脚本
【发布时间】:2013-01-02 02:32:17
【问题描述】:

使用 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
}

【问题讨论】:

标签: node.js haxe


【解决方案1】:

考虑一下

//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

【讨论】:

    猜你喜欢
    • 2021-08-21
    • 2023-03-04
    • 2017-05-18
    • 2010-09-21
    • 1970-01-01
    • 2021-08-16
    • 2019-11-19
    • 1970-01-01
    相关资源
    最近更新 更多