Skip to content
Closed
Show file tree
Hide file tree
Changes from 7 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 src/bundler/ParseTask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2557,6 +2557,8 @@ pub mod parse_worker {
opts.ignore_dce_annotations =
topts.ignore_dce_annotations && !task.source_index.is_runtime();

opts.target = target;

// For files that are not user-specified entrypoints, set `import.meta.main` to `false`.
// Entrypoints will have `import.meta.main` set as "unknown", unless we use `--compile`,
// in which we inline `true`.
Expand Down
1 change: 1 addition & 0 deletions src/bundler/transpiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,7 @@ impl<'a> Transpiler<'a> {
transform_only: self.options.transform_only,
import_meta_main_value: None,
lower_import_meta_main_for_node_js: false,
target,
framework: None,
repl_mode: self.options.repl_mode,
};
Expand Down
33 changes: 32 additions & 1 deletion src/js_parser/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,11 +509,42 @@
});
}

// Inline import.meta properties for Bake
// CJS output has no syntactic `import.meta`; Bake wants
// per-source-file paths in either format. Both need the
// `import.meta` property access replaced here.
Comment thread
robobun marked this conversation as resolved.
Resolved
if p.options.framework.is_some()
|| (p.options.bundle
&& p.options.output_format == js_parser::options::Format::Cjs)
{
// For `--format=cjs --target=bun|node` (and not Bake),
// `__dirname`/`__filename` fall through to the CJS
// wrapper at runtime. Keep `import.meta.dir`/`.path`
// consistent by rewriting them to those wrapper params
// instead of inlining the build-time path. (#4216)
Comment thread
robobun marked this conversation as resolved.
Resolved
if p.options.framework.is_none()
&& (p.options.target.is_bun() || p.options.target.is_node())
{
if name == b"dir" || name == b"dirname" {
p.record_usage(p.dirname_ref);
return Some(p.new_expr(
E::Identifier {
ref_: p.dirname_ref,
..Default::default()
},
name_loc,
));
} else if name == b"path" || name == b"filename" {
p.record_usage(p.filename_ref);
return Some(p.new_expr(
E::Identifier {
ref_: p.filename_ref,
..Default::default()
},
name_loc,
));
}

Check warning on line 545 in src/js_parser/fold.rs

View check run for this annotation

Claude / Claude Code Review

import.meta.url still inlines build-time path in --format=cjs --target=bun|node

The new inner branch rewrites `import.meta.dir`/`.dirname`/`.path`/`.filename` to `__dirname`/`__filename` for `--format=cjs --target=bun|node`, but `import.meta.url` and `import.meta.file` still fall through to the pre-existing string-literal inlining just below and emit the build machine's absolute path — the same #4216 leak this PR is fixing, in the same block. The fix is more involved (`require('node:url').pathToFileURL(__filename).href` / `require('node:path').basename(__filename)` rather t
Comment thread
robobun marked this conversation as resolved.
Resolved
}

if name == b"dir" || name == b"dirname" {
// Inline import.meta.dir
return Some(
Expand Down
Loading
Loading