使用 externs 和目标 JavaScript 时,如何强制 Haxe 编译器使用 require 语句?

When using externs and targeting JavaScript, how do I force the Haxe compiler to use require statements?

我正在使用来自 haxe-js-kit 的一些 externs,我的目标是客户端 JS。根据 haxe-js-kit 说明,我在 build.hxml 文件中包含了以下行:

--macro npm.Package.export("package.json")

现在,当我编译时,会生成一个 package.json 文件,它标识了我需要安装的依赖项。那么我 运行:

npm install

并且这些依赖项安装在 node_modules/ 中。现在我想将我的应用程序构建的 JS(由 Haxe 编译器生成)与我刚刚下载到 node_modules/ 的 JS 库打包在一起。通常,我会使用 webpack 之类的东西来执行此操作,但 webpack 依赖于 CommonJS 或 AMD 依赖项声明。 Haxe 编译器不会在编译后的 JS 中插入 require 语句——它假定这些依赖项将全局可用。

Haxe 确实提供了一种将 CommonJS require 语句放入已编译 JS 的机制:@:jsRequire("fs"). But the author of the extern is responsible for adding this annotation to their extern classes, and it doesn't look 就像 haxe-js-kit 那样。

作为externs的客户端,有没有办法告诉Haxe编译器在输出的JS中包含require语句,这样我就可以使用webpack之类的工具将我的依赖打包成一个文件?

您或许可以使用编译器参数自行添加元数据:

--macro addMetadata('@:jsRequire("fs")', 'path.to.TheExtern')

因此,即使您不拥有源代码,也可以使用 Haxe 元数据装饰 class。

或者,您可以在 haxe-js-kit repo 上提交问题,询问为什么没有。