Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了将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.

大佬总结

以上是大佬教程为你收集整理的将Haxe模块导入node.js脚本全部内容,希望文章能够帮你解决将Haxe模块导入node.js脚本所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。