Skip to content

Commit deadab9

Browse files
committed
fix: map entrypoints
1 parent 9c933f8 commit deadab9

File tree

9 files changed

+79
-27
lines changed

9 files changed

+79
-27
lines changed

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"deno",
2020
"src/rs_lib/target",
2121
"target",
22-
"tests/jsx/testdata"
22+
"tests/**/testdata"
2323
],
2424
"imports": {
2525
"@david/dax": "jsr:@david/dax@^0.43.2",

src/rs_lib/lib.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use deno_cache_dir::file_fetcher::NullBlobStore;
1616
use deno_graph::MediaType;
1717
use deno_graph::Module;
1818
use deno_graph::ModuleGraph;
19+
use deno_graph::Position;
1920
use deno_graph::analysis::ModuleAnalyzer;
2021
use deno_graph::ast::DefaultModuleAnalyzer;
2122
use deno_graph::ast::EsParser;
@@ -339,10 +340,9 @@ impl DenoLoader {
339340
&mut self,
340341
roots: Vec<String>,
341342
) -> Result<(), anyhow::Error> {
342-
let cwd = self.workspace_factory.initial_cwd();
343343
let roots = roots
344344
.into_iter()
345-
.map(|e| parse_entrypoint(e, &cwd))
345+
.map(|e| self.resolve_entrypoint(e))
346346
.collect::<Result<Vec<_>, _>>()?;
347347
let npm_package_info_provider = self
348348
.npm_installer_factory
@@ -441,9 +441,7 @@ impl DenoLoader {
441441
)?,
442442
),
443443
None => {
444-
let entrypoint =
445-
parse_entrypoint(specifier, self.workspace_factory.initial_cwd())?
446-
.to_string();
444+
let entrypoint = self.resolve_entrypoint(specifier)?.to_string();
447445
(
448446
entrypoint,
449447
deno_path_util::url_from_file_path(
@@ -599,6 +597,26 @@ impl DenoLoader {
599597
Ok(Cow::Borrowed(source.as_bytes()))
600598
}
601599
}
600+
601+
fn resolve_entrypoint(
602+
&self,
603+
specifier: String,
604+
) -> Result<Url, anyhow::Error> {
605+
let cwd = self.workspace_factory.initial_cwd();
606+
if specifier.contains('\\') {
607+
return Ok(deno_path_util::url_from_file_path(&resolve_absolute_path(
608+
specifier, cwd,
609+
))?);
610+
}
611+
let referrer = deno_path_util::url_from_directory_path(cwd)?;
612+
Ok(self.resolver.resolve(
613+
&specifier,
614+
&referrer,
615+
Position::zeroed(),
616+
node_resolver::ResolutionMode::Import,
617+
node_resolver::NodeResolutionKind::Execution,
618+
)?)
619+
}
602620
}
603621

604622
fn create_module_response(
@@ -641,24 +659,6 @@ fn create_external_repsonse(url: &Url) -> JsValue {
641659
obj.into()
642660
}
643661

644-
fn parse_entrypoint(
645-
entrypoint: String,
646-
cwd: &Path,
647-
) -> Result<Url, anyhow::Error> {
648-
if entrypoint.starts_with("jsr:")
649-
|| entrypoint.starts_with("https:")
650-
|| entrypoint.starts_with("http:")
651-
|| entrypoint.starts_with("file:")
652-
|| entrypoint.starts_with("npm:")
653-
{
654-
Ok(Url::parse(&entrypoint)?)
655-
} else {
656-
Ok(deno_path_util::url_from_file_path(&resolve_absolute_path(
657-
entrypoint, cwd,
658-
))?)
659-
}
660-
}
661-
662662
fn resolve_absolute_path(path: String, cwd: &Path) -> PathBuf {
663663
let path = sys_traits::impls::wasm_string_to_path(path);
664664
cwd.join(path)

tests/helpers.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,19 @@ export async function createLoader(
2121
};
2222
}
2323

24-
export function assertResponseText(response: LoadResponse, text: string) {
24+
export function assertResponseText(
25+
response: LoadResponse,
26+
text: string,
27+
opts?: { skipSourceMap?: boolean },
28+
) {
2529
assertEquals(response.kind, "module");
2630
const moduleResponse = response as ModuleLoadResponse;
27-
assertEquals(new TextDecoder().decode(moduleResponse.code), text);
31+
let actualText = new TextDecoder().decode(moduleResponse.code);
32+
if (opts?.skipSourceMap) {
33+
actualText = actualText.replace(
34+
/\/\/# sourceMappingURL=.*$/,
35+
"",
36+
);
37+
}
38+
assertEquals(actualText, text);
2839
}

tests/jsx/main.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { assert } from "node:console";
21
import {
32
assertResponseText,
43
createLoader,
54
ResolutionMode,
65
} from "../helpers.ts";
6+
import { assert } from "@std/assert";
77

88
Deno.test("loads jsx transpiled", async () => {
99
const mainJsx = import.meta.dirname + "/testdata/main.jsx";
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {
2+
assertResponseText,
3+
createLoader,
4+
ResolutionMode,
5+
} from "../helpers.ts";
6+
7+
Deno.test("loads linked entrypoint", async () => {
8+
const mainFile = import.meta.dirname + "/testdata/main/main.ts";
9+
const { loader } = await createLoader({
10+
configPath: import.meta.dirname + "/testdata/main/deno.json",
11+
}, {
12+
entrypoints: [mainFile, "jsr:@denotest/add", "@denotest/add"],
13+
});
14+
15+
const response = await loader.load(
16+
loader.resolve("@denotest/add", undefined, ResolutionMode.Import),
17+
);
18+
assertResponseText(
19+
response,
20+
`export function add(a, b) {
21+
return a + b;
22+
}
23+
`,
24+
{ skipSourceMap: true },
25+
);
26+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "@denotest/add",
3+
"exports": "./mod.ts"
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function add(a: number, b: number) {
2+
return a + b;
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"links": [
3+
"../add"
4+
]
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { add } from "@denotest/add";
2+
3+
console.log(add(1, 2));

0 commit comments

Comments
 (0)