Alex Miller
Alex Miller

Reputation: 1626

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

I'm using some externs from haxe-js-kit, and I'm targeting client-side JS. As per the haxe-js-kit instructions, I included the following line in my build.hxml file:

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

Now when I compile, a package.json file is generated which identifies the dependencies I need to install. So then I run:

npm install

And those dependencies are installed in node_modules/. Now I'd like to package the built JS for my app (generated by the Haxe compiler) with the JS libraries I just downloaded into node_modules/. Usually, I'd use something like webpack to do this, but webpack depends on there being CommonJS or AMD dependency declarations. The Haxe compiler does not insert require statements in the compiled JS--it assumes that those dependencies will be globally available.

Haxe does provide a mechanism for putting CommonJS require statements in the compiled JS: @:jsRequire("fs"). But the author of the extern is responsible for adding this annotation to their extern classes, and it doesn't look like haxe-js-kit does this .

As a client of externs, is there a way to tell the Haxe compiler to include require statements in the output JS, so I can use a tool like webpack to package up my dependencies into a single file?

Upvotes: 1

Views: 233

Answers (1)

Jeff Ward
Jeff Ward

Reputation: 19116

You could perhaps add the metadata yourself with a compiler argument:

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

Thus, even if you don't own to source, you can decorate the class with Haxe metadata.

Alternately, you could file an issue on the haxe-js-kit repo asking why it doesn't.

Upvotes: 1

Related Questions