Fable: Simple fs -> js solution?

Created on 19 Mar 2018  路  6Comments  路  Source: fable-compiler/Fable

Description

Recently I wrote some VSTS-Build-Tasks with typescript (https://github.com/matthid/Paket.TeamBuildCredentials/blob/master/SetPaketCredentialProvider.dev/setCredentialManager.ts). The primary reason to use typescript was that it was so easy to get started:

Basically npm install <> && tsc file.js is everything you need to do in order to get out of the "javascript misery".
The problem especially for build-tasks is that it feels like using fable is more overhead than just writing the task in javascript/typescript.

My question now is: Can we already do the same for fable and I just missed it? What is currently the most minimal way to write such a build task in f#?

Feel free to point me to some existing samples/documentation maybe I have just missed them.

Most helpful comment

fable-splitter 2.1 (currently in beta, so you need to install it with npm i fable-splitter@next fable-compiler@next but the stable version will be released soon) it's close to this. You can just do npx fable-splitter src -o build. Please check the README for more information.

Note that .fsx scripts are not supported at the moment because there's no standard solution yet to add package references, so for now it's easier to use .fsproj. But we can revisit it when .fsx can contain nuget/paket references and we get them resolved by the F# compiler or Dotnet.ProjInfo.

All 6 comments

I'm pretty sure there's nothing quite that simple - mainly because Fable needs both dotnet and javascript infrastructures working in tandem. I've written a summary of how to create a minimal fable application here - https://medium.com/@bilkusg/fable-f-why-and-how-14e4d3702593 which works with the latest templates which (once you've dealt with the prerequisites ), can turn a basic F# file into javascript in about 4 lines.

That's right, there are two things right now that make it a bit difficult to compile a single F# file to JS with zero-configuration:

  • Fable 1.x only accepts .fsproj files. Until Fable 0.7 it was common to use single .fsx files, but then we moved Fable dependencies to Nuget and the new .fsproj and/or Paket made it much easier to handle them, instead of adding manual references to the .dll on top of the .fsx file. I'm not sure what's the current status of Paket references in .fsx files and whether they could work with Fable or not.

  • As @bilkusg, Fable runs two processes: dotnet and node.js. It was like this from the beginning, but Fable 1.x made it explicit to make it easier to connect Fable with different JS tools (Webpack, Rollup...).

None of the points above is unsolvable: we could add .fsx support again, and also make a more opinionated CLI tool available that compiles F# to JS with a single command and no configuration. But this has not been a common scenario so far as it's usually easier to simply write JS or TS for simple scripts like the one you pointed out (in fact the file you linked is valid -modern- JS, you can just add the // @ts-check comment to get Typescript checking). Take also into account that JS/TS tooling are usually better in handling external dependencies though ts2fable is getting better and better.

However, if someone is interested in building such a tool I will gladly provide support and guidance for it :)

So I started some proof of concept of trying to do this based of @ncave's repl.

node fantasy.js ../examples/hello.fsx
Getting Checker...
Parsing Project...
Loading compiler...
Compiling To Babel Json Ast...
Compiling to codeES2015
"use strict";

var _String = require("fable-core/String");

(0, _String.toConsole)((0, _String.printf)("%s"))("hello world");

Still needs lots of love though.

That's great @TheAngryByrd! Where's your POC?

Had to give it a good cleaning before I showed it to anyone 馃樃

https://github.com/TheAngryByrd/fantasy

can try it with

./build.sh
cd dist && node fantasy ../examples/hello.fsx

fable-splitter 2.1 (currently in beta, so you need to install it with npm i fable-splitter@next fable-compiler@next but the stable version will be released soon) it's close to this. You can just do npx fable-splitter src -o build. Please check the README for more information.

Note that .fsx scripts are not supported at the moment because there's no standard solution yet to add package references, so for now it's easier to use .fsproj. But we can revisit it when .fsx can contain nuget/paket references and we get them resolved by the F# compiler or Dotnet.ProjInfo.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alfonsogarciacaro picture alfonsogarciacaro  路  3Comments

jwosty picture jwosty  路  3Comments

funlambda picture funlambda  路  4Comments

alfonsogarciacaro picture alfonsogarciacaro  路  3Comments

MangelMaxime picture MangelMaxime  路  3Comments