-
Notifications
You must be signed in to change notification settings - Fork 5.8k
feat: include @types/node type declarations out of the box
#31502
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
base: main
Are you sure you want to change the base?
Changes from 22 commits
0af5314
2b5e154
c698f40
f1b495c
2b0ffa7
022ba86
1b5218c
85d57ba
edd9f2d
db7b17d
f7559d3
a4db9b3
c4c792b
303b45a
8b6b8af
a1ab766
24c96a8
deac893
8185011
20212b3
4c013d9
07e29c1
2d12f42
a8db720
eab21b3
c1770e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,6 @@ fn compress_decls(out_dir: &Path) { | |
| "lib.deno.window.d.ts", | ||
| "lib.deno.worker.d.ts", | ||
| "lib.deno.shared_globals.d.ts", | ||
| "lib.deno.ns.d.ts", | ||
| "lib.deno.unstable.d.ts", | ||
| "lib.deno_console.d.ts", | ||
| "lib.deno_url.d.ts", | ||
|
|
@@ -122,6 +121,7 @@ fn compress_decls(out_dir: &Path) { | |
| "lib.esnext.iterator.d.ts", | ||
| "lib.esnext.promise.d.ts", | ||
| "lib.esnext.sharedmemory.d.ts", | ||
| "lib.node.d.ts", | ||
| "lib.scripthost.d.ts", | ||
| "lib.webworker.asynciterable.d.ts", | ||
| "lib.webworker.d.ts", | ||
|
|
@@ -134,6 +134,71 @@ fn compress_decls(out_dir: &Path) { | |
| } | ||
| } | ||
|
|
||
| fn process_node_types(out_dir: &Path) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should see about extracting the libs out to a separate crate so it can be done in parallel (I haven't measured if it's worth it yet though).
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I measured it and it was 873ms. It's probably worth extracting out to a separate crate after this lands. |
||
| let root_dir = Path::new(".").canonicalize().unwrap(); | ||
| let dts_dir = root_dir.join("tsc").join("dts"); | ||
| let node_dir = dts_dir.join("node"); | ||
|
|
||
| // Recursively find all .d.ts files in the node directory | ||
| fn visit_dirs(dir: &Path, cb: &mut dyn FnMut(&Path)) -> std::io::Result<()> { | ||
| for entry in std::fs::read_dir(dir)? { | ||
| let entry = entry?; | ||
| let path = entry.path(); | ||
| if path.is_dir() { | ||
| visit_dirs(&path, cb)?; | ||
| } else if path.extension().and_then(|s| s.to_str()) == Some("ts") | ||
| || path.extension().and_then(|s| s.to_str()) == Some("cts") | ||
| { | ||
| cb(&path); | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| println!("cargo:rerun-if-changed={}", node_dir.display()); | ||
|
|
||
| let mut paths = Vec::new(); | ||
| visit_dirs(&node_dir, &mut |path| { | ||
| paths.push(path.to_path_buf()); | ||
| }) | ||
| .unwrap(); | ||
|
|
||
| // Sort for deterministic builds | ||
| paths.sort(); | ||
|
|
||
| // Compress all the files if release | ||
| if !cfg!(debug_assertions) && std::env::var("CARGO_FEATURE_HMR").is_err() { | ||
| for path in &paths { | ||
| let relative = path.strip_prefix(&root_dir).unwrap(); | ||
| compress_source(out_dir, &relative.to_string_lossy()); | ||
| } | ||
| } | ||
|
|
||
| // Generate a Rust file with the node type entries (always, for both debug and release) | ||
| let mut generated = String::from("// Auto-generated by build.rs\n"); | ||
| generated.push_str( | ||
| "// This macro expands to an array of maybe_compressed_lib! entries\n", | ||
| ); | ||
| generated.push_str("macro_rules! node_type_libs {\n"); | ||
| generated.push_str(" () => {\n"); | ||
| generated.push_str(" [\n"); | ||
|
|
||
| for path in paths { | ||
| let relative = path.strip_prefix(&dts_dir).unwrap(); | ||
| let relative_str = relative.to_string_lossy().replace('\\', "/"); | ||
| generated.push_str(&format!( | ||
| " maybe_compressed_lib!(\"{}\"),\n", | ||
| relative_str | ||
| )); | ||
| } | ||
|
|
||
| generated.push_str(" ]\n"); | ||
| generated.push_str(" };\n"); | ||
| generated.push_str("}\n"); | ||
|
|
||
| std::fs::write(out_dir.join("node_types.rs"), generated).unwrap(); | ||
| } | ||
|
|
||
| fn compress_source(out_dir: &Path, file: &str) { | ||
| let path = Path::new(file) | ||
| .canonicalize() | ||
|
|
@@ -200,9 +265,11 @@ fn main() { | |
| // To debug snapshot issues uncomment: | ||
| // op_fetch_asset::trace_serializer(); | ||
|
|
||
| let out_dir = std::path::PathBuf::from(std::env::var_os("OUT_DIR").unwrap()); | ||
|
|
||
| process_node_types(&out_dir); | ||
|
|
||
| if !cfg!(debug_assertions) && std::env::var("CARGO_FEATURE_HMR").is_err() { | ||
| let out_dir = | ||
| std::path::PathBuf::from(std::env::var_os("OUT_DIR").unwrap()); | ||
| compress_sources(&out_dir); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicate (see above)