Skip to content

Commit

Permalink
Remove StringBuilder from BabelWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Nov 27, 2023
1 parent 546f016 commit f4cd67f
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/Fable.Compiler/Library.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Fable.Compiler.CodeServices

open System
open System.Text
open System.IO
open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.SourceCodeServices
open Fable
Expand All @@ -24,24 +24,29 @@ type BabelWriter
let fileExt = ".js"
let sourceDir = Path.GetDirectoryName(sourcePath)
let targetDir = Path.GetDirectoryName(targetPath)
let memoryStream = new IO.MemoryStream()
let stream = new IO.StreamWriter(memoryStream)
let memoryStream = new MemoryStream()
let streamWriter = new StreamWriter(memoryStream)
do streamWriter.NewLine <- "\n"

let sb = StringBuilder()
// let mapGenerator = lazy (SourceMapSharp.SourceMapGenerator(?sourceRoot = cliArgs.SourceMapsRoot))

override x.ToString() = sb.ToString()
member x.ReadContentAsString() : Async<string> =
async {
do! streamWriter.FlushAsync() |> Async.AwaitTask
memoryStream.Position <- 0L
let streamReader = new StreamReader(memoryStream)
return! (streamReader.ReadToEndAsync() |> Async.AwaitTask)
}

interface Printer.Writer with
// Don't dispose the stream here because we need to access the memory stream to check if file has changed
member _.Dispose() = ()

member _.Write(str) =
sb.Append(str) |> ignore
stream.WriteAsync(str) |> Async.AwaitTask
streamWriter.WriteAsync(str) |> Async.AwaitTask

member _.MakeImportPath(path) =
let projDir = IO.Path.GetDirectoryName(projectFile)
let projDir = Path.GetDirectoryName(projectFile)

let path =
// TODO: Check precompiled out path for other languages too
Expand Down Expand Up @@ -120,7 +125,7 @@ let compileFileToJs
)

do! BabelPrinter.run writer babel
let output = writer.ToString()
let! output = writer.ReadContentAsString()
return output
}

Expand Down

0 comments on commit f4cd67f

Please sign in to comment.