+<span class=grvsc-line><span class=grvsc-source><span class=mtk9>test</span><span class=mtk1>(</span><span class=mtk9>MyUnion_Baz</span><span class=mtk1>())</span></span></span></code></pre><h2 id=compilation-options>Compilation options<a href=#compilation-options aria-hidden=true tabindex=-1><span class=anchor>#</span></a></h2><p>In order to compile to TypeScript, you only need to pass the <code>--lang ts</code> option to Fable tool, but let's have a look at <a href=https://github.com/alfonsogarciacaro/compost/blob/e783ed687bc19887f02aa0b071585a1a152c8956/package.json#L7-L11>the extra options in the compostjs example</a>:<pre class="atom-one-light grvsc-container"data-index=13 data-language><code class=grvsc-code><span class=grvsc-line><span class=grvsc-source>dotnet fable src/compost -o src/compost-ts --lang ts --fableLib fable-library --noReflection</span></span></code></pre><ul><li><code>--fableLib fable-library</code>: So far, Fable has been embedding the library files in each project, but from now on we are also <a href=https://www.npmjs.com/package/fable-library>distributing it through npm</a>. By using this compiler option you can tell Fable to use the npm package instead of the embedded files. This is is particularly useful if a consumer installs two or more libraries compiled with Fable, to avoid duplication of the library files.</ul><blockquote><p>Since Fable 4.1.2, when the tool starts, after printing the compiler version it will aslo tell you the minimum fable-library version compatible with the generated code.</blockquote><ul><li><code>--noReflection</code>: By default Fable emits helpers for each declared type containing reflection information. These are used, for example, when generating <a href=https://thoth-org.github.io/Thoth.Json/documentation/auto/introduction.html>auto coders with Thoth.Json</a>. You don't need to care much about them because if not used they will be removed by tree shaking. But if you are not using reflection you can select this option to reduce the amount of generated code.</ul><p>If publishing the library to npm, you will also need an extra step to compile the TypeScript code to JavaScript and generate the declaration files. You can use the TypeScript compiler for that like follows:<pre class="atom-one-light grvsc-container"data-index=14 data-language><code class=grvsc-code><span class=grvsc-line><span class=grvsc-source>npm run fable && tsc --outDir ./dist --declaration --noEmit false</span></span></code></pre><p>In most situations you will also need a <a href=https://www.typescriptlang.org/docs/handbook/tsconfig-json.html>tsconfig.json file</a>. You can use <a href=https://github.com/alfonsogarciacaro/compost/blob/e783ed687bc19887f02aa0b071585a1a152c8956/tsconfig.json>the one in the compostjs example</a> as reference.</p><br><hr><br><p>That was it! There are still a few things missing in TypeScript compilation, but we believe it's already in a state that will let you integrate F# in TypeScript projects and/or write npm libraries in F# with confidence. We hope you find it useful and we are looking forward for all the great things you are going to build with F# and Fable. Make sure to let us know!</div></div></div></div></div></div><script async="" src="/resources/nacara-standard-layouts/scripts/menu.js"></script></body></html>
0 commit comments