Skip to content

Commit 024c08e

Browse files
committed
refactor: load module content an owned value
This is a pre-requisite for WASM imports that additionally allows us to minimize clones when doing things like BOM stripping.
1 parent cbf7134 commit 024c08e

File tree

3 files changed

+32
-47
lines changed

3 files changed

+32
-47
lines changed

src/graph.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ fn resolve(
11771177
pub(crate) fn parse_module(
11781178
specifier: &ModuleSpecifier,
11791179
maybe_headers: Option<&HashMap<String, String>>,
1180-
content: Arc<String>,
1180+
content: String,
11811181
maybe_assert_type: Option<&str>,
11821182
maybe_kind: Option<&ModuleKind>,
11831183
maybe_resolver: Option<&dyn Resolver>,
@@ -1187,6 +1187,8 @@ pub(crate) fn parse_module(
11871187
) -> ModuleSlot {
11881188
let media_type = get_media_type(specifier, maybe_headers);
11891189

1190+
let content = content.into();
1191+
11901192
// here we check any media types that should have assertions made against them
11911193
// if they aren't the root and add them to the graph, otherwise we continue
11921194
if media_type == MediaType::Json
@@ -1565,7 +1567,7 @@ impl<'a> Builder<'a> {
15651567
Some((specifier, kind, Ok(Some(response)))) => {
15661568
let assert_types =
15671569
self.pending_assert_types.remove(&specifier).unwrap();
1568-
self.visit(&specifier, &kind, &response, &build_kind, assert_types);
1570+
self.visit(&specifier, &kind, response, &build_kind, assert_types);
15691571
Some(specifier)
15701572
}
15711573
Some((specifier, _, Ok(None))) => {
@@ -1696,7 +1698,7 @@ impl<'a> Builder<'a> {
16961698
&mut self,
16971699
requested_specifier: &ModuleSpecifier,
16981700
kind: &ModuleKind,
1699-
response: &LoadResponse,
1701+
response: LoadResponse,
17001702
build_kind: &BuildKind,
17011703
assert_types: HashSet<Option<String>>,
17021704
) {
@@ -1744,18 +1746,15 @@ impl<'a> Builder<'a> {
17441746
&specifier,
17451747
kind,
17461748
maybe_headers.as_ref(),
1747-
content.clone(),
1749+
content,
17481750
build_kind,
17491751
assert_types.into_iter().next().unwrap(),
17501752
)
17511753
};
17521754
(specifier, module_slot)
17531755
}
17541756
};
1755-
self
1756-
.graph
1757-
.module_slots
1758-
.insert(specifier.clone(), module_slot);
1757+
self.graph.module_slots.insert(specifier, module_slot);
17591758
}
17601759

17611760
/// Visit a module, parsing it and resolving any dependencies.
@@ -1764,7 +1763,7 @@ impl<'a> Builder<'a> {
17641763
specifier: &ModuleSpecifier,
17651764
kind: &ModuleKind,
17661765
maybe_headers: Option<&HashMap<String, String>>,
1767-
content: Arc<String>,
1766+
content: String,
17681767
build_kind: &BuildKind,
17691768
maybe_assert_type: Option<String>,
17701769
) -> ModuleSlot {
@@ -2055,7 +2054,7 @@ mod tests {
20552054
fn test_module_dependency_includes() {
20562055
let specifier = ModuleSpecifier::parse("file:///a.ts").unwrap();
20572056
let source_parser = ast::DefaultSourceParser::default();
2058-
let content = Arc::new(r#"import * as b from "./b.ts";"#.to_string());
2057+
let content = r#"import * as b from "./b.ts";"#.to_string();
20592058
let slot = parse_module(
20602059
&specifier,
20612060
None,
@@ -2144,7 +2143,7 @@ mod tests {
21442143
Ok(Some(LoadResponse::Module {
21452144
specifier: specifier.clone(),
21462145
maybe_headers: None,
2147-
content: Arc::new("await import('file:///bar.js')".to_string()),
2146+
content: "await import('file:///bar.js')".to_string(),
21482147
}))
21492148
})
21502149
}
@@ -2155,7 +2154,7 @@ mod tests {
21552154
Ok(Some(LoadResponse::Module {
21562155
specifier: specifier.clone(),
21572156
maybe_headers: None,
2158-
content: Arc::new("import 'file:///baz.js'".to_string()),
2157+
content: "import 'file:///baz.js'".to_string(),
21592158
}))
21602159
})
21612160
}
@@ -2166,7 +2165,7 @@ mod tests {
21662165
Ok(Some(LoadResponse::Module {
21672166
specifier: specifier.clone(),
21682167
maybe_headers: None,
2169-
content: Arc::new("console.log('Hello, world!')".to_string()),
2168+
content: "console.log('Hello, world!')".to_string(),
21702169
}))
21712170
})
21722171
}
@@ -2210,7 +2209,7 @@ mod tests {
22102209
Ok(Some(LoadResponse::Module {
22112210
specifier: specifier.clone(),
22122211
maybe_headers: None,
2213-
content: Arc::new("await import('file:///bar.js')".to_string()),
2212+
content: "await import('file:///bar.js')".to_string(),
22142213
}))
22152214
}),
22162215
"file:///bar.js" => Box::pin(async move { Ok(None) }),
@@ -2270,14 +2269,14 @@ mod tests {
22702269
Ok(Some(LoadResponse::Module {
22712270
specifier: Url::parse("file:///foo_actual.js").unwrap(),
22722271
maybe_headers: None,
2273-
content: Arc::new("import 'file:///bar.js'".to_string()),
2272+
content: "import 'file:///bar.js'".to_string(),
22742273
}))
22752274
}),
22762275
"file:///bar.js" => Box::pin(async move {
22772276
Ok(Some(LoadResponse::Module {
22782277
specifier: Url::parse("file:///bar_actual.js").unwrap(),
22792278
maybe_headers: None,
2280-
content: Arc::new("(".to_string()),
2279+
content: "(".to_string(),
22812280
}))
22822281
}),
22832282
_ => unreachable!(),

src/lib.rs

+14-27
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use source::Resolver;
2525
use std::cell::RefCell;
2626
use std::collections::HashMap;
2727
use std::rc::Rc;
28-
use std::sync::Arc;
2928

3029
cfg_if! {
3130
if #[cfg(feature = "rust")] {
@@ -149,7 +148,7 @@ cfg_if! {
149148
pub fn parse_module(
150149
specifier: &ModuleSpecifier,
151150
maybe_headers: Option<&HashMap<String, String>>,
152-
content: Arc<String>,
151+
content: String,
153152
maybe_kind: Option<&ModuleKind>,
154153
maybe_resolver: Option<&dyn Resolver>,
155154
maybe_parser: Option<&dyn SourceParser>,
@@ -294,7 +293,7 @@ cfg_if! {
294293
match graph::parse_module(
295294
&specifier,
296295
maybe_headers.as_ref(),
297-
Arc::new(content),
296+
content,
298297
None,
299298
maybe_kind.as_ref(),
300299
maybe_resolver.as_ref().map(|r| r as &dyn Resolver),
@@ -2817,15 +2816,13 @@ export function a(a) {
28172816
let result = parse_module(
28182817
&specifier,
28192818
None,
2820-
Arc::new(
2821-
r#"
2819+
r#"
28222820
import { a } from "./a.ts";
28232821
import * as b from "./b.ts";
28242822
export { c } from "./c.ts";
28252823
const d = await import("./d.ts");
28262824
"#
2827-
.to_string(),
2828-
),
2825+
.to_string(),
28292826
None,
28302827
None,
28312828
None,
@@ -2843,13 +2840,11 @@ export function a(a) {
28432840
let result = parse_module(
28442841
&specifier,
28452842
None,
2846-
Arc::new(
2847-
r#"
2843+
r#"
28482844
import a from "./a.json" assert { type: "json" };
28492845
await import("./b.json", { assert: { type: "json" } });
28502846
"#
2851-
.to_string(),
2852-
),
2847+
.to_string(),
28532848
Some(&ModuleKind::Esm),
28542849
None,
28552850
None,
@@ -2910,16 +2905,14 @@ export function a(a) {
29102905
let result = parse_module(
29112906
&specifier,
29122907
None,
2913-
Arc::new(
2914-
r#"
2908+
r#"
29152909
/** @jsxImportSource https://example.com/preact */
29162910
29172911
export function A() {
29182912
return <div>Hello Deno</div>;
29192913
}
29202914
"#
2921-
.to_string(),
2922-
),
2915+
.to_string(),
29232916
Some(&ModuleKind::Esm),
29242917
None,
29252918
None,
@@ -2956,12 +2949,10 @@ export function a(a) {
29562949
let result = parse_module(
29572950
&specifier,
29582951
maybe_headers,
2959-
Arc::new(
2960-
r#"declare interface A {
2952+
r#"declare interface A {
29612953
a: string;
29622954
}"#
2963-
.to_string(),
2964-
),
2955+
.to_string(),
29652956
Some(&ModuleKind::Esm),
29662957
None,
29672958
None,
@@ -2975,8 +2966,7 @@ export function a(a) {
29752966
let result = parse_module(
29762967
&specifier,
29772968
None,
2978-
Arc::new(
2979-
r#"
2969+
r#"
29802970
/**
29812971
* Some js doc
29822972
*
@@ -2987,8 +2977,7 @@ export function a(a) {
29872977
return;
29882978
}
29892979
"#
2990-
.to_string(),
2991-
),
2980+
.to_string(),
29922981
Some(&ModuleKind::Esm),
29932982
None,
29942983
None,
@@ -3046,8 +3035,7 @@ export function a(a) {
30463035
let result = parse_module(
30473036
&specifier,
30483037
None,
3049-
Arc::new(
3050-
r#"
3038+
r#"
30513039
/**
30523040
* Some js doc
30533041
*
@@ -3058,8 +3046,7 @@ export function a(a: A): B {
30583046
return;
30593047
}
30603048
"#
3061-
.to_string(),
3062-
),
3049+
.to_string(),
30633050
Some(&ModuleKind::Esm),
30643051
None,
30653052
None,

src/source.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use std::collections::HashMap;
1919
use std::fmt;
2020
use std::path::PathBuf;
2121
use std::pin::Pin;
22-
use std::sync::Arc;
2322

2423
pub static DEFAULT_JSX_IMPORT_SOURCE_MODULE: &str = "jsx-runtime";
2524

@@ -56,7 +55,7 @@ pub enum LoadResponse {
5655
/// A loaded module.
5756
Module {
5857
/// The content of the remote module.
59-
content: Arc<String>,
58+
content: String,
6059
/// The final specifier of the module.
6160
specifier: ModuleSpecifier,
6261
/// If the module is a remote module, the headers should be returned as a
@@ -213,7 +212,7 @@ pub fn load_data_url(
213212
Ok(Some(LoadResponse::Module {
214213
specifier: specifier.clone(),
215214
maybe_headers: Some(headers),
216-
content: Arc::new(content),
215+
content,
217216
}))
218217
}
219218

@@ -265,7 +264,7 @@ impl MemoryLoader {
265264
})
266265
.collect()
267266
}),
268-
content: Arc::new(content.as_ref().to_string()),
267+
content: content.as_ref().to_string(),
269268
}),
270269
Source::BuiltIn(specifier) => Ok(LoadResponse::BuiltIn {
271270
specifier: ModuleSpecifier::parse(specifier.as_ref()).unwrap(),

0 commit comments

Comments
 (0)