6.1.2 Target-Specific Files

since Haxe 4.0.0

In addition to surrounding individual pieces of code with compile-time checks, it is possible to provide completely separate target-specific module alternatives to the compiler. This feature works as follows:

  • When the compiler finds the file <Module>.hx, it then checks the containing directory for a file called <Module>.<target>.hx, where <target> is the name of the current target.
  • The main file for the module (without a target-specific extension) must exist.
  • If a target-specific file is found for the module, the main file is not loaded at all. Keep in mind that this means errors are not checked in the main file.

As an example, we can have the following directory structure:

/somepackage/Example.hx
/somepackage/Example.js.hx
/Main.hx

In Main.hx we can use somepackage.Example module. This module is defined in the file somepackage/Example.hx. However, if we compile for JavaScript, the module is instead defined in the file somepackage/Example.js.hx.