Skip to content

Commit

Permalink
Add flag to disable reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsogarciacaro committed Jan 11, 2022
1 parent fd047ae commit 31e1d46
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/Fable.AST/Plugins.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type CompilerOptions =
Verbosity: Verbosity
FileExtension: string
TriggeredByDependency: bool
NoReflection: bool
}

type PluginHelper =
Expand Down
10 changes: 7 additions & 3 deletions src/Fable.Cli/Entry.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ open Fable

type CliArgs(args: string list) =
let argsMap =
let even = List.length args % 2 = 0
// If arguments are odd, assume last has true value in case it's a flag
let args = if even then args else args @ ["true"]
let args =
// Assume last arg has true value in case it's a flag
match List.tryLast args with
| Some key when key.StartsWith("-") -> args @ ["true"]
| _ -> args
(Map.empty, List.windowed 2 args) ||> List.fold (fun map pair ->
match pair with
| [key; value] when key.StartsWith("-") ->
Expand Down Expand Up @@ -73,6 +75,7 @@ let knownCliArgs() = [

// Hidden args
["--precompiledLib"], []
["--noReflection"], []
["--typescript"], []
["--trimRootModule"], []
["--fableLib"], []
Expand Down Expand Up @@ -231,6 +234,7 @@ type Runner =
define = define,
debugMode = (configuration = "Debug"),
optimizeFSharpAst = args.FlagEnabled "--optimize",
noReflection = args.FlagEnabled "--noReflection",
verbosity = verbosity)

let cliArgs =
Expand Down
15 changes: 9 additions & 6 deletions src/Fable.Cli/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -484,12 +484,15 @@ and FableCompiler(projCracked: ProjectCracked, fableProj: Project, checker: Inte
agent.PostAndAsyncReply(GetFableProject)

member _.StartCompilation(sourceFiles, filesToCompile, pathResolver, isSilent, isTriggeredByDependency) = async {
if not isSilent then
Log.always "Started Fable compilation..."
let! results, ms = Performance.measureAsync <| fun () ->
agent.PostAndAsyncReply(fun channel -> StartCompilation(sourceFiles, filesToCompile, pathResolver, isSilent, isTriggeredByDependency, channel))
Log.always $"Fable compilation finished in %i{ms}ms{Log.newLine}"
return results
if Array.isEmpty filesToCompile then
return [||], []
else
if not isSilent then
Log.always "Started Fable compilation..."
let! results, ms = Performance.measureAsync <| fun () ->
agent.PostAndAsyncReply(fun channel -> StartCompilation(sourceFiles, filesToCompile, pathResolver, isSilent, isTriggeredByDependency, channel))
Log.always $"Fable compilation finished in %i{ms}ms{Log.newLine}"
return results
}

static member CheckIfCompilationIsFinished(state: FableCompilerState) =
Expand Down
35 changes: 20 additions & 15 deletions src/Fable.Transforms/Fable2Babel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,9 @@ module Util =
| Fable.BaseValue(None,_) -> Super(None)
| Fable.BaseValue(Some boundIdent,_) -> identAsExpr boundIdent
| Fable.ThisValue _ -> Expression.thisExpression()
| Fable.TypeInfo t -> transformTypeInfo com ctx r (Some Map.empty) t
| Fable.TypeInfo t ->
if com.Options.NoReflection then addErrorAndReturnNull com r "Reflection is disabled"
else transformTypeInfo com ctx r (Some Map.empty) t
| Fable.Null _t ->
// if com.Options.typescript
// let ta = typeAnnotation com ctx t |> TypeAnnotation |> Some
Expand Down Expand Up @@ -1971,20 +1973,23 @@ module Util =

let declareType (com: IBabelCompiler) ctx (ent: Fable.Entity) entName (consArgs: Pattern[]) (consBody: BlockStatement) baseExpr classMembers: ModuleDeclaration list =
let typeDeclaration = declareClassType com ctx ent entName consArgs consBody baseExpr classMembers
let reflectionDeclaration =
let ta =
if com.Options.Language = TypeScript then
makeImportTypeAnnotation com ctx [] "Reflection" "TypeInfo"
|> TypeAnnotation |> Some
else None
let genArgs = Array.init (ent.GenericParameters.Length) (fun i -> "gen" + string i |> makeIdent)
let generics = genArgs |> Array.map identAsExpr
let body = transformReflectionInfo com ctx None ent generics
let args = genArgs |> Array.map (fun x -> Pattern.identifier(x.Name, ?typeAnnotation=ta))
let returnType = ta
makeFunctionExpression None (args, body, returnType, None)
|> declareModuleMember ent.IsPublic (entName + Naming.reflectionSuffix) false
[typeDeclaration; reflectionDeclaration]
if com.Options.NoReflection then
[typeDeclaration]
else
let reflectionDeclaration =
let ta =
if com.Options.Language = TypeScript then
makeImportTypeAnnotation com ctx [] "Reflection" "TypeInfo"
|> TypeAnnotation |> Some
else None
let genArgs = Array.init (ent.GenericParameters.Length) (fun i -> "gen" + string i |> makeIdent)
let generics = genArgs |> Array.map identAsExpr
let body = transformReflectionInfo com ctx None ent generics
let args = genArgs |> Array.map (fun x -> Pattern.identifier(x.Name, ?typeAnnotation=ta))
let returnType = ta
makeFunctionExpression None (args, body, returnType, None)
|> declareModuleMember ent.IsPublic (entName + Naming.reflectionSuffix) false
[typeDeclaration; reflectionDeclaration]

let transformModuleFunction (com: IBabelCompiler) ctx (info: Fable.MemberInfo) (membName: string) args body =
let args, body, returnType, typeParamDecl =
Expand Down
4 changes: 3 additions & 1 deletion src/Fable.Transforms/Global/Compiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ type CompilerOptionsHelper =
?optimizeFSharpAst,
?verbosity,
?fileExtension,
?clampByteArrays) =
?clampByteArrays,
?noReflection) =
{
CompilerOptions.Define = defaultArg define []
DebugMode = defaultArg debugMode true
Expand All @@ -23,6 +24,7 @@ type CompilerOptionsHelper =
Verbosity = defaultArg verbosity Verbosity.Normal
FileExtension = defaultArg fileExtension CompilerOptionsHelper.DefaultExtension
ClampByteArrays = defaultArg clampByteArrays false
NoReflection = defaultArg noReflection false
TriggeredByDependency = false
}

Expand Down

0 comments on commit 31e1d46

Please sign in to comment.