diff options
Diffstat (limited to 'cli/node/analyze.rs')
-rw-r--r-- | cli/node/analyze.rs | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/cli/node/analyze.rs b/cli/node/analyze.rs index 9da2cbf4b..270615cca 100644 --- a/cli/node/analyze.rs +++ b/cli/node/analyze.rs @@ -74,7 +74,15 @@ pub fn esm_code_with_node_globals( write!(result, "var {0} = {1}.{0};", global, global_this_expr).unwrap(); } - result.push_str(parsed_source.text_info().text_str()); + let file_text = parsed_source.text_info().text_str(); + // strip the shebang + let file_text = if file_text.starts_with("#!/") { + let start_index = file_text.find('\n').unwrap_or(file_text.len()); + &file_text[start_index..] + } else { + file_text + }; + result.push_str(file_text); Ok(result) } @@ -158,4 +166,24 @@ mod tests { assert!(r.contains("var process = globalThis.process;")); assert!(r.contains("export const x = 1;")); } + + #[test] + fn test_esm_code_with_node_globals_with_shebang() { + let r = esm_code_with_node_globals( + &ModuleSpecifier::parse("https://example.com/foo/bar.js").unwrap(), + "#!/usr/bin/env node\nexport const x = 1;".to_string(), + ) + .unwrap(); + assert_eq!( + r, + concat!( + "var globalThis = Deno[Deno.internal].node.globalThis;var Buffer = globalThis.Buffer;", + "var clearImmediate = globalThis.clearImmediate;var clearInterval = globalThis.clearInterval;", + "var clearTimeout = globalThis.clearTimeout;var global = globalThis.global;", + "var process = globalThis.process;var setImmediate = globalThis.setImmediate;", + "var setInterval = globalThis.setInterval;var setTimeout = globalThis.setTimeout;\n", + "export const x = 1;" + ), + ); + } } |