将Haxe模块导入到node.js脚本中 [英] Import Haxe modules into a node.js script

查看:52
本文介绍了将Haxe模块导入到node.js脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用node.js和Haxe,有什么方法可以编写一个从Haxe文件生成node.js模块,然后返回生成的模块的函数?我开始使用Haxe编写node.js模块,我需要一种更轻松地导入模块的方法.

Using node.js and Haxe, is there any way to write a function that generates a node.js modules from a Haxe file, and then returns the generated module? I'm started writing node.js modules using Haxe, and I need a way to import the modules more easily.

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中放入一些东西.

@:expose("hello") part is to put something in module.exports.

现在启动

haxe -js haxenode.js -dce no Haxenode

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

Now you can use haxenode.js in nodejs

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

因此,这些结合在一起可以回答您的问题:

So, this combined together is an answer to your question:

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'));
    });
}

请注意,输出文件名是存根.

Mind that output filename is a stub.

但是不要那样做-更好地在构建步骤中编译haxe(带有所有必需的编译选项),然后在运行时使用常规的require.

But don't do that - better to compile haxe as build step (with all necessary compile options) and then use regular require at runtime.

这篇关于将Haxe模块导入到node.js脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆