Skip to content

Commit

Permalink
feat(compile): ability to embed directory in executable (#26939)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Nov 19, 2024
1 parent 46b6037 commit 6b478cd
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 6 deletions.
6 changes: 3 additions & 3 deletions cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
<p(245)>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)
Expand Down
14 changes: 11 additions & 3 deletions cli/standalone/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
24 changes: 24 additions & 0 deletions tests/specs/compile/include_folder/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -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
}]
}
1 change: 1 addition & 0 deletions tests/specs/compile/include_folder/data/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
1 change: 1 addition & 0 deletions tests/specs/compile/include_folder/data/b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b
8 changes: 8 additions & 0 deletions tests/specs/compile/include_folder/main.js
Original file line number Diff line number Diff line change
@@ -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());
}
2 changes: 2 additions & 0 deletions tests/specs/compile/include_folder/output.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a
b

0 comments on commit 6b478cd

Please sign in to comment.