Repository navigation
bundler: require a bundled .node file from the chunk that holds it
#47483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,8 @@ use bun_js_parser::js_lexer; | |
|
|
||
| use crate::bun_css::properties::css_modules::Specifier as CssSpecifier; | ||
| use crate::bun_css::{BundlerStyleSheet, CssRef, CssRefTag}; | ||
| use crate::{Index, IndexInt, LinkerContext}; | ||
| use crate::options::Loader; | ||
| use crate::{Index, IndexInt, LinkerContext, WrapKind}; | ||
| use bun_collections::DynamicBitSetUnmanaged as BitSet; | ||
|
|
||
| type SymbolList<'a> = bun_ast::symbol::List<'a>; | ||
|
|
@@ -421,9 +422,28 @@ pub(crate) fn generate_code_for_lazy_export( | |
|
|
||
| // `require(<asset>)` prints as the runtime's `__require` outside CommonJS | ||
| // output, so the part that holds the call must import it. | ||
| let calls_runtime_require = matches!(expr.data, ExprData::ECall(ref c) | ||
| let mut calls_runtime_require = matches!(expr.data, ExprData::ECall(ref c) | ||
| if matches!(c.target.data, ExprData::ERequireCallTarget)) | ||
| && this.options.output_format != crate::options::OutputFormat::Cjs; | ||
| if calls_runtime_require | ||
| && requires_asset_from_import_meta(this, source_index) | ||
| && let ExprData::ECall(mut call) = expr.data | ||
| { | ||
| let loc = call.target.loc; | ||
| call.target = Expr::init( | ||
| E::Dot { | ||
| target: Expr::init(E::ImportMeta {}, loc), | ||
| name: b"require".as_slice().into(), | ||
| name_loc: loc, | ||
| ..Default::default() | ||
| }, | ||
| loc, | ||
| ); | ||
|
Comment on lines
+433
to
+441
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Users who build a split Bun-target bundle with Why this was flaggedA user runs Verification: Defines on
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is true, and it is deliberate. The description states it under Downsides (10 of the 208 checked builds) and under "The rule, and what it decides" in the notes. Main already prints a literal So on main, a build that replaces The example in the comment does not run on main. An earlier revision of this branch kept main's call when a |
||
| // A module without a wrapper keeps its use of `__require`. It is that module's one use of the runtime | ||
| // module, and the chunk of the runtime module and the order of the chunks follow that use (#44495). | ||
| calls_runtime_require = | ||
| this.graph.meta.items_flags()[source_index as usize].wrap == WrapKind::None; | ||
| } | ||
|
|
||
| match exports_kind { | ||
| bun_ast::ExportsKind::Cjs => { | ||
|
|
@@ -561,3 +581,13 @@ pub(crate) fn generate_code_for_lazy_export( | |
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Whether the `require(<asset>)` of the bundled `.node` file `source_index` (`require_embedded_asset`) calls | ||
| /// `import.meta.require`, as a split `require()` does for Bun. The linker prints the path of the file for the | ||
| /// chunk that holds the call, and the runtime's `__require` resolves from the chunk of the runtime module. | ||
| /// For Node.js, see `LinkerContext::makes_require_for_asset`. | ||
| fn requires_asset_from_import_meta(this: &LinkerContext, source_index: IndexInt) -> bool { | ||
| this.graph.code_splitting | ||
| && this.parse_graph().input_files.items_loader()[source_index as usize] == Loader::Napi | ||
| && this.graph.ast.items_target()[source_index as usize].is_bun() | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.