From 4698c9531af50c702c59f1dfb19e62482c4ca654 Mon Sep 17 00:00:00 2001 From: Alfonso Garcia-Caro Date: Mon, 10 Jan 2022 11:28:18 +0900 Subject: [PATCH] Add flag to disable reflection --- src/Fable.AST/Plugins.fs | 1 + src/Fable.Cli/Entry.fs | 10 ++++--- src/Fable.Cli/Main.fs | 15 ++++++----- src/Fable.Transforms/Fable2Babel.fs | 35 ++++++++++++++----------- src/Fable.Transforms/Global/Compiler.fs | 4 ++- 5 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/Fable.AST/Plugins.fs b/src/Fable.AST/Plugins.fs index e6239bb657..5a2bf461d1 100644 --- a/src/Fable.AST/Plugins.fs +++ b/src/Fable.AST/Plugins.fs @@ -26,6 +26,7 @@ type CompilerOptions = Verbosity: Verbosity FileExtension: string TriggeredByDependency: bool + NoReflection: bool } type PluginHelper = diff --git a/src/Fable.Cli/Entry.fs b/src/Fable.Cli/Entry.fs index 41a56bc466..1b6df49474 100644 --- a/src/Fable.Cli/Entry.fs +++ b/src/Fable.Cli/Entry.fs @@ -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("-") -> @@ -73,6 +75,7 @@ let knownCliArgs() = [ // Hidden args ["--precompiledLib"], [] + ["--noReflection"], [] ["--typescript"], [] ["--trimRootModule"], [] ["--fableLib"], [] @@ -231,6 +234,7 @@ type Runner = define = define, debugMode = (configuration = "Debug"), optimizeFSharpAst = args.FlagEnabled "--optimize", + noReflection = args.FlagEnabled "--noReflection", verbosity = verbosity) let cliArgs = diff --git a/src/Fable.Cli/Main.fs b/src/Fable.Cli/Main.fs index 6f0ac83eba..acef548a99 100644 --- a/src/Fable.Cli/Main.fs +++ b/src/Fable.Cli/Main.fs @@ -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) = diff --git a/src/Fable.Transforms/Fable2Babel.fs b/src/Fable.Transforms/Fable2Babel.fs index ff46930efd..5beecdcf25 100644 --- a/src/Fable.Transforms/Fable2Babel.fs +++ b/src/Fable.Transforms/Fable2Babel.fs @@ -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 @@ -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 = diff --git a/src/Fable.Transforms/Global/Compiler.fs b/src/Fable.Transforms/Global/Compiler.fs index df3363f4e4..43664a17d7 100644 --- a/src/Fable.Transforms/Global/Compiler.fs +++ b/src/Fable.Transforms/Global/Compiler.fs @@ -12,7 +12,8 @@ type CompilerOptionsHelper = ?optimizeFSharpAst, ?verbosity, ?fileExtension, - ?clampByteArrays) = + ?clampByteArrays, + ?noReflection) = { CompilerOptions.Define = defaultArg define [] DebugMode = defaultArg debugMode true @@ -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 }