Skip to content

Commit

Permalink
fix: regression - make node: specifiers work (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Mar 27, 2024
1 parent dece171 commit 18495b7
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 5 deletions.
5 changes: 5 additions & 0 deletions deno.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"jsr:@std/path@^0.218.2": "jsr:@std/path@0.218.2",
"jsr:@std/testing@0.220": "jsr:@std/testing@0.220.1",
"npm:@ts-morph/bootstrap@0.22": "npm:@ts-morph/bootstrap@0.22.0",
"npm:@types/node": "npm:@types/node@18.16.19",
"npm:code-block-writer@^13.0.1": "npm:code-block-writer@13.0.1",
"npm:using-statement@^0.4": "npm:using-statement@0.4.2"
},
Expand Down Expand Up @@ -119,6 +120,10 @@
"path-browserify": "path-browserify@1.0.1"
}
},
"@types/node@18.16.19": {
"integrity": "sha512-IXl7o+R9iti9eBW4Wg2hx1xQDig183jj7YLn8F7udNceyfkbn1ZxmzZXuak20gR40D7pIkIY1kYGx5VIGbaHKA==",
"dependencies": {}
},
"balanced-match@1.0.2": {
"integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==",
"dependencies": {}
Expand Down
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

0 comments on commit 18495b7

Please sign in to comment.