feat: enhance dynamic import with template argument #266
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
import(`./${Deno.build.target}/mod.ts`)
We can replace
${Deno.build.target}
with correcttarget
import("./aarch64-apple-darwin/mod.ts")
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
denoland/deno#24871
Works fine with my local patch: