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

Use source-map-sharp #2337

Merged
merged 3 commits into from
Jan 8, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
node_modules/
temp/
*.fs.js
*.fs.js.map
src/fable-library/*.js
src/fable-library/BigInt/*.js

Expand Down
11 changes: 8 additions & 3 deletions src/Fable.Cli/Entry.fs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Arguments:
--cwd Working directory
-o|--outDir Redirect compilation output to a directory
--extension Extension for generated JS files (default .fs.js)
-s|--sourceMaps Enable source maps

--define Defines a symbol for use in conditional compilation
--verbose Print more info during compilation
Expand Down Expand Up @@ -159,6 +160,7 @@ type Runner =
FableLibraryPath = argValue "--fableLib" args
RootDir = rootDir
OutDir = argValueMulti ["-o"; "--outDir"] args |> Option.map normalizeAbsolutePath
SourceMaps = flagEnabled "-s" args || flagEnabled "--sourceMaps" args
ForcePkgs = flagEnabled "--forcePkgs" args
NoRestore = flagEnabled "--noRestore" args
Exclude = argValue "--exclude" args
Expand Down Expand Up @@ -190,7 +192,7 @@ let clean args dir =
argValue "--extension" args |> Option.defaultValue (defaultFileExt typescript args)

// clean is a potentially destructive operation, we need a permission before proceeding
Console.WriteLine("This will recursively delete all *{0} files in {1}", fileExt, dir)
Console.WriteLine("This will recursively delete all *{0}[.map] files in {1}", fileExt, dir)
if not(flagEnabled "--yes" args) then
Console.WriteLine("Please press 'Y' or 'y' if you want to continue: ")
let keyInfo = Console.ReadKey()
Expand All @@ -201,8 +203,11 @@ let clean args dir =

let mutable fileCount = 0
let rec recClean dir =
IO.Directory.GetFiles(dir, "*" + fileExt)
|> Array.iter (fun file ->
seq {
yield! IO.Directory.GetFiles(dir, "*" + fileExt)
yield! IO.Directory.GetFiles(dir, "*" + fileExt + ".map")
}
|> Seq.iter (fun file ->
IO.File.Delete(file)
fileCount <- fileCount + 1
Log.verbose(lazy ("Deleted " + file)))
Expand Down
5 changes: 3 additions & 2 deletions src/Fable.Cli/Fable.Cli.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>3.0.5</Version>
<PackageVersion>3.0.5</PackageVersion>
<Version>3.1.0</Version>
<PackageVersion>3.1.0-beta-001</PackageVersion>
<!-- Allow users with newer dotnet SDK to run Fable, see #1910 -->
<RollForward>Major</RollForward>
<DisableImplicitFSharpCoreReference>true</DisableImplicitFSharpCoreReference>
Expand Down Expand Up @@ -37,6 +37,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Dotnet.ProjInfo" Version="0.44.0" />
<PackageReference Include="source-map-sharp" Version="1.0.2" />
<!-- <PackageReference Include="FSharp.Core" Version="5.0.0" /> -->
</ItemGroup>
</Project>
28 changes: 23 additions & 5 deletions src/Fable.Cli/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,27 @@ module private Util =
|> FableTransforms.transformFile com
|> Fable2Babel.Compiler.transformFile com

// TODO: Dummy interface until we have a dotnet port of SourceMapGenerator
// https://github.com/mozilla/source-map#with-sourcemapgenerator-low-level-api
let map = { new BabelPrinter.SourceMapGenerator with
member _.AddMapping(_,_,_,_,_) = () }
let mapPrinter, mapGen =
if cliArgs.SourceMaps then
let mapGenerator = SourceMapSharp.SourceMapGenerator()

let print outPath = async {
let mapPath = outPath + ".map"
do! IO.File.AppendAllLinesAsync(outPath, [$"//# sourceMappingURL={IO.Path.GetFileName(mapPath)}"]) |> Async.AwaitTask
use sw = IO.File.OpenWrite(mapPath)
do! Text.Json.JsonSerializer.SerializeAsync(sw, mapGenerator.toJSON()) |> Async.AwaitTask
}

print, { new BabelPrinter.SourceMapGenerator with
member _.AddMapping(orLine, orCol, genLine, genCol, name) =
let generated: SourceMapSharp.Util.MappingIndex =
{line = genLine; column = genCol}
let original: SourceMapSharp.Util.MappingIndex =
{line = orLine; column = orCol}
mapGenerator.AddMapping(generated, original, source=com.CurrentFile, ?name=name) }
else
Async.ignore, { new BabelPrinter.SourceMapGenerator with
member _.AddMapping(_,_,_,_,_) = () }

let outPath = getOutJsPath cliArgs dedupTargetDir com.CurrentFile

Expand All @@ -173,7 +190,8 @@ module private Util =

// write output to file
let writer = new FileWriter(com.CurrentFile, outPath, cliArgs, dedupTargetDir)
do! BabelPrinter.run writer map babel
do! BabelPrinter.run writer mapGen babel
do! mapPrinter outPath

Log.always("Compiled " + File.getRelativePathFromCwd com.CurrentFile)

Expand Down
5 changes: 5 additions & 0 deletions src/Fable.Cli/RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### 3.1.0-beta-001

* Source map support @delneg
* Fix #2332: watch broken for certain directory structures @jwosty

### 3.0.5

* Fixed compiler option parsing @ncave
Expand Down
5 changes: 5 additions & 0 deletions src/Fable.Cli/Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type CliArgs =
FableLibraryPath: string option
ForcePkgs: bool
NoRestore: bool
SourceMaps: bool
Exclude: string option
Replace: Map<string, string>
RunProcess: RunProcess option
Expand Down Expand Up @@ -211,6 +212,10 @@ module Async =
disp.Dispose()
onSuccess(v)))

let ignore (_: 'a) = async {
return ()
}

module Imports =
open Fable

Expand Down
2 changes: 1 addition & 1 deletion src/Fable.Transforms/Global/Compiler.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Fable

module Literals =
let [<Literal>] VERSION = "3.0.5"
let [<Literal>] VERSION = "3.1.0-beta-001"

type CompilerOptionsHelper =
static member DefaultExtension = ".fs.js"
Expand Down