@@ -3,6 +3,266 @@ title: TypeScript
33layout : standard
44---
55
6+ This section is specific to TypeScript targetting, it will guide you through the process of setting up your project and using Fable with TypeScript.
7+
8+ We will cover the basics of using Node.js and [ Vite] ( https://vitejs.dev/ ) for the browser, but you can use any tools you want.
9+
10+ ## Node.js
11+
12+ ::: info
13+ Please make sure you followed the [ Fable setup guide] ( /docs/2-steps/your-first-fable-project ) before continuing.
14+ :::
15+
16+ In this section, we are going to see how to run Fable code using Node.js.
17+
18+ When using JavaScript, you will need a ` package.json ` to manage your dependencies.
19+ This file also allows you to confiture the type of ` module ` that Node.js use to interpret your code.
20+
21+ <ul class =" textual-steps " >
22+
23+ <li >
24+
25+ Generate a ` package.json ` file.
26+
27+ ``` bash
28+ npm init -y
29+ ```
30+
31+ </li >
32+
33+ <li >
34+
35+ Add the following line to the generated ` package.json ` file:
36+
37+ ``` json
38+ "type" : " module" ,
39+ ```
40+
41+ It should looks something like that now:
42+
43+ ``` json
44+ {
45+ "name" : " my-fable-project" ,
46+ "version" : " 1.0.0" ,
47+ "type" : " module" ,
48+ "description" : " " ,
49+ "main" : " Program.fs.js" ,
50+ "scripts" : {
51+ "test" : " echo \" Error: no test specified\" && exit 1"
52+ },
53+ "keywords" : [],
54+ "author" : " " ,
55+ "license" : " ISC"
56+ }
57+ ```
58+
59+ </li >
60+
61+ <li >
62+
63+ Install TypeScript compiler and Node.js type definitions.
64+
65+ ``` bash
66+ npm i -D typescript @types/node
67+ ```
68+
69+ </li >
70+
71+ <li >
72+
73+ Use TypeScript to compile your F# code.
74+
75+ ``` bash
76+ npx tsc Program.fs --target es2022
77+ ```
78+
79+ </li >
80+
81+ <li >
82+
83+ Run your code.
84+
85+ ``` bash
86+ node Program.fs.js
87+ ```
88+
89+ You should see ` Hello from F# ` in your terminal.
90+
91+ </li >
92+
93+ <li >
94+
95+ When targeting the Node.JS, you will probably want to have access to the Node.JS API.
96+
97+ To do so, you can use the [ Fable.Node] ( https://github.com/fable-compiler/fable-node ) .
98+
99+ ``` bash
100+ dotnet add package Fable.Node
101+ ```
102+
103+ </li >
104+
105+ <li >
106+
107+ Change the content of ` Program.fs ` to the following:
108+
109+ ``` fs
110+ open Node.Api
111+
112+ fs.writeFileSync("test.txt", "Hello World")
113+ ```
114+
115+ </li >
116+
117+ <li >
118+
119+ Run Fable and TypeScript in watch mode.
120+
121+ The follwing command start Fable in watch mode, and after the first Fable compilation, it will start TypeScript in watch mode.
122+
123+ In the [ TypeSript] ( ) section, we will learn how to use [ Fun.Build] ( )
124+ to organize and simplify our build process.
125+
126+ ``` bash
127+ dotnet fable watch --lang typescript --run npx tsc Program.fs.ts --target es2022 --watch --preserveWatchOutput
128+ ```
129+
130+ If you run your node script again, you should see a new file ` test.txt ` with the content ` Hello World ` .
131+
132+ Try changing the content of ` Hello World ` to something else and re-run your script.
133+
134+ You should see that Fable and TypeScript re-compile your code and the file content changed.
135+ </li >
136+
137+ </ul >
138+
139+ ## Browser
140+
6141::: info
7- This section is under construction.
8- :::
142+ Please make sure you followed the [ Fable setup guide] ( /docs/2-steps/your-first-fable-project ) before continuing.
143+ :::
144+
145+ In this section, we are going to use [ Vite] ( https://vitejs.dev/ ) as a development server to run our code in the browser.
146+
147+ If you want to use another bundler, please refer to their respective documentation.
148+
149+ <ul class =" textual-steps " >
150+
151+ <li >
152+
153+ Generate a ` package.json ` file.
154+
155+ ``` bash
156+ npm init -y
157+ ```
158+
159+ </li >
160+
161+ <li >
162+
163+ Install Vite.
164+
165+ ``` bash
166+ npm i -D vite
167+ ```
168+
169+ </li >
170+
171+ <li >
172+
173+ Create ` vite.config.ts ` file, and add the following content:
174+
175+ ``` ts
176+ import { defineConfig } from ' vite'
177+
178+ // https://vitejs.dev/config/
179+ export default defineConfig ({
180+ clearScreen: false ,
181+ server: {
182+ watch: {
183+ ignored: [
184+ " **/*.fs" // Don't watch F# files
185+ ]
186+ }
187+ }
188+ })
189+ ```
190+
191+ </li >
192+
193+ <li >
194+
195+ Create ` index.html ` file, and add the following content:
196+
197+ ``` html
198+ <!DOCTYPE html>
199+ <html lang =" en" >
200+ <head >
201+ <meta charset =" UTF-8" />
202+ <meta name =" viewport" content =" width=device-width, initial-scale=1.0" />
203+ <title >Fable</title >
204+ </head >
205+ <body >
206+ <div id =" root" ></div >
207+ <script type =" module" src =" /Program.fs.ts" ></script >
208+ </body >
209+ </html >
210+ ```
211+
212+ This file is the entry point of your application. It will load the generated JavaScript file.
213+
214+ </li >
215+
216+ <li >
217+
218+ Run Vite and go to the indicated URL.
219+
220+ ``` bash
221+ npx vite
222+ ```
223+
224+ If you open the browser console, you should see ` Hello from F# ` .
225+
226+ </li >
227+
228+ <li >
229+
230+ When targeting the Browser, you will probably want to have access to the Browser API.
231+
232+ To do so, you can use the [ Fable.Browser.Dom] ( https://github.com/fable-compiler/fable-browser ) .
233+
234+ ``` bash
235+ dotnet add package Fable.Browser.Dom
236+ ```
237+
238+ </li >
239+
240+ <li >
241+
242+ Change the content of ` Program.fs ` to the following:
243+
244+ ``` fs
245+ open Browser
246+
247+ let div = document.createElement "div"
248+ div.innerHTML <- "Hello world!"
249+ document.body.appendChild div |> ignore
250+ ```
251+
252+ </li >
253+
254+ <li >
255+
256+ Run Fable in watch mode and Vite in parallel.
257+
258+ ``` bash
259+ dotnet fable watch --lang typescript --run npx vite
260+ ```
261+
262+ If you open the browser, you should see ` Hello world! ` .
263+
264+ Try changing the content of ` Program.fs ` and see the result in the browser.
265+
266+ </li >
267+
268+ </ul >
0 commit comments