Skip to content

Commit

Permalink
Merge pull request #3420 from fable-compiler/publish-library-ts
Browse files Browse the repository at this point in the history
Use library-ts also for JS compilation
  • Loading branch information
alfonsogarciacaro authored Apr 15, 2023
2 parents 87e823b + 8f1962c commit d9e1b31
Show file tree
Hide file tree
Showing 23 changed files with 544 additions and 278 deletions.
117 changes: 57 additions & 60 deletions build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ module Util =
let runTypeScript projectDir =
run ("npm run tsc -- --project " + projectDir)

let runTypeScriptWithArgs projectDir args =
run ("npm run tsc -- --project " + projectDir + " " + String.concat " " args)

let runFableWithArgs projectDir args =
run ("dotnet run -c Release --project src/Fable.Cli -- " + projectDir + " " + String.concat " " args)

Expand Down Expand Up @@ -123,52 +126,51 @@ module Unused =

// TARGETS ---------------------------

let buildLibraryJsWithOptions (opts: {| watch: bool |}) =
let baseDir = __SOURCE_DIRECTORY__

let projectDir = baseDir </> "src/fable-library"
let buildDir = baseDir </> "build/fable-library"
let fableOpts = [
"--outDir " + buildDir
"--fableLib " + buildDir
"--exclude Fable.Core"
"--define FX_NO_BIGINT"
"--define FABLE_LIBRARY"
if opts.watch then "--watch"
]

cleanDirs [buildDir]
runInDir baseDir "npm install"
makeDirRecursive buildDir

copyFile (projectDir </> "package.json") buildDir

if opts.watch then
Async.Parallel [
runNpmScriptAsync "tsc" [
"--project " + projectDir
"--watch"
]
runFableWithArgsAsync projectDir fableOpts
] |> runAsyncWorkflow
else
runTSLint projectDir
runTypeScript projectDir
runFableWithArgs projectDir fableOpts
removeDirRecursive (buildDir </> ".fable")

let buildLibraryJs() = buildLibraryJsWithOptions {| watch = false |}
let watchLibraryJs() = buildLibraryJsWithOptions {| watch = true |}

let buildLibraryJsIfNotExists() =
if not (pathExists (__SOURCE_DIRECTORY__ </> "build/fable-library")) then
buildLibraryJs()
// let buildLibraryJsWithOptions (opts: {| watch: bool |}) =
// let baseDir = __SOURCE_DIRECTORY__

// let projectDir = baseDir </> "src/fable-library"
// let buildDir = baseDir </> "build/fable-library"
// let fableOpts = [
// "--outDir " + buildDir
// "--fableLib " + buildDir
// "--exclude Fable.Core"
// "--define FX_NO_BIGINT"
// "--define FABLE_LIBRARY"
// if opts.watch then "--watch"
// ]

// cleanDirs [buildDir]
// runInDir baseDir "npm install"
// makeDirRecursive buildDir

// copyFile (projectDir </> "package.json") buildDir

// if opts.watch then
// Async.Parallel [
// runNpmScriptAsync "tsc" [
// "--project " + projectDir
// "--watch"
// ]
// runFableWithArgsAsync projectDir fableOpts
// ] |> runAsyncWorkflow
// else
// runTSLint projectDir
// runTypeScript projectDir
// runFableWithArgs projectDir fableOpts
// removeDirRecursive (buildDir </> ".fable")

// let buildLibraryJs() = buildLibraryJsWithOptions {| watch = false |}

// let buildLibraryJsIfNotExists() =
// if not (pathExists (__SOURCE_DIRECTORY__ </> "build/fable-library")) then
// buildLibraryJs()

let buildLibraryTs() =
let baseDir = __SOURCE_DIRECTORY__
let sourceDir = "src/fable-library"
let buildDirTs = "build/fable-library-ts"
let buildDirJs = "build/temp/fable-library-js"
let sourceDir = "./src/fable-library"
let buildDirTs = "./build/fable-library-ts"
let buildDirJs = "./build/fable-library"

cleanDirs [buildDirTs; buildDirJs]
runInDir baseDir "npm install"
Expand All @@ -187,21 +189,18 @@ let buildLibraryTs() =
copyFiles (sourceDir </> "ts") "*.json" buildDirTs
copyDirRecursive (sourceDir </> "lib") (buildDirTs </> "lib")

// Remove extra d.ts files (like Choice.d.s)
for file in IO.Directory.GetFiles buildDirTs do
if file.EndsWith(".d.ts") then removeFile file

// runTSLint buildDirTs
runInDir baseDir ("npm run tsc -- --project " + buildDirTs + " --outDir " + buildDirJs)
runTypeScriptWithArgs buildDirTs ["--outDir " + buildDirJs]
copyFile (buildDirTs </> "lib/big.d.ts") (buildDirJs </> "lib/big.d.ts")

let buildLibraryTsIfNotExists() =
if not (pathExists (__SOURCE_DIRECTORY__ </> "build/fable-library-ts")) then
buildLibraryTs()

let buildLibraryPy() =
let libraryDir = "src/fable-library-py"
let libraryDir = "./src/fable-library-py"
let projectDir = libraryDir </> "fable_library"
let buildDirPy = "build/fable-library-py"
let buildDirPy = "./build/fable-library-py"

cleanDirs [buildDirPy]

Expand Down Expand Up @@ -323,13 +322,13 @@ let buildWorker (opts: {| minify: bool; watch: bool |}) =

// Put fable-library files next to bundle
printfn "Copying fable-library..."
buildLibraryJsIfNotExists()
buildLibraryTsIfNotExists()
let libraryDir = "build/fable-library"
let libraryTarget = distDir </> "fable-library"
copyDirRecursive libraryDir libraryTarget

let buildStandalone (opts: {| minify: bool; watch: bool |}) =
buildLibraryJs()
buildLibraryTs()

printfn "Building standalone%s..." (if opts.minify then "" else " (no minification)")

Expand Down Expand Up @@ -477,12 +476,12 @@ let testProjectConfigs() =
let testIntegration() =
runInDir "tests/Integration/Integration" "dotnet run -c Release"

buildLibraryJsIfNotExists()
buildLibraryTsIfNotExists()
runInDir "tests/Integration/Compiler" "dotnet run -c Release"
testProjectConfigs()

let testJs() =
buildLibraryJsIfNotExists()
buildLibraryTsIfNotExists()

compileAndRunTestsWithMocha true "Main"

Expand Down Expand Up @@ -627,7 +626,7 @@ let buildLocalPackage pkgDir =
buildLocalPackageWith pkgDir
"tool install fable"
(resolveDir "src/Fable.Cli/Fable.Cli.fsproj") (fun version ->
buildLibraryJs()
buildLibraryTs()
updateVersionInFableTransforms version)

let testRepos() =
Expand Down Expand Up @@ -726,7 +725,6 @@ let packages =
"Fable.Core", doNothing
"Fable.Cli", (fun () ->
Publish.loadReleaseVersion "src/Fable.Cli" |> updateVersionInFableTransforms
buildLibraryJs()
buildLibraryTs()
buildLibraryPy()
buildLibraryRust()
Expand Down Expand Up @@ -780,7 +778,7 @@ match BUILD_ARGS_LOWER with
| "watch-test-dart"::_ -> testDart(true)

| "quicktest"::_ ->
buildLibraryJsIfNotExists()
buildLibraryTsIfNotExists()
watchFableWithArgs "src/quicktest" ["--watch --exclude Fable.Core --noCache --runScript"]
| "quicktest-ts"::_ ->
buildLibraryTsIfNotExists()
Expand All @@ -802,7 +800,7 @@ match BUILD_ARGS_LOWER with
buildLibraryRustIfNotExists()
watchFableWithArgs "src/quicktest-rust" ["--lang rs -e .rs --watch --exclude Fable.Core --noCache --runScript"]
| "run"::_ ->
buildLibraryJsIfNotExists()
buildLibraryTsIfNotExists()
// Don't take args from pattern matching because they're lowered
let restArgs = BUILD_ARGS |> List.skip 1 |> String.concat " "
run $"""dotnet run -c Release --project {resolveDir "src/Fable.Cli"} -- {restArgs}"""
Expand All @@ -814,8 +812,7 @@ match BUILD_ARGS_LOWER with
let pkgInstallCmd = buildLocalPackageWith (resolveDir "temp/pkg") "add package Fable.Core" (resolveDir "src/Fable.Core/Fable.Core.fsproj") ignore
printfn $"\nFable.Core package has been created, use the following command to install it:\n {pkgInstallCmd}\n"

| ("watch-library")::_ -> watchLibraryJs()
| ("fable-library"|"library")::_ -> buildLibraryJs()
| ("fable-library"|"library")::_
| ("fable-library-ts"|"library-ts")::_ -> buildLibraryTs()
| ("fable-library-py"|"library-py")::_ -> buildLibraryPy()
| ("fable-library-rust" | "library-rust")::_ -> buildLibraryRust()
Expand Down
31 changes: 16 additions & 15 deletions src/Fable.Cli/ProjectCracker.fs
Original file line number Diff line number Diff line change
Expand Up @@ -591,21 +591,22 @@ let copyDirIfDoesNotExist replaceFsprojExt (source: string) (target: string) =
copyDir replaceFsprojExt source target

let getFableLibraryPath (opts: CrackerOptions) =
match opts.FableLib with
| Some path -> Path.normalizeFullPath path
| None ->
let buildDir, libDir =
match opts.FableOptions.Language with
| Python ->
match opts.FableLib with
| Some Py.Naming.sitePackages -> "fable-library-py", "fable-library"
| _ -> "fable-library-py/fable_library", "fable_library"
| Dart -> "fable-library-dart", "fable_library"
| Rust -> "fable-library-rust", "fable-library-rust"
| TypeScript -> "fable-library-ts", "fable-library-ts"
| Php -> "fable-library-php", "fable-library-php"
| _ -> "fable-library", "fable-library" + "." + Literals.VERSION

let buildDir, libDir =
match opts.FableOptions.Language, opts.FableLib with
| Dart, None -> "fable-library-dart", "fable_library"
| Rust, None -> "fable-library-rust", "fable-library-rust"
| TypeScript, None -> "fable-library-ts", "fable-library-ts"
| Php, None -> "fable-library-php", "fable-library-php"
| JavaScript, None -> "fable-library", "fable-library" + "." + Literals.VERSION
| Python, None -> "fable-library-py/fable_library", "fable_library"
| Python, Some Py.Naming.sitePackages -> "fable-library-py", "fable-library"
| _, Some path ->
if path.StartsWith("./") then "", Path.normalizeFullPath path
elif IO.Path.IsPathRooted(path) then "", Path.normalizePath path
else "", path

if String.IsNullOrEmpty(buildDir) then libDir
else
let fableLibrarySource =
let baseDir = AppContext.BaseDirectory
baseDir
Expand Down
18 changes: 14 additions & 4 deletions src/Fable.Transforms/BabelPrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module PrinterExtensions =
// Sometimes empty IIFE remain in the AST
| CallExpression(ArrowFunctionExpression(_,(BlockStatement body),_,_,_),_,_,_) ->
body |> Array.exists isProductiveStatement
| CommentedExpression(_,e) -> hasSideEffects e
| _ -> true

and isProductiveStatement(s: Statement) =
Expand Down Expand Up @@ -274,9 +275,10 @@ module PrinterExtensions =
printer.Print(expr)
printer.Print(")")

/// Surround with parens anything that can potentially conflict with operator precedence
member printer.ComplexExpressionWithParens(expr: Expression) =
/// Should the expression be printed with parens when nested?
member printer.IsComplex(expr: Expression) =
match expr with
| CommentedExpression(_, e) -> printer.IsComplex(e)
| Undefined _
| Literal(NullLiteral _)
| Literal(Literal.StringLiteral _)
Expand All @@ -293,8 +295,13 @@ module PrinterExtensions =
| ObjectExpression _
| JsxTemplate _
| JsxElement _
| UnaryExpression _ -> printer.Print(expr)
| _ -> printer.WithParens(expr)
| UnaryExpression _ -> false
| _ -> true

/// Surround with parens anything that can potentially conflict with operator precedence
member printer.ComplexExpressionWithParens(expr: Expression) =
if printer.IsComplex(expr) then printer.WithParens(expr)
else printer.Print(expr)

member printer.ComplexTypeWithParens(t: TypeAnnotation) =
match t with
Expand Down Expand Up @@ -397,6 +404,9 @@ module PrinterExtensions =

member printer.Print(expr: Expression) =
match expr with
| CommentedExpression(comment, expr) ->
printer.Print("/* " + comment + " */ ")
printer.Print(expr)
| JsxElement(componentOrTag, props, children) -> printer.PrintJsxElement(componentOrTag, props, children)
| JsxTemplate(parts, values) -> printer.PrintJsxTemplate(parts, values)
| Super(loc) -> printer.Print("super", ?loc = loc)
Expand Down
Loading

0 comments on commit d9e1b31

Please sign in to comment.