Repository navigation
bundler: spell import.meta.require as the require of node, CommonJS and dev-server output
#46299
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
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 |
|---|---|---|
|
|
@@ -612,6 +612,23 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O | |
| }); | ||
| } | ||
|
|
||
| // The printer spells this node as the `require` of the output. `--drop` of it needs the `E::Dot`. A revisit cannot record the use of the runtime's `__require`. | ||
| if name == b"require" | ||
|
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. 🟣 pre-existing, not blocking: Users who build Why this was flaggedTrigger: source containing Verification: With minify, src/js_parser/visit/visit_expr.rs:995-1016 builds an |
||
| && p.options.lower_import_meta_require | ||
| && !p.method_call_must_be_replaced_with_undefined | ||
|
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. 🟡 (optional) Node-target builds of code that reads an Why this was flaggedA production (non-HMR) build for node contains, e.g., Verification: src/js_parser/fold.rs:782 and :816 set |
||
| && !p.is_revisit_for_substitution | ||
|
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. 🟣 pre-existing, not blocking: Users bundling for node with Why this was flaggedTrigger: Verification: The new arm at src/js_parser/fold.rs:616-630 bails on |
||
| && identifier_opts.assign_target() == js_ast::AssignTarget::None | ||
| && !identifier_opts.is_delete_target() | ||
| { | ||
| if p.options.features.allow_runtime { | ||
| p.record_usage_of_runtime_require(); | ||
| } | ||
| return Some(Expr { | ||
| data: js_ast::ExprData::ESpecial(E::Special::ImportMetaRequire), | ||
| loc: target.loc, | ||
| }); | ||
| } | ||
|
Comment on lines
+616
to
+630
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 bundle for node code that detects Bun by testing Why this was flaggedA dependency or source file contains Verification: src/js_parser/fold.rs:616-630 replaces every non-assigned, non-deleted read of |
||
|
|
||
| // Inline import.meta properties for Bake | ||
| if p.options.framework.is_some() | ||
| || (p.options.bundle | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3420,6 +3420,16 @@ pub(crate) mod __gated_printer { | |
| true, | ||
| ); | ||
| } | ||
| E::Special::ImportMetaRequire => { | ||
| self.print_expr( | ||
| Expr { | ||
| data: ExprData::ERequireCallTarget, | ||
| loc: expr.loc, | ||
| }, | ||
| level, | ||
|
Comment on lines
3420
to
+3429
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. 🟡 (optional) Dev-server client code that stores Why this was flaggedA dev-server client module contains Verification: |
||
| in_flags, | ||
| ); | ||
| } | ||
| }, | ||
| ExprData::ECommonjsExportIdentifier(id) => { | ||
| self.print_commonjs_export_identifier(*id, expr.loc, false); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟣 pre-existing, not blocking: pre-existing: users of
bun build --format=cjswith no--targetstill get CommonJS output that containsimport.meta.require, which is a SyntaxError in every CommonJS loader (node, webpack). The gate at src/bundler/ParseTask.rs:2705 istarget.is_server_side(), so a browser-target CJS build is left unlowered. The stated reason for excluding the browser (a module script has norequire) only holds for ESM output; a CJS module always hasrequireand never hasimport.meta. Fix: lowerimport.meta.requiretorequirefor every CJS output regardless of target (Format::Cjs => true), keeping the HTML client files of a server build untouched, which already come through an ESM transpiler.A small fix can ride a push you are already making; otherwise a short reply is enough.
Why this was flagged
A library or a dependency it bundles contains
typeof Bun !== "undefined" ? import.meta.require("x") : null, or is a file thatbun build --target=bunwrote (var __require = import.meta.require;). The user runsbun build --format=cjs entry.jswith no--target, sotargetat src/bundler/ParseTask.rs:2479 isTarget::Browser. The match at src/bundler/ParseTask.rs:2705 giveslower_import_meta_require = false, so theE::Dotarm at src/js_parser/fold.rs:616 does not fire and the output keepsimport.meta.require. Loading that CommonJS file in node or any bundler throwsSyntaxError: Cannot use 'import.meta' outside a module. The base branch produces the same broken output, so this is pre-existing; the PR lowers the same spelling for--target=bunand--target=nodeCJS output at the same line, and the printer path at src/js_printer/lib.rs:3559 already prints the bare globalrequireforFormat::Cjs, so the same lowering would be correct for browser-target CJS too.Verification:
bun build --format=cjs entry.jswith no--targetdefaults toTarget::Browser. src/bundler/ParseTask.rs:2702-2710 setsoptions::Format::Cjs => target.is_server_side(), which is false forTarget::Browser, so the fold at src/js_parser/fold.rs:616-630 does not fire. Node's CommonJS loader rejectsimport.metawith a SyntaxError, so the CJS output fails to load at all.