title: How do I get haxe to generate externs?
body: <p>I am writing haxe code which I want to compile to an arbitrary target as a module and then use the results from another module compiled for this same target. I don’t want to handle this the “Haxe way” (which is to fully inline all libraries at compiletime). Instead I want to be able to write distinct Haxe modules and reference them with full type safety without inlining between the modules. The natural way to do this would be to have both source Haxe files and a separate directory of “headers” filled with <code>extern</code> describing the public API of my module, with these <code>extern</code>s somehow automatically generated so that they don’t need to be manually maintained.</p>
  
  <p>I cannot figure out how to get Haxe to emit externs. It would make sense to me if <code>haxe-externs</code> were an actual <a href="https://haxe.org/documentation/introduction/compiler-targets.html" rel="nofollow noreferrer">“target platform”</a> so that I could do something like:</p>
  
  <pre><code>$ haxe ClassName -hxe externsoutdir
  </code></pre>
  
  <p>It would make less sense but still be acceptable if one of the <code>-D</code> flags like <code>-D dump</code> (which seems to sort of get one part of the way there) or some imaginary, nonexistent <code>-D dump-externs</code> existed. Then you could generate externs while compiling to your favorite target:</p>
  
  <pre><code>$ haxe ClassName -js outfile.js -D shallow-expose -D dump-externs=externsoutdir
  </code></pre>
  
  <p>The idea is to take a class definition like this:</p>
  
  <pre><code>@:expose
  class ClassName {
      function quack() {
      trace('quack');
      }
  }
  </code></pre>
  
  <p>and emit something like this in a separate directory:</p>
  
  <pre><code>extern class ClassName {
      function quack():Void;
  }
  </code></pre>
  
  <p>so that I can consume it from another module like this:</p>
  
  <pre><code>@:expose
  class MyClassName extends ClassName {
      override function quack() {
      super.quack();
      trace('…and again I say “quack”');
      }
  }
  
  $ haxe -cp path\to\externsoutdir MyClassName -js outfile.js -D shallow-expose
  </code></pre>
  
  <p>It would only make sense to generate <code>extern</code>s for things decorated with <code>@:expose</code> or some other decorator.</p>
  
  <p>I will figure out how to wrap the emitted modules to load each other correctly. That’s easy. The hard part is generating the <code>extern</code> definitions—shouldn’t Haxe already have a way to do this?</p>
  
  <p>Is there already some tool or built-in way I’m missing to do this? When Googling, all I see are projects that supposedly help with generating externs for existing JavaScript libraries. But that’s not my use case…</p>