Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: deno specifier and workspace support #289

Merged
merged 46 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
94db638
Saving state for revert
dsherret Aug 9, 2023
c05ae82
Slight progress... mostly thinking.
dsherret Aug 9, 2023
7ed2454
More work.
dsherret Aug 14, 2023
d2c9576
refactor: remove tuple for pending information
dsherret Aug 14, 2023
e515ae8
Merge branch 'refactor_remove_tuple_pending_info' into deno_specifiers
dsherret Aug 14, 2023
71c2980
Merge branch 'main' into deno_specifiers
dsherret Aug 14, 2023
6ffe9e7
Update.
dsherret Aug 15, 2023
0e79c6b
More progress, but I need to refactor to support lazily retrieving th…
dsherret Aug 17, 2023
5c10e91
Further progress.
dsherret Aug 17, 2023
e25bfb0
Add content loading
dsherret Aug 17, 2023
352a81c
More messy work
dsherret Aug 21, 2023
4d6e4b8
Merge branch 'main' into deno_specifiers
dsherret Aug 21, 2023
29d835a
Update
dsherret Aug 21, 2023
7526675
Start of working
dsherret Aug 21, 2023
6586116
Updates
dsherret Aug 21, 2023
a714c10
Use module info when loading
dsherret Aug 22, 2023
24d2f15
Consolidate
dsherret Aug 22, 2023
23c9bc2
Load in parallel
dsherret Aug 22, 2023
370729f
Save
dsherret Aug 22, 2023
b1fbf75
Maybe fix
dsherret Aug 23, 2023
12894b9
Fix
dsherret Aug 23, 2023
8fdabe4
Maybe implement workspace members
dsherret Aug 23, 2023
592adf4
Export
dsherret Aug 23, 2023
7ee0de7
Logging
dsherret Aug 23, 2023
fa87c72
Update loader interface
dsherret Aug 23, 2023
95aa303
Fixes
dsherret Aug 24, 2023
609d935
Merge main
dsherret Aug 28, 2023
72f773a
Fix serialization
dsherret Aug 28, 2023
2f7736d
Add build diagnostics and start adding tests
dsherret Aug 28, 2023
3b164ed
Generalize test builder
dsherret Aug 28, 2023
c78c4dc
Starting on spec tests for deno specifiers
dsherret Aug 28, 2023
5f1413b
Move no graph info test to a spec
dsherret Aug 28, 2023
1745604
Stabilize json output
dsherret Aug 28, 2023
8f9c88b
Test for with module graph info
dsherret Aug 28, 2023
c16e760
Merge branch 'main' into deno_specifiers
dsherret Aug 29, 2023
9d38ba6
More tests
dsherret Aug 29, 2023
ca9f12d
More
dsherret Aug 29, 2023
8d23275
Cleanup
dsherret Aug 29, 2023
0af758c
Add test for reloading
dsherret Aug 29, 2023
4abddfc
Ensure cache is used if it exists instead of the module graph info
dsherret Aug 29, 2023
372a194
Update JavaScript side.
dsherret Aug 30, 2023
59be8dc
Format
dsherret Aug 30, 2023
7833834
Remove println
dsherret Aug 30, 2023
d78b95c
Add test for future being shared
dsherret Aug 30, 2023
0197da8
Clean up builder a bit
dsherret Aug 30, 2023
b99fb77
More method extractions
dsherret Aug 30, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 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 @@ -23,6 +23,7 @@ type_tracing = ["deno_ast/transforms", "deno_ast/visit", "deno_ast/utils"]

[dependencies]
anyhow = "1.0.43"
async-trait = "0.1.68"
data-url = "0.3.0"
deno_ast = { version = "0.28.0", features = ["dep_graph", "module_specifier"] }
deno_semver = "0.4.0"
Expand Down
4 changes: 4 additions & 0 deletions js/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export type {
TypesDependency,
} from "./types.d.ts";

// note: keep this in line with deno_cache
export type CacheSetting = "only" | "use" | "reload";

export interface CreateGraphOptions {
/**
* A callback that is called with the URL string of the resource to be loaded
Expand All @@ -55,6 +58,7 @@ export interface CreateGraphOptions {
load?(
specifier: string,
isDynamic: boolean,
cacheSetting: CacheSetting,
): Promise<LoadResponse | undefined>;
/** The type of graph to build. `"all"` includes all dependencies of the
* roots. `"typesOnly"` skips any code only dependencies that do not impact
Expand Down
13 changes: 11 additions & 2 deletions lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use deno_graph::source::load_data_url;
use deno_graph::source::CacheInfo;
use deno_graph::source::LoadFuture;
use deno_graph::source::Loader;
use deno_graph::source::LoaderCacheSetting;
use deno_graph::source::Resolver;
use deno_graph::source::DEFAULT_JSX_IMPORT_SOURCE_MODULE;
use deno_graph::BuildOptions;
Expand Down Expand Up @@ -59,10 +60,11 @@ impl Loader for JsLoader {
}
}

fn load(
fn load_with_cache_setting(
&mut self,
specifier: &ModuleSpecifier,
is_dynamic: bool,
cache_setting: LoaderCacheSetting,
) -> LoadFuture {
if specifier.scheme() == "data" {
Box::pin(future::ready(load_data_url(specifier)))
Expand All @@ -71,7 +73,13 @@ impl Loader for JsLoader {
let context = JsValue::null();
let arg1 = JsValue::from(specifier.to_string());
let arg2 = JsValue::from(is_dynamic);
let result = self.load.call2(&context, &arg1, &arg2);
let arg3 = JsValue::from(match cache_setting {
// note: keep these values aligned with deno_cache
LoaderCacheSetting::Only => "only",
LoaderCacheSetting::Prefer => "prefer",
LoaderCacheSetting::Reload => "reload",
});
let result = self.load.call3(&context, &arg1, &arg2, &arg3);
let f = async move {
let response = match result {
Ok(result) => {
Expand Down Expand Up @@ -247,6 +255,7 @@ pub async fn js_create_graph(
module_analyzer: None,
imports,
reporter: None,
workspace_members: Vec::new(),
},
)
.await;
Expand Down
7 changes: 7 additions & 0 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ pub struct PositionRange {
}

impl PositionRange {
pub fn zeroed() -> Self {
Self {
start: Position::zeroed(),
end: Position::zeroed(),
}
}

pub fn from_source_range(
range: SourceRange,
text_info: &SourceTextInfo,
Expand Down
90 changes: 90 additions & 0 deletions src/deno.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

use std::collections::BTreeMap;
use std::collections::HashMap;

use deno_semver::package::PackageNv;
use deno_semver::package::PackageReq;
use deno_semver::Version;
use deno_semver::VersionReq;
use serde::Deserialize;
use serde::Serialize;

use crate::ModuleInfo;

#[derive(Serialize, Deserialize, Clone)]
pub struct DenoPackageInfo {
pub versions: HashMap<Version, DenoPackageInfoVersion>,
}

#[derive(Serialize, Deserialize, Clone, Default)]
pub struct DenoPackageInfoVersion {
// currently not supported because it doesn't work in workspaces
// pub main: Option<String>,
}

#[derive(Serialize, Deserialize, Clone, Default)]
pub struct DenoPackageVersionInfo {
#[serde(rename = "moduleGraph1")]
pub module_graph: Option<serde_json::Value>,
}

impl DenoPackageVersionInfo {
pub fn module_info(&self, specifier: &str) -> Option<ModuleInfo> {
let module_graph = self.module_graph.as_ref()?.as_object()?;
let module_info = module_graph.get(specifier)?;
serde_json::from_value(module_info.clone()).ok()
}
}

#[derive(Debug, Clone, Default, Serialize)]
pub struct DenoSpecifierSnapshot {
#[serde(flatten)]
package_reqs: BTreeMap<PackageReq, PackageNv>,
#[serde(skip_serializing)]
packages_by_name: HashMap<String, Vec<PackageNv>>,
}

impl DenoSpecifierSnapshot {
pub fn is_empty(&self) -> bool {
self.package_reqs.is_empty()
}

pub fn add(&mut self, package_req: PackageReq, nv: PackageNv) {
let nvs = self
.packages_by_name
.entry(package_req.name.clone())
.or_default();
if !nvs.contains(&nv) {
nvs.push(nv.clone());
}
self.package_reqs.insert(package_req, nv);
}

pub fn versions_by_name(&self, name: &str) -> Option<&Vec<PackageNv>> {
self.packages_by_name.get(name)
}

pub fn mappings(&self) -> impl Iterator<Item = (&PackageReq, &PackageNv)> {
self.package_reqs.iter()
}
}

pub fn resolve_version<'a>(
version_req: &VersionReq,
versions: impl Iterator<Item = &'a Version>,
) -> Option<&'a Version> {
let mut maybe_best_version: Option<&Version> = None;
for version in versions {
if version_req.matches(version) {
let is_best_version = maybe_best_version
.as_ref()
.map(|best_version| (*best_version).cmp(version).is_lt())
.unwrap_or(true);
if is_best_version {
maybe_best_version = Some(version);
}
}
}
maybe_best_version
}
Loading
Loading