Skip to content

Commit

Permalink
feat: add source maps to graph (#149)
Browse files Browse the repository at this point in the history
Closes #146
  • Loading branch information
kitsonk authored Apr 28, 2022
1 parent 0609b36 commit 2b8b979
Show file tree
Hide file tree
Showing 11 changed files with 317 additions and 26 deletions.
53 changes: 53 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ ring = "0.16.20"
serde = { version = "1.0.130", features = ["derive", "rc"] }
serde_json = { version = "1.0.67", features = ["preserve_order"] }
serde-wasm-bindgen = { version = "0.4.2", optional = true }
sourcemap = "=6.0.1"
termcolor = "1.1.2"
deno_ast = { version = "0.14.0", features = ["dep_graph", "module_specifier"] }
url = { version = "2.2.2", features = ["serde"] }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ Also, the build script (`_build.ts`) requires the Deno CLI to be installed and
available in the path. If it is, the script should _just work_:

```
> ./_build.ts
> deno task build
```

But can be manually invoked like:
Expand Down
44 changes: 19 additions & 25 deletions lib/deno_graph.generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ function makeMutClosure(arg0, arg1, dtor, f) {
}
function __wbg_adapter_20(arg0, arg1, arg2) {
wasm
._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__haaf10ac45db29e97(
._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h963f79d8759e578e(
arg0,
arg1,
addHeapObject(arg2),
Expand Down Expand Up @@ -311,29 +311,23 @@ export function createGraph(
maybe_build_kind,
maybe_imports,
) {
var ptr0 = isLikeNone(maybe_jsx_import_source_module)
? 0
: passStringToWasm0(
maybe_jsx_import_source_module,
wasm.__wbindgen_malloc,
wasm.__wbindgen_realloc,
);
var ptr0 = isLikeNone(maybe_jsx_import_source_module) ? 0 : passStringToWasm0(
maybe_jsx_import_source_module,
wasm.__wbindgen_malloc,
wasm.__wbindgen_realloc,
);
var len0 = WASM_VECTOR_LEN;
var ptr1 = isLikeNone(maybe_lockfile_name)
? 0
: passStringToWasm0(
maybe_lockfile_name,
wasm.__wbindgen_malloc,
wasm.__wbindgen_realloc,
);
var ptr1 = isLikeNone(maybe_lockfile_name) ? 0 : passStringToWasm0(
maybe_lockfile_name,
wasm.__wbindgen_malloc,
wasm.__wbindgen_realloc,
);
var len1 = WASM_VECTOR_LEN;
var ptr2 = isLikeNone(maybe_build_kind)
? 0
: passStringToWasm0(
maybe_build_kind,
wasm.__wbindgen_malloc,
wasm.__wbindgen_realloc,
);
var ptr2 = isLikeNone(maybe_build_kind) ? 0 : passStringToWasm0(
maybe_build_kind,
wasm.__wbindgen_malloc,
wasm.__wbindgen_realloc,
);
var len2 = WASM_VECTOR_LEN;
var ret = wasm.createGraph(
addHeapObject(roots),
Expand Down Expand Up @@ -368,7 +362,7 @@ function handleError(f, args) {
}
}
function __wbg_adapter_69(arg0, arg1, arg2, arg3) {
wasm.wasm_bindgen__convert__closures__invoke2_mut__had5d8f15ca1c7c5b(
wasm.wasm_bindgen__convert__closures__invoke2_mut__h6b7a7347e885071e(
arg0,
arg1,
addHeapObject(arg2),
Expand Down Expand Up @@ -827,8 +821,8 @@ const imports = {
__wbindgen_throw: function (arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
},
__wbindgen_closure_wrapper1683: function (arg0, arg1, arg2) {
var ret = makeMutClosure(arg0, arg1, 284, __wbg_adapter_20);
__wbindgen_closure_wrapper1773: function (arg0, arg1, arg2) {
var ret = makeMutClosure(arg0, arg1, 285, __wbg_adapter_20);
return addHeapObject(ret);
},
},
Expand Down
Binary file modified lib/deno_graph_bg.wasm
Binary file not shown.
10 changes: 10 additions & 0 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

import type { MediaType } from "./media_type.ts";
import {
type RawSourceMap,
type SourceMapUrl,
} from "https://esm.sh/[email protected]/source-map.d.ts";

/** Additional meta data that is used to enrich the output of the module
* graph. */
Expand Down Expand Up @@ -151,6 +155,12 @@ export interface ModuleJson extends CacheInfo {
/** If available, the calculated checksum of the module which can be used for
* validating the integrity of the module. */
checksum?: string;
/** If available, the upstream source map from the module. If present,
* `sourceMapUrl` will be undefined. */
sourceMap?: RawSourceMap;
/** If available, the upstream source map URL from the module. If present,
* `sourceMap` will be undefined. */
sourceMapUrl?: SourceMapUrl;
}

/** The plain-object representation of a module graph that is suitable for
Expand Down
8 changes: 8 additions & 0 deletions schemas/module-graph.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@
"type": "string",
"description": "The path to an optionally emitted source map between the original and emitted version of the file."
},
"sourceMap": {
"description": "The source map that was parsed from the source of the module, which indicates an \"upstream\" source map that applies to the module.",
"$ref": "https://json.schemastore.org/sourcemap-v3.json"
},
"sourceMapUrl": {
"type": "string",
"description": "The source map URL parsed from the source of the module, which indicates there is an \"upstream\" source map that applies to the module."
},
"error": {
"type": "string",
"description": "If when resolving the module, Deno encountered an error and the module is unavailable, the text of that error will be indicated here."
Expand Down
21 changes: 21 additions & 0 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use crate::module_specifier::resolve_import;
use crate::module_specifier::ModuleSpecifier;
use crate::module_specifier::SpecifierError;
use crate::source::*;
use crate::source_map::parse_sourcemap;
use crate::source_map::ParsedSourceMap;

use anyhow::Result;
use deno_ast::MediaType;
Expand All @@ -26,6 +28,7 @@ use serde::ser::SerializeStruct;
use serde::Deserialize;
use serde::Serialize;
use serde::Serializer;
use serde_json::Value;
use std::cell::RefCell;
use std::cmp::Ordering;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -598,6 +601,10 @@ pub struct Module {
serialize_with = "serialize_maybe_source"
)]
pub maybe_source: Option<Arc<String>>,
#[serde(rename = "sourceMap", skip_serializing_if = "Option::is_none")]
pub maybe_source_map: Option<Value>,
#[serde(rename = "sourceMapUrl", skip_serializing_if = "Option::is_none")]
pub maybe_source_map_url: Option<ModuleSpecifier>,
#[serde(
rename = "typesDependency",
skip_serializing_if = "Option::is_none",
Expand All @@ -616,13 +623,21 @@ impl Module {
parsed_source: ParsedSource,
) -> Self {
let source = parsed_source.source().text();
let (maybe_source_map, maybe_source_map_url) =
match parse_sourcemap(&specifier, &parsed_source) {
Some(ParsedSourceMap::Url(url)) => (None, Some(url)),
Some(ParsedSourceMap::Value(value)) => (Some(value), None),
_ => (None, None),
};
Self {
dependencies: Default::default(),
kind,
maybe_cache_info: None,
maybe_checksum: None,
maybe_parsed_source: Some(parsed_source),
maybe_source: Some(source),
maybe_source_map,
maybe_source_map_url,
maybe_types_dependency: None,
media_type: MediaType::Unknown,
specifier,
Expand All @@ -640,6 +655,8 @@ impl Module {
maybe_checksum: None,
maybe_parsed_source: None,
maybe_source: None,
maybe_source_map: None,
maybe_source_map_url: None,
maybe_types_dependency: None,
media_type: MediaType::Unknown,
specifier,
Expand Down Expand Up @@ -679,6 +696,8 @@ impl Module {
maybe_checksum: None,
maybe_parsed_source: None,
maybe_source: None,
maybe_source_map: None,
maybe_source_map_url: None,
maybe_types_dependency: None,
media_type: MediaType::Unknown,
specifier,
Expand Down Expand Up @@ -1176,6 +1195,8 @@ pub(crate) fn parse_module(
maybe_checksum: None,
maybe_parsed_source: None,
maybe_source: Some(content),
maybe_source_map: None,
maybe_source_map_url: None,
maybe_types_dependency: None,
media_type: MediaType::Json,
specifier: specifier.clone(),
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod graph;
mod info;
mod module_specifier;
pub mod source;
mod source_map;
mod text_encoding;

use graph::BuildKind;
Expand Down
Loading

0 comments on commit 2b8b979

Please sign in to comment.