Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
203 changes: 139 additions & 64 deletions src/js_parser/parse/parse_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ pub struct Options<'a> {
pub import_meta_main_value: Option<bool>,
pub lower_import_meta_main_for_node_js: bool,

/// The bundle target. Controls whether `__dirname` / `__filename` are
/// inlined as build-time string literals (browser) or lowered to
/// `import.meta.dir` / `import.meta.path` so they resolve at runtime
/// (Bun / Node ESM output). Only consulted when `bundle` is set.
Comment thread
robobun marked this conversation as resolved.
Resolved
pub target: js_ast::Target,

/// When using react fast refresh or server components, the framework is
/// able to customize what import sources are used.
pub framework: Option<&'a options::Framework>, // TYPE_ONLY: was bun_runtime::bake::Framework
Expand Down Expand Up @@ -132,6 +138,7 @@ impl<'a> Default for Options<'a> {
transform_only: false,
import_meta_main_value: None,
lower_import_meta_main_for_node_js: false,
target: js_ast::Target::Browser,
framework: None,
repl_mode: false,
}
Expand Down Expand Up @@ -215,6 +222,7 @@ impl<'a> Options<'a> {
transform_only: self.transform_only,
import_meta_main_value: self.import_meta_main_value,
lower_import_meta_main_for_node_js: self.lower_import_meta_main_for_node_js,
target: self.target,
framework: self.framework,
repl_mode: self.repl_mode,
}
Expand Down Expand Up @@ -282,6 +290,7 @@ impl<'a> Options<'a> {
transform_only: false,
import_meta_main_value: None,
lower_import_meta_main_for_node_js: false,
target: js_ast::Target::Browser,
framework: None,
repl_mode: false,
};
Expand Down Expand Up @@ -1013,79 +1022,145 @@ impl<'a> Parser<'a> {
// var __dirname = "foo/bar"
// var __filename = "foo/bar/baz.js"
//
// When bundling for Bun or Node, the build machine's absolute source
// path is the wrong value: the bundle will typically run from a
// different location (and on a different machine). For ESM output we
// instead emit `var __dirname = import.meta.dir` (Bun) or
// `import.meta.dirname` (Node), so it resolves to the output file's
// directory at runtime. For CJS output on those targets the runtime
// already provides `__dirname`/`__filename` via the module wrapper, so
// we emit nothing and let references fall through.
//
// https://github.com/oven-sh/bun/issues/4216
Comment thread
robobun marked this conversation as resolved.
Resolved
if p.options.bundle || !p.options.features.commonjs_at_runtime {
if uses_dirname || uses_filename {
let count = (uses_dirname as usize) + (uses_filename as usize);
let mut declared_symbols =
bun_ast::DeclaredSymbolList::init_capacity(count).expect("unreachable");
let decls = p
.arena
.alloc_slice_fill_with::<G::Decl, _>(count, |_| G::Decl::default());
if uses_dirname {
decls[0] = G::Decl {
binding: p.b(
B::Identifier {
r#ref: p.dirname_ref,
},
bun_ast::Loc::EMPTY,
),
value: Some(p.new_expr(
E::String {
data: p.source.path.name().dir.into(),
..Default::default()
},
bun_ast::Loc::EMPTY,
)),
// `(dir_property, path_property)` — `Some` when the target
// runtime exposes these on `import.meta`, `None` to fall back
// to the build-time string literal.
let import_meta_names: Option<(&'static [u8], &'static [u8])> =
if p.options.bundle && p.options.output_format == options::Format::Esm {
if p.options.target.is_bun() {
Some((b"dir", b"path"))
} else if p.options.target.is_node() {
Some((b"dirname", b"filename"))
} else {
None
}
} else {
None
};
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
declared_symbols.append_assume_capacity(DeclaredSymbol {
ref_: p.dirname_ref,
is_top_level: true,
});

let defer_to_cjs_wrapper = p.options.bundle
&& p.options.output_format == options::Format::Cjs
&& (p.options.target.is_bun() || p.options.target.is_node());

if import_meta_names.is_some() {
p.has_import_meta = true;
}
if uses_filename {
decls[uses_dirname as usize] = G::Decl {
binding: p.b(
B::Identifier {
r#ref: p.filename_ref,
},
bun_ast::Loc::EMPTY,
),
value: Some(p.new_expr(
E::String {
data: p.source.path.text.into(),

if !defer_to_cjs_wrapper {
let count = (uses_dirname as usize) + (uses_filename as usize);
let mut declared_symbols =
bun_ast::DeclaredSymbolList::init_capacity(count).expect("unreachable");
let decls = p
.arena
.alloc_slice_fill_with::<G::Decl, _>(count, |_| G::Decl::default());
if uses_dirname {
let value = if let Some((dir_name, _)) = import_meta_names {
let import_meta = p.new_expr(E::ImportMeta {}, bun_ast::Loc::EMPTY);
p.new_expr(
E::Dot {
name: dir_name.into(),
name_loc: bun_ast::Loc::EMPTY,
target: import_meta,
can_be_removed_if_unused: true,
..Default::default()
},
bun_ast::Loc::EMPTY,
)
} else {
p.new_expr(
E::String {
data: p.source.path.name().dir.into(),
..Default::default()
},
bun_ast::Loc::EMPTY,
)
};
decls[0] = G::Decl {
binding: p.b(
B::Identifier {
r#ref: p.dirname_ref,
},
bun_ast::Loc::EMPTY,
),
value: Some(value),
};
declared_symbols.append_assume_capacity(DeclaredSymbol {
ref_: p.dirname_ref,
is_top_level: true,
});
}
if uses_filename {
let value = if let Some((_, path_name)) = import_meta_names {
let import_meta = p.new_expr(E::ImportMeta {}, bun_ast::Loc::EMPTY);
p.new_expr(
E::Dot {
name: path_name.into(),
name_loc: bun_ast::Loc::EMPTY,
target: import_meta,
can_be_removed_if_unused: true,
..Default::default()
},
bun_ast::Loc::EMPTY,
)
} else {
p.new_expr(
E::String {
data: p.source.path.text.into(),
..Default::default()
},
bun_ast::Loc::EMPTY,
)
};
decls[uses_dirname as usize] = G::Decl {
binding: p.b(
B::Identifier {
r#ref: p.filename_ref,
},
bun_ast::Loc::EMPTY,
),
value: Some(value),
};
declared_symbols.append_assume_capacity(DeclaredSymbol {
ref_: p.filename_ref,
is_top_level: true,
});
}

let part_stmts = p.arena.alloc_slice_fill_with(1, |_| {
p.s(
S::Local {
kind: js_ast::LocalKind::KVar,
decls: {
let mut dl = G::DeclList::init_capacity(decls.len());
for d in decls.iter_mut() {
dl.append_assume_capacity(core::mem::take(d));
}
dl
},
..Default::default()
},
bun_ast::Loc::EMPTY,
)),
};
declared_symbols.append_assume_capacity(DeclaredSymbol {
ref_: p.filename_ref,
is_top_level: true,
)
});
before.push(js_ast::Part {
stmts: part_stmts.into(),
declared_symbols,
tag: bun_ast::PartTag::DirnameFilename,
..Default::default()
});
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

let part_stmts = p.arena.alloc_slice_fill_with(1, |_| {
p.s(
S::Local {
kind: js_ast::LocalKind::KVar,
decls: {
let mut dl = G::DeclList::init_capacity(decls.len());
for d in decls.iter_mut() {
dl.append_assume_capacity(core::mem::take(d));
}
dl
},
..Default::default()
},
bun_ast::Loc::EMPTY,
)
});
before.push(js_ast::Part {
stmts: part_stmts.into(),
declared_symbols,
tag: bun_ast::PartTag::DirnameFilename,
..Default::default()
});
uses_dirname = false;
uses_filename = false;
}
Expand Down
22 changes: 20 additions & 2 deletions test/bundler/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ console.log(utils());`,
});
const baseDir = baseDirPath + "";

// `--compile` bundles for Bun's runtime, so `__dirname`/`__filename`
// resolve to the compiled executable's virtual `$bunfs` path at runtime
// instead of the build machine's source path (#4216).
const { exited } = Bun.spawn({
cmd: [
bunExe(),
Expand All @@ -211,8 +214,23 @@ console.log(utils());`,
const text = await proc.stdout.text();
await proc.exited;

expect(text).toContain(path.join(baseDir, "我") + "\n");
expect(text).toContain(path.join(baseDir, "我", "我.ts") + "\n");
const bunfsRoot = isWindows ? "B:\\~BUN\\root" : "/$bunfs/root";
expect(text).toBe(bunfsRoot + "\n" + path.join(bunfsRoot, "exe.exe") + "\n");

// The browser target has no runtime source for `__dirname`, so it still
// inlines the source path as a string literal. Exercise the UTF-8 path
// round-trip this test originally covered there.
await using browserBuild = Bun.spawn({
cmd: [bunExe(), "build", path.join(baseDir, "我/我.ts"), "--target=browser"],
env: bunEnv,
cwd: baseDir,
stdout: "pipe",
stderr: "pipe",
});
const [browserOut, browserExit] = await Promise.all([browserBuild.stdout.text(), browserBuild.exited]);
expect(browserOut).toContain(JSON.stringify(path.join(baseDir, "我")));
expect(browserOut).toContain(JSON.stringify(path.join(baseDir, "我", "我.ts")));
expect(browserExit).toBe(0);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
});

test.skipIf(!isWindows)("should be able to handle pretty path when using pnpm + #14685", async () => {
Expand Down
Loading
Loading