Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions changelog.d/9632-bun-project-node-addons.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
### Bun compatibility

- **Root/project Node-API addons loaded through `import.meta.require` now ship
and run after a Bun extraction tree is removed.** Declare each file by its
exact project-relative path, for example
`"perry": { "nativeAddonPaths": ["native/addon.node"] }`. Perry follows
immutable aliases and simple path constants, including
`new URL("./native/addon.node", import.meta.url).pathname`, and maps relative,
absolute, and `/$bunfs/root/` spellings to the same authenticated sidecar
entry. Dynamic or otherwise unprovable paths fail compilation with guidance
instead of producing a binary that depends on the build machine's source
tree.
4 changes: 3 additions & 1 deletion crates/perry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ path = "src/main.rs"

[dependencies]
perry-parser.workspace = true
swc_common.workspace = true
swc_ecma_ast.workspace = true
swc_ecma_transforms_base.workspace = true
swc_ecma_visit.workspace = true
perry-hir.workspace = true
perry-transform.workspace = true
perry-codegen.workspace = true
Expand Down Expand Up @@ -172,7 +175,6 @@ all-codegen-backends = [

[dev-dependencies]
tempfile.workspace = true
swc_common.workspace = true

[build-dependencies]
winresource = "0.1.31"
9 changes: 9 additions & 0 deletions crates/perry/src/commands/compile/collect_modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ mod dynamic_glob;
mod eval_worker;
mod feature_detect;
mod import_helpers;
mod import_meta_require;
mod json_module;
mod native_addon;
mod parse_error;
Expand All @@ -56,6 +57,7 @@ use import_helpers::{
cached_resolve_import_with_lexical_base, collect_js_module_imports,
ensure_bunfs_import_resolves, env_defines_for_lowering,
};
use import_meta_require::rewrite_import_meta_require_addons;
use json_module::synthesize_json_module;
pub(super) use native_addon::package_has_unsupported_node_addon;
use native_addon::{collect_or_refuse_node_addon, refuse_compile_package_native_addon};
Expand Down Expand Up @@ -338,6 +340,13 @@ fn collect_module_one(
fs::read_to_string(&canonical)
.map_err(|e| anyhow!("Failed to read {}: {}", canonical.display(), e))?
};
// Bun exposes `import.meta.require`; unlike CommonJS `require`, aliases of
// that function and URL-derived addon paths are invisible to the ordinary
// static-require scan. Recover and rewrite exact Node-API loads before HIR
// lowering so runtime execution uses only the authenticated sidecar id.
// Run before the Bun virtual-literal asset scan so a `.node` call target
// ships once in the sidecar rather than also being embedded as inert data.
let raw_source = rewrite_import_meta_require_addons(&raw_source, &canonical, ctx)?;
register_bunfs_literal_assets(&raw_source, ctx);
// CJS wrapping consumes literal `require()` sites and replaces them with
// generated loader calls. Queue native targets before that rewrite so the
Expand Down
Loading
Loading