Skip to content

Commit 31e1d46

Browse files
Add flag to disable reflection
1 parent fd047ae commit 31e1d46

File tree

5 files changed

+40
-25
lines changed

5 files changed

+40
-25
lines changed

src/Fable.AST/Plugins.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type CompilerOptions =
2626
Verbosity: Verbosity
2727
FileExtension: string
2828
TriggeredByDependency: bool
29+
NoReflection: bool
2930
}
3031

3132
type PluginHelper =

src/Fable.Cli/Entry.fs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ open Fable
66

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

7476
// Hidden args
7577
["--precompiledLib"], []
78+
["--noReflection"], []
7679
["--typescript"], []
7780
["--trimRootModule"], []
7881
["--fableLib"], []
@@ -231,6 +234,7 @@ type Runner =
231234
define = define,
232235
debugMode = (configuration = "Debug"),
233236
optimizeFSharpAst = args.FlagEnabled "--optimize",
237+
noReflection = args.FlagEnabled "--noReflection",
234238
verbosity = verbosity)
235239

236240
let cliArgs =

src/Fable.Cli/Main.fs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -484,12 +484,15 @@ and FableCompiler(projCracked: ProjectCracked, fableProj: Project, checker: Inte
484484
agent.PostAndAsyncReply(GetFableProject)
485485

486486
member _.StartCompilation(sourceFiles, filesToCompile, pathResolver, isSilent, isTriggeredByDependency) = async {
487-
if not isSilent then
488-
Log.always "Started Fable compilation..."
489-
let! results, ms = Performance.measureAsync <| fun () ->
490-
agent.PostAndAsyncReply(fun channel -> StartCompilation(sourceFiles, filesToCompile, pathResolver, isSilent, isTriggeredByDependency, channel))
491-
Log.always $"Fable compilation finished in %i{ms}ms{Log.newLine}"
492-
return results
487+
if Array.isEmpty filesToCompile then
488+
return [||], []
489+
else
490+
if not isSilent then
491+
Log.always "Started Fable compilation..."
492+
let! results, ms = Performance.measureAsync <| fun () ->
493+
agent.PostAndAsyncReply(fun channel -> StartCompilation(sourceFiles, filesToCompile, pathResolver, isSilent, isTriggeredByDependency, channel))
494+
Log.always $"Fable compilation finished in %i{ms}ms{Log.newLine}"
495+
return results
493496
}
494497

495498
static member CheckIfCompilationIsFinished(state: FableCompilerState) =

src/Fable.Transforms/Fable2Babel.fs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,9 @@ module Util =
951951
| Fable.BaseValue(None,_) -> Super(None)
952952
| Fable.BaseValue(Some boundIdent,_) -> identAsExpr boundIdent
953953
| Fable.ThisValue _ -> Expression.thisExpression()
954-
| Fable.TypeInfo t -> transformTypeInfo com ctx r (Some Map.empty) t
954+
| Fable.TypeInfo t ->
955+
if com.Options.NoReflection then addErrorAndReturnNull com r "Reflection is disabled"
956+
else transformTypeInfo com ctx r (Some Map.empty) t
955957
| Fable.Null _t ->
956958
// if com.Options.typescript
957959
// let ta = typeAnnotation com ctx t |> TypeAnnotation |> Some
@@ -1971,20 +1973,23 @@ module Util =
19711973

19721974
let declareType (com: IBabelCompiler) ctx (ent: Fable.Entity) entName (consArgs: Pattern[]) (consBody: BlockStatement) baseExpr classMembers: ModuleDeclaration list =
19731975
let typeDeclaration = declareClassType com ctx ent entName consArgs consBody baseExpr classMembers
1974-
let reflectionDeclaration =
1975-
let ta =
1976-
if com.Options.Language = TypeScript then
1977-
makeImportTypeAnnotation com ctx [] "Reflection" "TypeInfo"
1978-
|> TypeAnnotation |> Some
1979-
else None
1980-
let genArgs = Array.init (ent.GenericParameters.Length) (fun i -> "gen" + string i |> makeIdent)
1981-
let generics = genArgs |> Array.map identAsExpr
1982-
let body = transformReflectionInfo com ctx None ent generics
1983-
let args = genArgs |> Array.map (fun x -> Pattern.identifier(x.Name, ?typeAnnotation=ta))
1984-
let returnType = ta
1985-
makeFunctionExpression None (args, body, returnType, None)
1986-
|> declareModuleMember ent.IsPublic (entName + Naming.reflectionSuffix) false
1987-
[typeDeclaration; reflectionDeclaration]
1976+
if com.Options.NoReflection then
1977+
[typeDeclaration]
1978+
else
1979+
let reflectionDeclaration =
1980+
let ta =
1981+
if com.Options.Language = TypeScript then
1982+
makeImportTypeAnnotation com ctx [] "Reflection" "TypeInfo"
1983+
|> TypeAnnotation |> Some
1984+
else None
1985+
let genArgs = Array.init (ent.GenericParameters.Length) (fun i -> "gen" + string i |> makeIdent)
1986+
let generics = genArgs |> Array.map identAsExpr
1987+
let body = transformReflectionInfo com ctx None ent generics
1988+
let args = genArgs |> Array.map (fun x -> Pattern.identifier(x.Name, ?typeAnnotation=ta))
1989+
let returnType = ta
1990+
makeFunctionExpression None (args, body, returnType, None)
1991+
|> declareModuleMember ent.IsPublic (entName + Naming.reflectionSuffix) false
1992+
[typeDeclaration; reflectionDeclaration]
19881993

19891994
let transformModuleFunction (com: IBabelCompiler) ctx (info: Fable.MemberInfo) (membName: string) args body =
19901995
let args, body, returnType, typeParamDecl =

src/Fable.Transforms/Global/Compiler.fs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ type CompilerOptionsHelper =
1212
?optimizeFSharpAst,
1313
?verbosity,
1414
?fileExtension,
15-
?clampByteArrays) =
15+
?clampByteArrays,
16+
?noReflection) =
1617
{
1718
CompilerOptions.Define = defaultArg define []
1819
DebugMode = defaultArg debugMode true
@@ -23,6 +24,7 @@ type CompilerOptionsHelper =
2324
Verbosity = defaultArg verbosity Verbosity.Normal
2425
FileExtension = defaultArg fileExtension CompilerOptionsHelper.DefaultExtension
2526
ClampByteArrays = defaultArg clampByteArrays false
27+
NoReflection = defaultArg noReflection false
2628
TriggeredByDependency = false
2729
}
2830

0 commit comments

Comments
 (0)