From 6b478cd0a3fdff15d5d9d8849a3019652b919921 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Tue, 19 Nov 2024 18:20:14 -0500 Subject: [PATCH] feat(compile): ability to embed directory in executable (#26939) --- cli/args/flags.rs | 6 ++--- cli/standalone/binary.rs | 14 ++++++++--- .../compile/include_folder/__test__.jsonc | 24 +++++++++++++++++++ tests/specs/compile/include_folder/data/a.txt | 1 + tests/specs/compile/include_folder/data/b.txt | 1 + tests/specs/compile/include_folder/main.js | 8 +++++++ tests/specs/compile/include_folder/output.out | 2 ++ 7 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 tests/specs/compile/include_folder/__test__.jsonc create mode 100644 tests/specs/compile/include_folder/data/a.txt create mode 100644 tests/specs/compile/include_folder/data/b.txt create mode 100644 tests/specs/compile/include_folder/main.js create mode 100644 tests/specs/compile/include_folder/output.out diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 85b8abefe1..f40d5aed41 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -1909,10 +1909,10 @@ On the first invocation with deno will download the proper binary and cache it i Arg::new("include") .long("include") .help( - cstr!("Includes an additional module or local data file in the compiled executable. + cstr!("Includes an additional module or file/directory in the compiled executable. Use this flag if a dynamically imported module or a web worker main module - fails to load in the executable or to embed a file in the executable. This flag can - be passed multiple times, to include multiple additional modules.", + fails to load in the executable or to embed a file or directory in the executable. + This flag can be passed multiple times, to include multiple additional modules.", )) .action(ArgAction::Append) .value_hint(ValueHint::FilePath) diff --git a/cli/standalone/binary.rs b/cli/standalone/binary.rs index 8153993137..39e508dc3b 100644 --- a/cli/standalone/binary.rs +++ b/cli/standalone/binary.rs @@ -620,9 +620,17 @@ impl<'a> DenoCompileBinaryWriter<'a> { }; for include_file in include_files { let path = deno_path_util::url_to_file_path(include_file)?; - vfs - .add_file_at_path(&path) - .with_context(|| format!("Including {}", path.display()))?; + if path.is_dir() { + // TODO(#26941): we should analyze if any of these are + // modules in order to include their dependencies + vfs + .add_dir_recursive(&path) + .with_context(|| format!("Including {}", path.display()))?; + } else { + vfs + .add_file_at_path(&path) + .with_context(|| format!("Including {}", path.display()))?; + } } let mut remote_modules_store = RemoteModulesStoreBuilder::default(); let mut code_cache_key_hasher = if self.cli_options.code_cache_enabled() { diff --git a/tests/specs/compile/include_folder/__test__.jsonc b/tests/specs/compile/include_folder/__test__.jsonc new file mode 100644 index 0000000000..eeaea53b65 --- /dev/null +++ b/tests/specs/compile/include_folder/__test__.jsonc @@ -0,0 +1,24 @@ +{ + "tempDir": true, + "steps": [{ + "if": "unix", + "args": "compile --allow-read=data --include data --output main main.js", + "output": "[WILDCARD]" + }, { + "if": "unix", + "commandName": "./main", + "args": [], + "output": "output.out", + "exitCode": 0 + }, { + "if": "windows", + "args": "compile --allow-read=data --include data --output main.exe main.js", + "output": "[WILDCARD]" + }, { + "if": "windows", + "commandName": "./main.exe", + "args": [], + "output": "output.out", + "exitCode": 0 + }] +} diff --git a/tests/specs/compile/include_folder/data/a.txt b/tests/specs/compile/include_folder/data/a.txt new file mode 100644 index 0000000000..7898192261 --- /dev/null +++ b/tests/specs/compile/include_folder/data/a.txt @@ -0,0 +1 @@ +a diff --git a/tests/specs/compile/include_folder/data/b.txt b/tests/specs/compile/include_folder/data/b.txt new file mode 100644 index 0000000000..6178079822 --- /dev/null +++ b/tests/specs/compile/include_folder/data/b.txt @@ -0,0 +1 @@ +b diff --git a/tests/specs/compile/include_folder/main.js b/tests/specs/compile/include_folder/main.js new file mode 100644 index 0000000000..831d26167f --- /dev/null +++ b/tests/specs/compile/include_folder/main.js @@ -0,0 +1,8 @@ +const dataDir = import.meta.dirname + "/data"; +const files = Array.from( + Deno.readDirSync(dataDir).map((entry) => dataDir + "/" + entry.name), +); +files.sort(); +for (const file of files) { + console.log(Deno.readTextFileSync(file).trim()); +} diff --git a/tests/specs/compile/include_folder/output.out b/tests/specs/compile/include_folder/output.out new file mode 100644 index 0000000000..422c2b7ab3 --- /dev/null +++ b/tests/specs/compile/include_folder/output.out @@ -0,0 +1,2 @@ +a +b