Skip to content

Commit

Permalink
Merge pull request #2103 from fable-compiler/loader-cache
Browse files Browse the repository at this point in the history
Cache for JS files
  • Loading branch information
alfonsogarciacaro authored Jul 6, 2020
2 parents 2a54391 + 564db8a commit e84aa4d
Show file tree
Hide file tree
Showing 19 changed files with 667 additions and 344 deletions.
6 changes: 5 additions & 1 deletion build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ let buildTypescript projectDir =
let buildFableSplitter() =
buildTypescript "src/fable-splitter"

let buildFableLoader() =
buildTypescript "src/fable-loader"

let buildSplitterWithArgs projectDir args =
if pathExists "src/fable-splitter/dist" |> not then
buildFableSplitter()
Expand Down Expand Up @@ -290,7 +293,7 @@ let packages =
"fable-babel-plugins", doNothing
"fable-compiler", buildCompiler
"fable-compiler-js", buildCompilerJs
"fable-loader", doNothing
"fable-loader", buildFableLoader
"fable-metadata", doNothing
"fable-publish-utils", doNothing
"fable-splitter", buildFableSplitter
Expand Down Expand Up @@ -323,6 +326,7 @@ match argsLower with
| ("fable-compiler"|"compiler")::_ -> buildCompiler()
| ("fable-compiler-js"|"compiler-js")::_ -> buildCompilerJs()
| ("fable-splitter"|"splitter")::_ -> buildFableSplitter()
| ("fable-loader"|"loader")::_ -> buildFableLoader()
| ("fable-standalone"|"standalone")::_ -> buildStandalone()
| "download-standalone"::_ -> downloadStandalone()
| "publish"::restArgs -> publishPackages restArgs
Expand Down
340 changes: 198 additions & 142 deletions src/Fable.Cli/Agent.fs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/Fable.Cli/Parser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Message =
rootDir: string
define: string[]
noReferences: bool
noRestore: bool
typedArrays: bool
clampByteArrays: bool
classTypes: bool
Expand Down Expand Up @@ -77,6 +78,7 @@ let parse (msg: string) =
|> Array.append [|Naming.fableCompilerConstant|]
|> Array.distinct
noReferences = parseBoolean false "noReferences" json
noRestore = parseBoolean false "noRestore" json
typedArrays = parseBoolean false "typedArrays" json
clampByteArrays = parseBoolean false "clampByteArrays" json
classTypes = parseBoolean false "classTypes" json
Expand Down
50 changes: 30 additions & 20 deletions src/Fable.Cli/ProjectCracker.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ open FSharp.Compiler.SourceCodeServices
open Fable
open Globbing.Operators

type Options = {
define: string[]
noReferences: bool
noRestore: bool
rootDir: string
projFile: string
}

let isSystemPackage (pkgName: string) =
pkgName.StartsWith("System.")
|| pkgName.StartsWith("Microsoft.")
Expand Down Expand Up @@ -234,12 +242,15 @@ let private isUsefulOption (opt : string) =
/// and get F# compiler args from an .fsproj file. As we'll merge this
/// later with other projects we'll only take the sources and the references,
/// checking if some .dlls correspond to Fable libraries
let fullCrack noReferences (projFile: string): CrackedFsproj =
let fullCrack (opts: Options): CrackedFsproj =
let projFile = opts.projFile
// Use case insensitive keys, as package names in .paket.resolved
// may have a different case, see #1227
let dllRefs = Dictionary(StringComparer.OrdinalIgnoreCase)
// Try restoring project
if projFile.EndsWith(".fsproj") then
if opts.noRestore then
Log.always "Skipping restore..."
else
Process.runCmd Log.always
(IO.Path.GetDirectoryName projFile)
"dotnet" ["restore"; IO.Path.GetFileName projFile]
Expand All @@ -265,7 +276,7 @@ let fullCrack noReferences (projFile: string): CrackedFsproj =
else
(Path.normalizeFullPath line)::src, otherOpts)
let projRefs =
if noReferences then []
if opts.noReferences then []
else
projRefs |> List.choose (fun projRef ->
// Remove dllRefs corresponding to project references
Expand Down Expand Up @@ -307,7 +318,7 @@ let easyCrack (projFile: string): CrackedFsproj =
PackageReferences = []
OtherCompilerOptions = [] }

let getCrackedProjectsFromMainFsproj noReferences (projFile: string) =
let getCrackedProjectsFromMainFsproj (opts: Options) =
let rec crackProjects (acc: CrackedFsproj list) (projFile: string) =
let crackedFsproj =
match acc |> List.tryFind (fun x -> x.ProjectFile = projFile) with
Expand All @@ -316,29 +327,29 @@ let getCrackedProjectsFromMainFsproj noReferences (projFile: string) =
// Add always a reference to the front to preserve compilation order
// Duplicated items will be removed later
List.fold crackProjects (crackedFsproj::acc) crackedFsproj.ProjectReferences
let mainProj = fullCrack noReferences projFile
let mainProj = fullCrack opts
let refProjs =
List.fold crackProjects [] mainProj.ProjectReferences
|> List.distinctBy (fun x -> x.ProjectFile)
refProjs, mainProj

let getCrackedProjects define noReferences (projFile: string) =
match (Path.GetExtension projFile).ToLower() with
let getCrackedProjects (opts: Options) =
match (Path.GetExtension opts.projFile).ToLower() with
| ".fsx" ->
getProjectOptionsFromScript define projFile
getProjectOptionsFromScript opts.define opts.projFile
| ".fsproj" ->
getCrackedProjectsFromMainFsproj noReferences projFile
getCrackedProjectsFromMainFsproj opts
| s -> failwithf "Unsupported project type: %s" s

// It is common for editors with rich editing or 'intellisense' to also be watching the project
// file for changes. In some cases that editor will lock the file which can cause fable to
// get a read error. If that happens the lock is usually brief so we can reasonably wait
// for it to be released.
let retryGetCrackedProjects define noReferences (projFile: string) =
let retryGetCrackedProjects opts =
let retryUntil = (DateTime.Now + TimeSpan.FromSeconds 2.)
let rec retry () =
try
getCrackedProjects define noReferences projFile
getCrackedProjects opts
with
| :? IOException as ioex ->
if retryUntil > DateTime.Now then
Expand All @@ -357,7 +368,7 @@ let createFableDir rootDir =
let fableDir = IO.Path.Combine(rootDir, Naming.fableHiddenDir)
if isDirectoryEmpty fableDir then
Directory.CreateDirectory(fableDir) |> ignore
File.WriteAllText(IO.Path.Combine(fableDir, ".gitignore"), "*.*")
File.WriteAllText(IO.Path.Combine(fableDir, ".gitignore"), "**/*")
fableDir

let copyDirIfDoesNotExist (source: string) (target: string) =
Expand Down Expand Up @@ -398,13 +409,12 @@ let removeFilesInObjFolder sourceFiles =
let reg = System.Text.RegularExpressions.Regex(@"[\\\/]obj[\\\/]")
sourceFiles |> Array.filter (reg.IsMatch >> not)

let getFullProjectOpts (define: string[]) (noReferences: bool) (rootDir: string) (projFile: string) =
let projFile = Path.GetFullPath(projFile)
if not(File.Exists(projFile)) then
failwith ("File does not exist: " + projFile)
let projRefs, mainProj = retryGetCrackedProjects define noReferences projFile
let getFullProjectOpts (opts: Options) =
if not(File.Exists(opts.projFile)) then
failwith ("File does not exist: " + opts.projFile)
let projRefs, mainProj = retryGetCrackedProjects opts
let fableLibraryPath, pkgRefs =
copyFableLibraryAndPackageSources rootDir mainProj.PackageReferences
copyFableLibraryAndPackageSources opts.rootDir mainProj.PackageReferences
let projOpts =
let sourceFiles =
let pkgSources = pkgRefs |> List.collect getSourcesFromFsproj
Expand Down Expand Up @@ -433,9 +443,9 @@ let getFullProjectOpts (define: string[]) (noReferences: bool) (rootDir: string)
mainProj.DllReferences
|> List.mapToArray (fun r -> "-r:" + r)
let otherOpts = mainProj.OtherCompilerOptions |> Array.ofList
[ getBasicCompilerArgs define
[ getBasicCompilerArgs opts.define
otherOpts
dllRefs ]
|> Array.concat
makeProjectOptions projFile sourceFiles otherOptions
makeProjectOptions opts.projFile sourceFiles otherOptions
projOpts, fableLibraryPath
6 changes: 5 additions & 1 deletion src/Fable.Cli/Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ type IMessageHandler =
abstract Respond: write: (TextWriter->unit) -> unit

type AgentMsg =
| Parsed of projectFile: string
* Fable.Transforms.State.Project
* FSharp.Compiler.SourceCodeServices.InteractiveChecker
| Received of handler: IMessageHandler
| Respond of response: obj * handler: IMessageHandler

Expand Down Expand Up @@ -125,7 +128,8 @@ module Log =
let writerLock = obj()

let always (msg: string) =
if GlobalParams.Singleton.Verbosity <> Fable.Verbosity.Silent then
if GlobalParams.Singleton.Verbosity <> Fable.Verbosity.Silent
&& not(String.IsNullOrEmpty(msg)) then
lock writerLock (fun () ->
Console.Out.WriteLine(msg)
Console.Out.Flush())
Expand Down
2 changes: 1 addition & 1 deletion src/Fable.Transforms/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ let isCompatibleWithJsComparison = function
// * `LanguagePrimitive.PhysicalHash` creates an identity hash no matter whether GetHashCode is implemented or not.
let identityHash r (arg: Expr) =
match arg.Type with
| Boolean | Char | String | Number _ | Enum _ | Option | Tuple | List
| Boolean | Char | String | Number _ | Enum _ | Option _ | Tuple _ | List _
| Builtin(BclInt64 | BclUInt64 | BclDecimal | BclBigInt)
| Builtin(BclGuid | BclTimeSpan | BclDateTime | BclDateTimeOffset)
| Builtin(FSharpSet _ | FSharpMap _ | FSharpChoice _ | FSharpResult _) ->
Expand Down
2 changes: 1 addition & 1 deletion src/Fable.Transforms/State.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ open System.Collections.Concurrent
type Project(projectOptions: FSharpProjectOptions,
implFiles: IDictionary<string, FSharpImplementationFileContents>,
errors: FSharpErrorInfo array) =
let projectFile = Path.normalizePath projectOptions.ProjectFileName
let projectFile = projectOptions.ProjectFileName
let inlineExprs = ConcurrentDictionary<string, InlineExpr>()
let rootModules =
implFiles |> Seq.map (fun kv ->
Expand Down
2 changes: 1 addition & 1 deletion src/fable-compiler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function processArgs(args?: {[x: string]: any}) {
delete args.path;
}
for (const k of Object.keys(args)) {
if (args[k] !== false) {
if (args[k] != null && args[k] !== false) {
cliArgs.push("--" + k.replace(/[A-Z]/g, (x) => "-" + x.toLowerCase()));
if (args[k] !== true) {
cliArgs.push(args[k]);
Expand Down
1 change: 1 addition & 0 deletions src/fable-loader/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
1 change: 1 addition & 0 deletions src/fable-loader/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Override .gitignore to include the `dist` folder
151 changes: 0 additions & 151 deletions src/fable-loader/index.js

This file was deleted.

Loading

0 comments on commit e84aa4d

Please sign in to comment.