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

fix: regression - make node: specifiers work #393

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions deno.lock

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

Binary file modified lib/pkg/dnt_wasm_bg.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions rs-lib/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ impl ModuleGraph {
None
}
})
.filter(|s| !matches!(s.scheme(), "node"))
}

pub fn all_modules(&self) -> impl Iterator<Item = &Module> {
Expand Down
5 changes: 5 additions & 0 deletions rs-lib/src/loader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ impl<'a> deno_graph::source::Loader for SourceLoader<'a> {
let loader = self.loader.clone();
let specifier = specifier.to_owned();
Box::pin(async move {
if specifier.scheme() == "node" {
return Ok(Some(deno_graph::source::LoadResponse::External {
specifier,
}));
}
let resp = loader
.load(
specifier.clone(),
Expand Down
19 changes: 14 additions & 5 deletions rs-lib/src/specifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,20 @@ pub fn get_specifiers<'a>(
.collect::<Vec<_>>();

for module in all_modules.iter() {
match module.specifier().scheme().to_lowercase().as_str() {
"file" => local_specifiers.push(module.specifier().clone()),
"http" | "https" => remote_specifiers.push(module.specifier().clone()),
_ => {
anyhow::bail!("Unhandled scheme on url: {}", module.specifier());
match module {
Module::Js(_) | Module::Json(_) => {
match module.specifier().scheme().to_lowercase().as_str() {
"file" => local_specifiers.push(module.specifier().clone()),
"http" | "https" => {
remote_specifiers.push(module.specifier().clone())
}
_ => {
anyhow::bail!("Unhandled scheme on url: {}", module.specifier());
}
}
}
Module::Npm(_) | Module::Node(_) | Module::External(_) => {
// ignore
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions rs-lib/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2060,6 +2060,25 @@ async fn module_decl_string_literal_change_specifier() {
);
}

#[tokio::test]
async fn node_specifier() {
let result = TestBuilder::new()
.with_loader(|loader| {
loader.add_local_file(
"/mod.ts",
"import * as fs from 'node:fs'; console.log(fs);",
);
})
.transform()
.await
.unwrap();

assert_files!(
result.main.files,
&[("mod.ts", "import * as fs from 'node:fs'; console.log(fs);"),]
);
}

fn get_shim_file_text(mut text: String) -> String {
text.push('\n');
text.push_str(
Expand Down
2 changes: 2 additions & 0 deletions tests/jsr_project/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import { parse } from "jsr:@std/csv/parse";
import { assertEquals } from "jsr:@std/assert/assert_equals";
import * as fs from "node:fs";

export function add(a: number, b: number) {
console.log(fs.readFileSync);
const result = parse("a,b,c\n1,2,3\n4,5,6");
assertEquals(result[0], ["a", "b", "c"]);
return a + b;
Expand Down
Loading