Open Mutefish0 opened 2 months ago
We now support dynamic imports with template arguments:
import("./a/${test}/.mod.ts")
This will include all files matching ./a/**/mod.ts.
./a/**/mod.ts
Many libraries that use Deno FFI require multiple files compiled for specific platforms. For example:
Library entry:
https://jsr.io/@scope/mylib/0.0.1/mod.ts
const lib = await import(`./ffi/${Deno.build.target}/ffi.ts`);
The files compiled for multiple platforms:
https://jsr.io/@scope/mylib/0.0.1/ffi/aarch64-apple-darwin/ffi.ts
https://jsr.io/@scope/mylib/0.0.1/ffi/x86_64-unknown-linux-gnu/ffi.ts
https://jsr.io/@scope/mylib/0.0.1/ffi/x86_64-pc-windows-msvc/ffi.ts
In our project:
// main.ts import xxx from "jsr:@scope/mylib" ...
Then use deno compile:
deno compile
deno compile --unstable-ffi --include ... main.ts
Currently, we have to manually --include all the files regardless of the actual platform it will run on:
deno compile --unstable-ffi --include https://jsr.io/@scope/mylib/0.0.1/ffi/aarch64-apple-darwin/ffi.ts --include https://jsr.io/@scope/mylib/0.0.1/ffi/x86_64-unknown-linux-gnu/ffi.ts main.ts
jsr:@scope/mylib
--include
Another option is to statically import all the FFI files, as shown here:
it will be more complicated to maintain the library, and also will incrase the compiled bundle size.
Analyze the expressions in the template argument in specific cases if they are known at compile time:
import(`./${Deno.build.target}/mod.ts`)
We can replace ${Deno.build.target} with correct target
${Deno.build.target}
target
import("./aarch64-apple-darwin/mod.ts")
which then become statically analyzable.
I have created a patch and some tests for deno_graph which rely on this PR.
deno_graph
run_test( " await import(`./${Deno.build.target}.ts`); ", vec![ ("file:///dev/x86_64-unknown-linux-gnu.ts", ""), ("file:///dev/aarch64-unknown-linux-gnu.ts", ""), ("file:///dev/x86_64-pc-windows-msvc.ts", ""), ("file:///dev/x86_64-apple-darwin.ts", ""), ("file:///dev/aarch64-apple-darwin.ts", ""), ("https://dev/aarch64-apple-darwin.ts", ""), ], if cfg!(all( target_arch = "x86_64", target_os = "macos", target_vendor = "apple" )) { vec!["file:///dev/x86_64-apple-darwin.ts"] } else if cfg!(all( target_arch = "aarch64", target_os = "macos", target_vendor = "apple" )) { vec!["file:///dev/aarch64-apple-darwin.ts"] } else if cfg!(all( target_arch = "x86_64", target_os = "linux", target_env = "gnu" )) { vec!["file:///dev/x86_64-unknown-linux-gnu.ts"] } else if cfg!(all( target_arch = "aarch64", target_os = "linux", target_env = "gnu" )) { vec!["file:///dev/aarch64-unknown-linux-gnu.ts"] } else if cfg!(all( target_arch = "x86_64", target_vendor = "pc", target_os = "windows", target_env = "msvc" )) { vec!["file:///dev/x86_64-pc-windows-msvc.ts"] } else { vec![] }, ) .await; run_test( " await import(`https://dev/${Deno.build.target}.ts`); ", vec![ ("https://dev/x86_64-unknown-linux-gnu.ts", ""), ("https://dev/aarch64-unknown-linux-gnu.ts", ""), ("https://dev/x86_64-pc-windows-msvc.ts", ""), ("https://dev/x86_64-apple-darwin.ts", ""), ("https://dev/aarch64-apple-darwin.ts", ""), ], if cfg!(all( target_arch = "x86_64", target_os = "macos", target_vendor = "apple" )) { vec!["https://dev/x86_64-apple-darwin.ts"] } else if cfg!(all( target_arch = "aarch64", target_os = "macos", target_vendor = "apple" )) { vec!["https://dev/aarch64-apple-darwin.ts"] } else if cfg!(all( target_arch = "x86_64", target_os = "linux", target_env = "gnu" )) { vec!["https://dev/x86_64-unknown-linux-gnu.ts"] } else if cfg!(all( target_arch = "aarch64", target_os = "linux", target_env = "gnu" )) { vec!["https://dev/aarch64-unknown-linux-gnu.ts"] } else if cfg!(all( target_arch = "x86_64", target_vendor = "pc", target_os = "windows", target_env = "msvc" )) { vec!["https://dev/x86_64-pc-windows-msvc.ts"] } else { vec![] }, ) .await;
https://github.com/denoland/deno/issues/24871
Works fine with my local patch:
DENORT_BIN=./target/debug/denort ./target/debug/deno compile --unstable-ffi -o ./main main.ts
All committers have signed the CLA.
We now support dynamic imports with template arguments:
This will include all files matching
./a/**/mod.ts
.The problem:
Many libraries that use Deno FFI require multiple files compiled for specific platforms. For example:
Library entry:
https://jsr.io/@scope/mylib/0.0.1/mod.ts
The files compiled for multiple platforms:
https://jsr.io/@scope/mylib/0.0.1/ffi/aarch64-apple-darwin/ffi.ts
https://jsr.io/@scope/mylib/0.0.1/ffi/x86_64-unknown-linux-gnu/ffi.ts
https://jsr.io/@scope/mylib/0.0.1/ffi/x86_64-pc-windows-msvc/ffi.ts
...In our project:
Then use
deno compile
:Currently, we have to manually --include all the files regardless of the actual platform it will run on:
jsr:@scope/mylib
, as we have to carefully--include
the correct version:Another option is to statically import all the FFI files, as shown here:
it will be more complicated to maintain the library, and also will incrase the compiled bundle size.
Proposal
Analyze the expressions in the template argument in specific cases if they are known at compile time:
We can replace
${Deno.build.target}
with correcttarget
which then become statically analyzable.
Proof of concept
I have created a patch and some tests for
deno_graph
which rely on this PR.Related Issues
https://github.com/denoland/deno/issues/24871
Works fine with my local patch: