Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Babel/TypeScript integration samples #236

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions samples/node/integration-babel/.babelrc
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
6 changes: 6 additions & 0 deletions samples/node/integration-babel/App.config
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
17 changes: 17 additions & 0 deletions samples/node/integration-babel/README.md
@@ -0,0 +1,17 @@
# Integration with Babel

This sample shows how to call Fable-compiled projects from Babel.

## Overview

This sample uses Fable to build a minimal "library" (`util.fs`) and call its functions from a Babel project (`index.js`).

## Running the sample

Simply run

```
fable
```

to install all prerequisites, build and run the sample.
76 changes: 76 additions & 0 deletions samples/node/integration-babel/TheLib.fsproj
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>1e44403b-7834-4842-91b1-53a5fa731c58</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>console</RootNamespace>
<AssemblyName>TheLib</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFSharpCoreVersion>4.4.0.0</TargetFSharpCoreVersion>
<Name>TheLib</Name>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<Tailcalls>false</Tailcalls>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<WarningLevel>3</WarningLevel>
<PlatformTarget>AnyCPU</PlatformTarget>
<DocumentationFile>bin\Debug\console.XML</DocumentationFile>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<Tailcalls>true</Tailcalls>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<WarningLevel>3</WarningLevel>
<PlatformTarget>AnyCPU</PlatformTarget>
<DocumentationFile>bin\Release\console.XML</DocumentationFile>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup>
<MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>
</PropertyGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '11.0'">
<PropertyGroup Condition="Exists('$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets')">
<FSharpTargetsPath>$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets</FSharpTargetsPath>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets')">
<FSharpTargetsPath>$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets</FSharpTargetsPath>
</PropertyGroup>
</Otherwise>
</Choose>
<Import Project="$(FSharpTargetsPath)" Condition="Exists('$(FSharpTargetsPath)')" />
<ItemGroup>
<None Include="App.config" />
<Compile Include="util.fs" />
</ItemGroup>
<ItemGroup>
<Reference Include="mscorlib" />
<Reference Include="FSharp.Core, Version=$(TargetFSharpCoreVersion), Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Numerics" />
</ItemGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
10 changes: 10 additions & 0 deletions samples/node/integration-babel/fableconfig.json
@@ -0,0 +1,10 @@
{
"module": "commonjs",
"sourceMaps": true,
"projFile": "./TheLib.fsproj",
"outDir": "out",
"scripts": {
"prebuild": "npm install",
"postbuild": "npm run build && node out/index World"
}
}
16 changes: 16 additions & 0 deletions samples/node/integration-babel/index.js
@@ -0,0 +1,16 @@
import { List } from 'fable-core';

import * as Util from './util';

if (process.argv.length <= 2)
console.log("Please provide an argument");
else {
const name = process.argv[2];
Util.greet(Util.reverse(name));

// Use fable-core to create a List
const list = List.ofArray([1, 2, 3, 4, 5]);

const result = Util.sum(list);
console.log(`sum([1, 2, 3, 4, 5]) = ${result}`);
}
16 changes: 16 additions & 0 deletions samples/node/integration-babel/package.json
@@ -0,0 +1,16 @@
{
"private": true,
"scripts": {
"build": "babel index.js -d out"
},
"engines": {
"fable": "^0.3.24"
},
"dependencies": {
"fable-core": "^0.1.12"
},
"devDependencies": {
"babel-cli": "^6.10.1",
"babel-preset-es2015": "^6.9.0"
}
}
14 changes: 14 additions & 0 deletions samples/node/integration-babel/util.fs
@@ -0,0 +1,14 @@
module Util

let reverse (s: string) =
s
|> Seq.rev
|> Seq.map string
|> String.concat ""

let greet s =
printfn "Hello %s!" s

let sum (list: int list) =
List.fold (fun acc i -> acc + i) 0 list

6 changes: 6 additions & 0 deletions samples/node/integration-typescript/App.config
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
19 changes: 19 additions & 0 deletions samples/node/integration-typescript/README.md
@@ -0,0 +1,19 @@
# Integration with TypeScript

This sample shows how to call Fable-compiled projects from TypeScript.

## Overview

This sample uses Fable to build a minimal "library" (`util.fs`) and call its functions from a TypeScript project (`index.ts`).

Please note that an `util.d.ts` declaration file is needed to import the code. In the future it may be generated automatically by Fable.

## Running the sample

Simply run

```
fable
```

to install all prerequisites, build and run the sample.
76 changes: 76 additions & 0 deletions samples/node/integration-typescript/TheLib.fsproj
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>1e44403b-7834-4842-91b1-53a5fa731c58</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>console</RootNamespace>
<AssemblyName>TheLib</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFSharpCoreVersion>4.4.0.0</TargetFSharpCoreVersion>
<Name>TheLib</Name>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<Tailcalls>false</Tailcalls>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<WarningLevel>3</WarningLevel>
<PlatformTarget>AnyCPU</PlatformTarget>
<DocumentationFile>bin\Debug\console.XML</DocumentationFile>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<Tailcalls>true</Tailcalls>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<WarningLevel>3</WarningLevel>
<PlatformTarget>AnyCPU</PlatformTarget>
<DocumentationFile>bin\Release\console.XML</DocumentationFile>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup>
<MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>
</PropertyGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '11.0'">
<PropertyGroup Condition="Exists('$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets')">
<FSharpTargetsPath>$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets</FSharpTargetsPath>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets')">
<FSharpTargetsPath>$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets</FSharpTargetsPath>
</PropertyGroup>
</Otherwise>
</Choose>
<Import Project="$(FSharpTargetsPath)" Condition="Exists('$(FSharpTargetsPath)')" />
<ItemGroup>
<None Include="App.config" />
<Compile Include="util.fs" />
</ItemGroup>
<ItemGroup>
<Reference Include="mscorlib" />
<Reference Include="FSharp.Core, Version=$(TargetFSharpCoreVersion), Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Numerics" />
</ItemGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
10 changes: 10 additions & 0 deletions samples/node/integration-typescript/fableconfig.json
@@ -0,0 +1,10 @@
{
"module": "commonjs",
"sourceMaps": true,
"projFile": "./TheLib.fsproj",
"outDir": "out",
"scripts": {
"prebuild": "npm install",
"postbuild": "npm run build && node out/index World"
}
}
18 changes: 18 additions & 0 deletions samples/node/integration-typescript/index.ts
@@ -0,0 +1,18 @@
/// <reference path="node_modules/@types/node/index.d.ts" />

import { List } from 'fable-core';

import * as Util from './util';

if (process.argv.length <= 2)
console.log("Please provide an argument");
else {
const name = process.argv[2];
Util.greet(Util.reverse(name));

// Use fable-core to create a List
const list = List.ofArray([1, 2, 3, 4, 5]);

const result = Util.sum(list);
console.log(`sum([1, 2, 3, 4, 5]) = ${result}`);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions samples/node/integration-typescript/package.json
@@ -0,0 +1,16 @@
{
"private": true,
"scripts": {
"build": "tsc"
},
"engines": {
"fable": "^0.3.24"
},
"dependencies": {
"@types/node": "^4.0.26-alpha",
"fable-core": "^0.1.12"
},
"devDependencies": {
"typescript": "^1.8.10"
}
}
11 changes: 11 additions & 0 deletions samples/node/integration-typescript/tsconfig.json
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"outDir": "out",
"sourceMap": true
},
"files": [
"index.ts"
]
}
3 changes: 3 additions & 0 deletions samples/node/integration-typescript/util.d.ts
@@ -0,0 +1,3 @@
export function reverse(s: string): string;
export function greet(s: string): void;
export function sum(list: any): number;
14 changes: 14 additions & 0 deletions samples/node/integration-typescript/util.fs
@@ -0,0 +1,14 @@
module Util

let reverse (s: string) =
s
|> Seq.rev
|> Seq.map string
|> String.concat ""

let greet s =
printfn "Hello %s!" s

let sum (list: int list) =
List.fold (fun acc i -> acc + i) 0 list