Skip to content

Commit

Permalink
add exports option (#216)
Browse files Browse the repository at this point in the history
* add exports option

* Fix test error

* Add yargs debugging option
  • Loading branch information
ctaggart authored Mar 25, 2018
1 parent 8b92ae8 commit c4c840b
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 28 deletions.
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,21 @@
"sourceMaps": true,
"outFiles": ["${workspaceRoot}/*/*.js"]
},
{
"type": "node",
"request": "launch",
"name": "Launch Cli",
"protocol": "inspector",
"program": "${workspaceRoot}/build/ts2fable.js",
"args":[
"${workspaceRoot}/node_modules/office-ui-fabric-react/lib/index.d.ts",
"${workspaceRoot}/test-compile/OfficeReact.fs",
"-e",
"uifabric",
"office-ui-fabric-react"
],
"sourceMaps": true,
"outFiles": ["${workspaceRoot}/*/*.js"]
},
]
}
7 changes: 7 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
"type": "shell",
"label": "CliTest",
"group": "build"
},
{
"command": "fake",
"args": ["build","-t","WatchCli"],
"type": "shell",
"label": "WatchCli",
"group": "build"
}
]
}
47 changes: 29 additions & 18 deletions build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,36 +89,43 @@ Target.Create "BuildCli" (fun _ ->
)

Target.Create "RunCli" (fun _ ->
let ts2fable definitionsFiles output =
let args = definitionsFiles @ [output] |> String.concat " "
let ts2fable args =
let args = args |> String.concat " "
async {
node <| sprintf "%s %s" (distDir</>"ts2fable.js") args
node <| sprintf "%s %s" (distDir</>"ts2fable.js") args
}

[
// used by ts2fable
ts2fable ["node_modules/typescript/lib/typescript.d.ts"] "test-compile/TypeScript.fs"
ts2fable ["node_modules/@types/node/index.d.ts"] "test-compile/Node.fs"
ts2fable ["node_modules/@types/yargs/index.d.ts"] "test-compile/Yargs.fs"
ts2fable ["node_modules/typescript/lib/typescript.d.ts";"test-compile/TypeScript.fs"]
ts2fable ["node_modules/@types/node/index.d.ts";"test-compile/Node.fs"]
ts2fable ["node_modules/@types/yargs/index.d.ts";"test-compile/Yargs.fs"]

// for test-compile
ts2fable ["node_modules/vscode/vscode.d.ts"] "test-compile/VSCode.fs"
ts2fable ["node_modules/vscode/vscode.d.ts";"test-compile/VSCode.fs"]
// ts2fable ["node_modules/izitoast/dist/izitoast/izitoast.d.ts"] "test-compile/IziToast.fs"
ts2fable ["node_modules/izitoast/types/index.d.ts"] "test-compile/IziToast.fs"
ts2fable ["node_modules/electron/electron.d.ts"] "test-compile/Electron.fs"
ts2fable ["node_modules/@types/react/index.d.ts"] "test-compile/React.fs"
ts2fable ["node_modules/@types/mocha/index.d.ts"] "test-compile/Mocha.fs"
ts2fable ["node_modules/@types/chai/index.d.ts"] "test-compile/Chai.fs"
ts2fable ["node_modules/chalk/types/index.d.ts"] "test-compile/Chalk.fs"
ts2fable ["node_modules/monaco-editor/monaco.d.ts"] "test-compile/Monaco.fs"
ts2fable ["node_modules/izitoast/types/index.d.ts";"test-compile/IziToast.fs"]
ts2fable ["node_modules/electron/electron.d.ts";"test-compile/Electron.fs"]
ts2fable ["node_modules/@types/react/index.d.ts";"test-compile/React.fs"]
ts2fable ["node_modules/@types/mocha/index.d.ts";"test-compile/Mocha.fs"]
ts2fable ["node_modules/@types/chai/index.d.ts";"test-compile/Chai.fs"]
ts2fable ["node_modules/chalk/types/index.d.ts";"test-compile/Chalk.fs"]
ts2fable ["node_modules/monaco-editor/monaco.d.ts";"test-compile/Monaco.fs"]
ts2fable
[ "node_modules/@types/google-protobuf/index.d.ts"
"node_modules/@types/google-protobuf/google/protobuf/empty_pb.d.ts"
"test-compile/Protobuf.fs"
]
"test-compile/Protobuf.fs"
ts2fable ["node_modules/synctasks/dist/SyncTasks.d.ts"] "test-compile/SyncTasks.fs"
ts2fable ["node_modules/subscribableevent/dist/SubscribableEvent.d.ts"] "test-compile/SubscribableEvent.fs"
ts2fable ["node_modules/office-ui-fabric-react/lib/index.d.ts"] "test-compile/OfficeUiFabricReact.fs"
ts2fable ["node_modules/synctasks/dist/SyncTasks.d.ts";"test-compile/SyncTasks.fs"]
ts2fable ["node_modules/subscribableevent/dist/SubscribableEvent.d.ts";"test-compile/SubscribableEvent.fs"]
ts2fable
[
"node_modules/office-ui-fabric-react/lib/index.d.ts"
"test-compile/OfficeReact.fs"
"-e"
"uifabric"
"office-ui-fabric-react"
]
]
|> Async.Parallel
|> Async.RunSynchronously
Expand Down Expand Up @@ -239,6 +246,10 @@ Target.Create "WatchTest" (fun _ ->
runDotnet toolDir "fable webpack -- --config webpack.config.test.js -w"
)

Target.Create "WatchCli" (fun _ ->
runDotnet toolDir "fable webpack -- --config webpack.config.cli.js -w"
)

Target.Create "CliTest" Target.DoNothing
Target.Create "Deploy" DoNothing
Target.Create "BuildAll" Target.DoNothing
Expand Down
15 changes: 13 additions & 2 deletions src/ts2fable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,24 @@ let argv =
yargs
.usage("Usage: ts2fable some.d.ts src/Some.fs")
.command(U2.Case1 "$0 [files..]", "")
.demandOption(U2.Case1 "files", "")
.option("files", jsOptions<Yargs.Options>(fun o ->
o.alias <- Some !^"f"
o.description <- Some "input ts files and output fs file"
o.``type`` <- U5.Case1 "array" |> Some
))
.option("exports", jsOptions<Yargs.Options>(fun o ->
o.alias <- Some !^"e"
o.description <- Some "export types from files that contain the string"
o.``type`` <- U5.Case1 "array" |> Some
))
.help()
.argv

//printfn "argv %A" argv
let files = argv.["files"].Value :?> string array |> List.ofArray
let tsfiles = files |> List.filter (fun s -> s.EndsWith ".ts")
let fsfile = files |> List.tryFind (fun s -> s.EndsWith ".fs")
let exports = match argv.["exports"] with None -> [] | Some v -> v :?> string array |> List.ofArray

match tsfiles.Length, fsfile with
| 0, _ -> failwithf "Please provide the path to a TypeScript file"
Expand All @@ -30,4 +41,4 @@ match tsfiles.Length, fsfile with
if not <| fs.existsSync(!^ts) then
failwithf "TypeScript file not found: %s" ts

writeFile tsfiles fsf
writeFile tsfiles fsf exports
23 changes: 19 additions & 4 deletions src/write.fs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,30 @@ let transform (file: FsFile): FsFile =
|> extractTypeLiterals // after fixEscapeWords
|> addAliasUnionHelpers

let getFsFileOut (fsPath: string) (tsPaths: string list) =
let stringContainsAny (b: string list) (a: string): bool =
b |> List.exists a.Contains

let getFsFileOut (fsPath: string) (tsPaths: string list) (exports: string list) =
let options = jsOptions<Ts.CompilerOptions>(fun o ->
o.target <- Some ScriptTarget.ES2015
o.``module`` <- Some ModuleKind.CommonJS
)
let setParentNodes = true
let host = ts.createCompilerHost(options, setParentNodes)
let program = ts.createProgram(ResizeArray tsPaths, options, host)
let tsFiles = tsPaths |> List.map program.getSourceFile

let exportFiles =
if exports.Length = 0 then tsPaths
else
program.getSourceFiles()
|> List.ofSeq
|> List.map (fun sf -> sf.fileName)
|> List.filter (stringContainsAny exports)

for export in exportFiles do
printfn "export %s" export

let tsFiles = exportFiles |> List.map program.getSourceFile
let checker = program.getTypeChecker()

let moduleNameMap =
Expand Down Expand Up @@ -101,7 +116,7 @@ let emitFsFileOutAsLines (fsPath: string) (fsFileOut: FsFileOut) =
file.``end``()
lines |> List.ofSeq

let writeFile (tsPaths: string list) (fsPath: string): unit =
let writeFile (tsPaths: string list) (fsPath: string) exports: unit =
// printfn "writeFile %A %s" tsPaths fsPath
let fsFileOut = getFsFileOut fsPath tsPaths
let fsFileOut = getFsFileOut fsPath tsPaths exports
emitFsFileOut fsPath fsFileOut
8 changes: 4 additions & 4 deletions test/fsFileTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ let inline equal (expected: 'T) (actual: 'T): unit =

let testFsFiles tsPaths fsPath (f: FsFile list -> unit) =

let fsFileOut = getFsFileOut fsPath tsPaths
emitFsFileOut fsPath fsFileOut
let fsFileOut = getFsFileOut fsPath tsPaths []
emitFsFileOut fsPath fsFileOut
f fsFileOut.Files

let testFsFileLines tsPaths fsPath (f: string list -> unit) =

let fsFileOut = getFsFileOut fsPath tsPaths
emitFsFileOutAsLines fsPath fsFileOut
let fsFileOut = getFsFileOut fsPath tsPaths []
emitFsFileOutAsLines fsPath fsFileOut
|> f


Expand Down
25 changes: 25 additions & 0 deletions tools/webpack.config.cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var path = require("path");
var webpack = require("webpack");
var common = require("./webpack.config.common");
var fs = require('fs');

console.log("Bundling for cli...");

module.exports = {
devtool: "inline-source-map",
entry: common.config.cliEntry,
target: "node",
externals: common.config.nodeExternals,
output: {
filename: 'ts2fable.js',
path: common.config.buildDir,
devtoolModuleFilenameTemplate: info =>
path.resolve(info.absoluteResourcePath).replace(/\\/g, '/'),
},
module: {
rules: common.getModuleRules()
},
resolve: {
modules: [common.config.nodeModulesDir]
},
};

0 comments on commit c4c840b

Please sign in to comment.