@@ -897,6 +897,132 @@ import { fib } from "./fib.wasm";
897
897
console .log (fib (20 ));
898
898
```
899
899
900
+ ## Compiler API
901
+
902
+ Deno supports runtime access to the built in TypeScript compiler. There are
903
+ three methods in the ` Deno ` namespace that provide this access.
904
+
905
+ ### ` Deno.compile() `
906
+
907
+ This works similar to ` deno fetch ` in that it can fetch code, compile it, but
908
+ not run it. It takes up to three arguments, the ` rootName ` , optionally
909
+ ` sources ` , and optionally ` options ` . The ` rootName ` is the root module which
910
+ will be used to generate the resulting program. This is like module name you
911
+ would pass on the command line in ` deno --reload run example.ts ` . The ` sources `
912
+ is a hash where the key is the fully qualified module name, and the value is the
913
+ text source of the module. If ` sources ` is passed, Deno will resolve all the
914
+ modules from within that hash and not attempt to resolve them outside of Deno.
915
+ If ` sources ` are not provided, Deno will resolve modules as if the root module
916
+ had been passed on the command line. Deno will also cache any of these
917
+ resources. The ` options ` argument is a set of options of type
918
+ ` Deno.CompilerOptions ` , which is a subset of the TypeScript compiler options
919
+ which can be supported by Deno.
920
+
921
+ The method resolves with a tuple where the first argument is any diagnostics
922
+ (syntax or type errors) related to the code, and a map of the code, where the
923
+ key would be the output filename and the value would be the content.
924
+
925
+ An example of providing sources:
926
+
927
+ ``` ts
928
+ const [diagnostics, emitMap] = await Deno .compile (" /foo.ts" , {
929
+ " /foo.ts" : ` import * as bar from "./bar.ts";\n console.log(bar);\n ` ,
930
+ " /bar.ts" : ` export const bar = "bar";\n `
931
+ });
932
+
933
+ assert (diagnostics == null ); // ensuring no diagnostics are returned
934
+ console .log (emitMap );
935
+ ```
936
+
937
+ We would expect map to contain 4 "files", named ` /foo.js.map ` , ` /foo.js ` ,
938
+ ` /bar.js.map ` , and ` /bar.js ` .
939
+
940
+ When not supplying resources, you can use local or remote modules, just like you
941
+ could do on the command line. So you could do something like this:
942
+
943
+ ``` ts
944
+ const [diagnostics, emitMap] = await Deno .compile (
945
+ " https://deno.land/std/examples/welcome.ts"
946
+ );
947
+ ```
948
+
949
+ We should get back in the ` emitMap ` a simple ` console.log() ` statement.
950
+
951
+ ### ` Deno.bundle() `
952
+
953
+ This works a lot like ` deno bundle ` does on the command line. It is also like
954
+ ` Deno.compile() ` , except instead of returning a map of files, it returns a
955
+ single string, which is a self-contained JavaScript ES module which will include
956
+ all of the code that was provided or resolved as well as exports of all the
957
+ exports of the root module that was provided. It takes up to three arguments,
958
+ the ` rootName ` , optionally ` sources ` , and optionally ` options ` . The ` rootName `
959
+ is the root module which will be used to generate the resulting program. This is
960
+ like module name you would pass on the command line in ` deno bundle example.ts ` .
961
+ The ` sources ` is a hash where the key is the fully qualified module name, and
962
+ the value is the text source of the module. If ` sources ` is passed, Deno will
963
+ resolve all the modules from within that hash and not attempt to resolve them
964
+ outside of Deno. If ` sources ` are not provided, Deno will resolve modules as if
965
+ the root module had been passed on the command line. Deno will also cache any of
966
+ these resources. The ` options ` argument is a set of options of type
967
+ ` Deno.CompilerOptions ` , which is a subset of the TypeScript compiler options
968
+ which can be supported by Deno.
969
+
970
+ An example of providing sources:
971
+
972
+ ``` ts
973
+ const [diagnostics, emit] = await Deno .compile (" /foo.ts" , {
974
+ " /foo.ts" : ` import * as bar from "./bar.ts";\n console.log(bar);\n ` ,
975
+ " /bar.ts" : ` export const bar = "bar";\n `
976
+ });
977
+
978
+ assert (diagnostics == null ); // ensuring no diagnostics are returned
979
+ console .log (emit );
980
+ ```
981
+
982
+ We would expect ` emit ` to be the text for an ES module, which would contain the
983
+ output sources for both modules.
984
+
985
+ When not supplying resources, you can use local or remote modules, just like you
986
+ could do on the command line. So you could do something like this:
987
+
988
+ ``` ts
989
+ const [diagnostics, emit] = await Deno .compile (
990
+ " https://deno.land/std/http/server.ts"
991
+ );
992
+ ```
993
+
994
+ We should get back in ` emit ` a self contained JavaScript ES module with all of
995
+ its dependencies resolved and exporting the same exports as the source module.
996
+
997
+ ### ` Deno.transpileOnly() `
998
+
999
+ This is based off of the TypeScript function ` transpileModule() ` . All this does
1000
+ is "erase" any types from the modules and emit JavaScript. There is no type
1001
+ checking and no resolution of dependencies. It accepts up to two arguments, the
1002
+ first is a hash where the key is the module name and the value is the contents.
1003
+ The only purpose of the module name is when putting information into a source
1004
+ map, of what the source file name was. The second is optionally ` options ` which
1005
+ is of type ` Deno.CompilerOptions ` . This is a subset of options which can be
1006
+ supported by Deno. It resolves with a map where the key is the source module
1007
+ name supplied, and the value is an object with a property of ` source ` which is
1008
+ the output contents of the module, and optionally ` map ` which would be the
1009
+ source map. By default, source maps are output, but can be turned off via the
1010
+ ` options ` argument.
1011
+
1012
+ An example:
1013
+
1014
+ ``` ts
1015
+ const result = await Deno .transpileOnly ({
1016
+ " /foo.ts" : ` enum Foo { Foo, Bar, Baz };\n `
1017
+ });
1018
+
1019
+ console .log (result [" /foo.ts" ].source );
1020
+ console .log (result [" /foo.ts" ].map );
1021
+ ```
1022
+
1023
+ We would expect the ` enum ` would be rewritten to an IIFE which constructs the
1024
+ enumerable, and the map to be defined.
1025
+
900
1026
## Program lifecycle
901
1027
902
1028
Deno supports browser compatible lifecycle events: ` load ` and ` unload ` . You can
0 commit comments