Skip to content

Commit 78a0912

Browse files
authored
feat: ability to skip loading statically analyzable dynamic dependencies (#574)
1 parent 961df5f commit 78a0912

File tree

6 files changed

+114
-0
lines changed

6 files changed

+114
-0
lines changed

lib/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ pub async fn js_create_graph(
309309
&loader,
310310
BuildOptions {
311311
is_dynamic: false,
312+
skip_dynamic_deps: false,
312313
resolver: maybe_resolver.as_ref().map(|r| r as &dyn Resolver),
313314
// todo(dsherret): actually implement this for Wasm users
314315
// and don't just use a RealSys here as it would be better

src/graph.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,8 @@ pub struct BuildFastCheckTypeGraphOptions<'a> {
12551255

12561256
pub struct BuildOptions<'a> {
12571257
pub is_dynamic: bool,
1258+
/// Skip loading statically analyzable dynamic dependencies.
1259+
pub skip_dynamic_deps: bool,
12581260
/// Additional imports that should be brought into the scope of
12591261
/// the module graph to add to the graph's "imports". This may
12601262
/// be extra modules such as TypeScript's "types" option or JSX
@@ -1278,6 +1280,7 @@ impl Default for BuildOptions<'_> {
12781280
fn default() -> Self {
12791281
Self {
12801282
is_dynamic: false,
1283+
skip_dynamic_deps: false,
12811284
imports: Default::default(),
12821285
executor: Default::default(),
12831286
locker: None,
@@ -3606,6 +3609,7 @@ impl FillPassMode {
36063609

36073610
struct Builder<'a, 'graph> {
36083611
in_dynamic_branch: bool,
3612+
skip_dynamic_deps: bool,
36093613
was_dynamic_root: bool,
36103614
file_system: &'a FileSystem,
36113615
jsr_url_provider: &'a dyn JsrUrlProvider,
@@ -3636,6 +3640,7 @@ impl<'a, 'graph> Builder<'a, 'graph> {
36363640
};
36373641
let mut builder = Self {
36383642
in_dynamic_branch: options.is_dynamic,
3643+
skip_dynamic_deps: options.skip_dynamic_deps,
36393644
was_dynamic_root: options.is_dynamic,
36403645
file_system: options.file_system,
36413646
jsr_url_provider: options.jsr_url_provider,
@@ -5187,6 +5192,10 @@ impl<'a, 'graph> Builder<'a, 'graph> {
51875192
maybe_version_info: Option<&JsrPackageVersionInfoExt>,
51885193
) {
51895194
for dep in dependencies.values_mut() {
5195+
if dep.is_dynamic && self.skip_dynamic_deps {
5196+
continue;
5197+
}
5198+
51905199
if matches!(self.graph.graph_kind, GraphKind::All | GraphKind::CodeOnly)
51915200
|| dep.maybe_type.is_none()
51925201
{

tests/ecosystem_test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ async fn test_version(
290290
&loader,
291291
BuildOptions {
292292
is_dynamic: false,
293+
skip_dynamic_deps: false,
293294
module_analyzer: &module_analyzer,
294295
file_system: &NullFileSystem,
295296
locker: None,

tests/helpers/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ pub struct TestBuilder {
175175
entry_point: String,
176176
entry_point_types: String,
177177
fast_check_cache: bool,
178+
skip_dynamic_deps: bool,
178179
workspace_members: Vec<WorkspaceMember>,
179180
workspace_fast_check: bool,
180181
lockfile_jsr_packages: BTreeMap<PackageReq, PackageNv>,
@@ -189,6 +190,7 @@ impl TestBuilder {
189190
entry_point: "file:///mod.ts".to_string(),
190191
entry_point_types: "file:///mod.ts".to_string(),
191192
fast_check_cache: false,
193+
skip_dynamic_deps: false,
192194
workspace_members: Default::default(),
193195
workspace_fast_check: false,
194196
lockfile_jsr_packages: Default::default(),
@@ -245,6 +247,12 @@ impl TestBuilder {
245247
self
246248
}
247249

250+
#[allow(unused)]
251+
pub fn skip_dynamic_deps(&mut self, value: bool) -> &mut Self {
252+
self.skip_dynamic_deps = value;
253+
self
254+
}
255+
248256
#[allow(unused)]
249257
pub fn ensure_locker(&mut self) -> &mut Self {
250258
self.locker.get_or_insert_with(Default::default);
@@ -297,6 +305,7 @@ impl TestBuilder {
297305
npm_resolver: Some(&TestNpmResolver),
298306
locker: self.locker.as_mut().map(|l| l as _),
299307
resolver: Some(&resolver),
308+
skip_dynamic_deps: self.skip_dynamic_deps,
300309
..Default::default()
301310
},
302311
)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
~~ {"skipDynamicDeps":true} ~~
2+
# https://localhost/mod.ts
3+
console.log(123);
4+
await import("https://localhost/other.ts");
5+
6+
# https://localhost/other.ts
7+
console.log("This should not be loaded");
8+
9+
# mod.ts
10+
import 'https://localhost/mod.ts'
11+
await import("https://localhost/other.ts");
12+
13+
# output
14+
{
15+
"roots": [
16+
"file:///mod.ts"
17+
],
18+
"modules": [
19+
{
20+
"kind": "esm",
21+
"dependencies": [
22+
{
23+
"specifier": "https://localhost/mod.ts",
24+
"code": {
25+
"specifier": "https://localhost/mod.ts",
26+
"resolutionMode": "import",
27+
"span": {
28+
"start": {
29+
"line": 0,
30+
"character": 7
31+
},
32+
"end": {
33+
"line": 0,
34+
"character": 33
35+
}
36+
}
37+
}
38+
},
39+
{
40+
"specifier": "https://localhost/other.ts",
41+
"code": {
42+
"specifier": "https://localhost/other.ts",
43+
"resolutionMode": "import",
44+
"span": {
45+
"start": {
46+
"line": 1,
47+
"character": 13
48+
},
49+
"end": {
50+
"line": 1,
51+
"character": 41
52+
}
53+
}
54+
},
55+
"isDynamic": true
56+
}
57+
],
58+
"size": 78,
59+
"mediaType": "TypeScript",
60+
"specifier": "file:///mod.ts"
61+
},
62+
{
63+
"kind": "esm",
64+
"dependencies": [
65+
{
66+
"specifier": "https://localhost/other.ts",
67+
"code": {
68+
"specifier": "https://localhost/other.ts",
69+
"resolutionMode": "import",
70+
"span": {
71+
"start": {
72+
"line": 1,
73+
"character": 13
74+
},
75+
"end": {
76+
"line": 1,
77+
"character": 41
78+
}
79+
}
80+
},
81+
"isDynamic": true
82+
}
83+
],
84+
"size": 62,
85+
"mediaType": "TypeScript",
86+
"specifier": "https://localhost/mod.ts"
87+
}
88+
],
89+
"redirects": {}
90+
}

tests/specs_test.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ fn run_graph_test(test: &CollectedTest) {
9191
builder.lockfile_jsr_packages(spec.lockfile_jsr_packages.clone());
9292

9393
if let Some(options) = &spec.options {
94+
builder.skip_dynamic_deps(options.skip_dynamic_deps);
9495
builder.workspace_fast_check(options.workspace_fast_check);
9596
builder.fast_check_cache(options.fast_check_cache);
9697
if let Some(checksums) = options.remote_checksums.as_ref() {
@@ -325,6 +326,9 @@ pub struct SpecOptions {
325326
#[serde(default)]
326327
#[serde(skip_serializing_if = "is_false")]
327328
pub fast_check_cache: bool,
329+
#[serde(default)]
330+
#[serde(skip_serializing_if = "is_false")]
331+
pub skip_dynamic_deps: bool,
328332
}
329333

330334
fn is_false(v: &bool) -> bool {

0 commit comments

Comments
 (0)