Repository navigation
bundler: use the native import.meta.main in ESM output for Node.js #43687
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
32dd7d9
cf697c6
4329f57
654d2b2
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 |
|---|---|---|
|
|
@@ -6261,26 +6261,71 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool, const SEMA: bool> | |
| }), | ||
| }; | ||
| } | ||
| // Node.js does not have import.meta.main, so we end up lowering | ||
| // this to `require.main === module`, but with the ESM format, | ||
| // both `require` and `module` are not present, so the code | ||
| // generation we need is: | ||
| // | ||
| // import { createRequire } from "node:module"; | ||
| // var __require = createRequire(import.meta.url); | ||
| // var import_meta_main = __require.main === __require.module; | ||
| // | ||
| // The printer can handle this for us, but we need to reference | ||
| // a handle to the `__require` function. | ||
| if self.options.lower_import_meta_main_for_node_js { | ||
| self.record_usage_of_runtime_require(); | ||
| return self.lower_import_meta_main_for_node_js(inverted, loc); | ||
| } | ||
| Expr { | ||
| loc, | ||
| data: js_ast::ExprData::EImportMetaMain(E::ImportMetaMain { inverted }), | ||
| } | ||
| } | ||
|
|
||
| fn lower_import_meta_main_for_node_js(&mut self, inverted: bool, loc: bun_ast::Loc) -> Expr { | ||
| self.record_usage_of_runtime_require(); | ||
|
|
||
| if self.options.output_format != options::Format::Esm { | ||
| // The printer writes this as `require.main == module`. | ||
| return Expr { | ||
| loc, | ||
| data: js_ast::ExprData::EImportMetaMain(E::ImportMetaMain { inverted }), | ||
| }; | ||
| } | ||
|
|
||
| // `import.meta.main ?? __require.main == __require.module`: Node.js before v22.18.0 and v24.2.0 has no import.meta.main. | ||
| let require_module = self.new_expr( | ||
| E::Dot { | ||
| target: self.value_for_require(loc), | ||
| name: b"module".into(), | ||
|
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. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Replace the fallback's nonexistent On Node versions without native 🤖 Prompt for AI Agents |
||
| name_loc: loc, | ||
| ..Default::default() | ||
| }, | ||
| loc, | ||
| ); | ||
| let require_main_is_module = self.new_expr( | ||
| E::Binary { | ||
| op: js_ast::op::Code::BinLooseEq, | ||
| left: Expr { | ||
| loc, | ||
| data: js_ast::ExprData::ERequireMain, | ||
| }, | ||
| right: require_module, | ||
| }, | ||
| loc, | ||
| ); | ||
| let value = self.new_expr( | ||
| E::Binary { | ||
| op: js_ast::op::Code::BinNullishCoalescing, | ||
| left: Expr { | ||
| loc, | ||
| data: js_ast::ExprData::EImportMetaMain(E::ImportMetaMain { inverted: false }), | ||
| }, | ||
| right: require_main_is_module, | ||
| }, | ||
| loc, | ||
| ); | ||
| if !inverted { | ||
| return value; | ||
| } | ||
| self.new_expr( | ||
| E::Unary { | ||
| op: js_ast::op::Code::UnNot, | ||
| value, | ||
| flags: E::UnaryFlags::default(), | ||
| }, | ||
| loc, | ||
| ) | ||
| } | ||
|
|
||
| pub(crate) fn keep_expr_symbol_name(&mut self, _value: Expr, _name: &[u8]) -> Expr { | ||
| _value | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.