mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 15:24:46 -05:00
fix(cli): bundle command support shebang file (#17113)
This commit is contained in:
parent
7716449d41
commit
501472f06b
4 changed files with 39 additions and 2 deletions
|
@ -470,4 +470,9 @@ mod bundle {
|
|||
http_server: true,
|
||||
exit_code: 1,
|
||||
});
|
||||
|
||||
itest!(bundle_shebang_file {
|
||||
args: "bundle subdir/shebang_file.js",
|
||||
output: "bundle/shebang_file.bundle.out",
|
||||
});
|
||||
}
|
||||
|
|
10
cli/tests/testdata/bundle/shebang_file.bundle.out
vendored
Normal file
10
cli/tests/testdata/bundle/shebang_file.bundle.out
vendored
Normal file
|
@ -0,0 +1,10 @@
|
|||
[WILDCARD]
|
||||
#!/usr/bin/env -S deno run --allow-read
|
||||
// deno-fmt-ignore-file
|
||||
// deno-lint-ignore-file
|
||||
// This code was bundled using `deno bundle` and it's not recommended to edit it manually
|
||||
|
||||
for (const item of Deno.readDirSync(".")){
|
||||
console.log(item.name);
|
||||
}
|
||||
|
5
cli/tests/testdata/subdir/shebang_file.js
vendored
Normal file
5
cli/tests/testdata/subdir/shebang_file.js
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
#!/usr/bin/env -S deno run --allow-read
|
||||
|
||||
for (const item of Deno.readDirSync(".")) {
|
||||
console.log(item.name);
|
||||
}
|
|
@ -147,12 +147,29 @@ fn bundle_module_graph(
|
|||
}
|
||||
}
|
||||
|
||||
deno_emit::bundle_graph(
|
||||
let mut output = deno_emit::bundle_graph(
|
||||
graph,
|
||||
deno_emit::BundleOptions {
|
||||
bundle_type: deno_emit::BundleType::Module,
|
||||
emit_options: ts_config_result.ts_config.into(),
|
||||
emit_ignore_directives: true,
|
||||
},
|
||||
)
|
||||
)?;
|
||||
|
||||
// todo(https://github.com/denoland/deno_emit/issues/85): move to deno_emit
|
||||
if let Some(shebang) = shebang_file(graph) {
|
||||
output.code = format!("{}\n{}", shebang, output.code);
|
||||
}
|
||||
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn shebang_file(graph: &deno_graph::ModuleGraph) -> Option<String> {
|
||||
let source = graph.get(&graph.roots[0].0)?.maybe_source.as_ref()?;
|
||||
let first_line = source.lines().next()?;
|
||||
if first_line.starts_with("#!") {
|
||||
Some(first_line.to_string())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue