Skip to content

Commit

Permalink
feat: additive graph (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Feb 4, 2023
1 parent ffc6fca commit 1b8d01c
Show file tree
Hide file tree
Showing 6 changed files with 621 additions and 404 deletions.
34 changes: 26 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ leverage the logic outside of the Deno CLI from JavaScript/TypeScript.

## Rust usage

### `create_graph()`
### `ModuleGraph::new(...)`

`create_graph()` is the main way of interfacing with the crate. It requires the
root module specifiers/URLs for the graph and an implementation of the
`ModuleGraph::new(GraphKind::All)` creates a new module graph. From there, you
can use the `.build(...).await` method to add roots. The `build` method requires
the root module specifiers/URLs for the graph and an implementation of the
`source::Loader` trait. It also optionally takes implementation of the
`source::Resolver` trait. It will load and parse the root module and recursively
all of its dependencies, returning asynchronously a resulting `ModuleGraph`.
Expand Down Expand Up @@ -68,21 +69,38 @@ A minimal example would look like this:
use deno_graph::create_graph;
use deno_graph::ModuleSpecifier;
use deno_graph::source::MemoryLoader;
use deno_graph::source::Source;
use futures::executor::block_on;

fn main() {
let mut loader = MemoryLoader::new(
vec![
("file:///test.ts", Ok(("file:///test.ts", None, "import * as a from \"./a.ts\";"))),
("file:///a.ts", Ok(("file:///a.ts", None, "export const a = \"a\";"))),
]
(
"file:///test.ts",
Source::Module {
specifier: "file:///test.ts",
maybe_headers: None,
content: "import * as a from \"./a.ts\";"
}
),
(
"file:///a.ts",
Source::Module {
specifier: "file:///a.ts",
maybe_headers: None,
content: "export const a = \"a\";",
}
),
],
Vec::new(),
);
let roots = vec![ModuleSpecifier::parse("file:///test.ts").unwrap()];
let future = async move {
let graph = create_graph(roots, &mut loader, Default::default()).await;
let mut graph = ModuleGraph::default();
graph.build(roots, &mut loader, Default::default()).await;
println!("{}", graph);
};
block_on()
block_on(future)
}
```

Expand Down
10 changes: 5 additions & 5 deletions lib/deno_graph_wasm.generated.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @generated file from wasmbuild -- do not edit
// deno-lint-ignore-file
// deno-fmt-ignore-file
// source-hash: 2397aff89324df2c6a510476c774e6e5627bc179
// source-hash: 524dc85b1c90c720808be7ab778c604fcc5bf618
let wasm;

const heap = new Array(32).fill(undefined);
Expand Down Expand Up @@ -241,7 +241,7 @@ function __wbg_adapter_48(arg0, arg1, arg2) {
* @param {Function | undefined} maybe_cache_info
* @param {Function | undefined} maybe_resolve
* @param {Function | undefined} maybe_resolve_types
* @param {string | undefined} maybe_build_kind
* @param {string | undefined} maybe_graph_kind
* @param {any} maybe_imports
* @returns {Promise<any>}
*/
Expand All @@ -253,7 +253,7 @@ export function createGraph(
maybe_cache_info,
maybe_resolve,
maybe_resolve_types,
maybe_build_kind,
maybe_graph_kind,
maybe_imports,
) {
var ptr0 = isLikeNone(maybe_default_jsx_import_source)
Expand All @@ -272,10 +272,10 @@ export function createGraph(
wasm.__wbindgen_realloc,
);
var len1 = WASM_VECTOR_LEN;
var ptr2 = isLikeNone(maybe_build_kind)
var ptr2 = isLikeNone(maybe_graph_kind)
? 0
: passStringToWasm0(
maybe_build_kind,
maybe_graph_kind,
wasm.__wbindgen_malloc,
wasm.__wbindgen_realloc,
);
Expand Down
Binary file modified lib/deno_graph_wasm_bg.wasm
Binary file not shown.
41 changes: 22 additions & 19 deletions lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use deno_graph::source::LoadFuture;
use deno_graph::source::Loader;
use deno_graph::source::Resolver;
use deno_graph::source::DEFAULT_JSX_IMPORT_SOURCE_MODULE;
use deno_graph::BuildKind;
use deno_graph::BuildOptions;
use deno_graph::GraphKind;
use deno_graph::ModuleGraph;
use deno_graph::ModuleKind;
use deno_graph::ModuleSpecifier;
use deno_graph::Range;
Expand Down Expand Up @@ -185,7 +187,7 @@ pub async fn js_create_graph(
maybe_cache_info: Option<js_sys::Function>,
maybe_resolve: Option<js_sys::Function>,
maybe_resolve_types: Option<js_sys::Function>,
maybe_build_kind: Option<String>,
maybe_graph_kind: Option<String>,
maybe_imports: JsValue,
) -> Result<JsValue, JsValue> {
let roots_vec: Vec<String> = serde_wasm_bindgen::from_value(roots)
Expand Down Expand Up @@ -229,24 +231,25 @@ pub async fn js_create_graph(
Vec::new()
};

let build_kind = match maybe_build_kind.as_deref() {
Some("typesOnly") => BuildKind::TypesOnly,
Some("codeOnly") => BuildKind::CodeOnly,
_ => BuildKind::All,
let graph_kind = match maybe_graph_kind.as_deref() {
Some("typesOnly") => GraphKind::TypesOnly,
Some("codeOnly") => GraphKind::CodeOnly,
_ => GraphKind::All,
};
let graph = deno_graph::create_graph(
roots,
&mut loader,
deno_graph::GraphOptions {
is_dynamic: false,
resolver: maybe_resolver.as_ref().map(|r| r as &dyn Resolver),
module_analyzer: None,
build_kind,
imports,
reporter: None,
},
)
.await;
let mut graph = ModuleGraph::new(graph_kind);
graph
.build(
roots,
&mut loader,
BuildOptions {
is_dynamic: false,
resolver: maybe_resolver.as_ref().map(|r| r as &dyn Resolver),
module_analyzer: None,
imports,
reporter: None,
},
)
.await;
let serializer =
serde_wasm_bindgen::Serializer::new().serialize_maps_as_objects(true);
Ok(graph.serialize(&serializer).unwrap())
Expand Down
Loading

0 comments on commit 1b8d01c

Please sign in to comment.