Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 1 addition & 1 deletion src/bundler/HTMLImportManifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub(crate) fn write<W: Write + ?Sized>(

writer.write_all(b"{")?;

let inject_compiler_filesystem_prefix = options.compile;
let inject_compiler_filesystem_prefix = options.compile_mode.is_executable();
// Use the server-side public path here.
let public_path: &[u8] = &options.public_path;
let mut temp_buffer: Vec<u8> = Vec::new();
Expand Down
8 changes: 3 additions & 5 deletions src/bundler/LinkerContext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use bun_ast::SideEffects;
use bun_resolver::Resolver;

use crate::Graph::Graph;
use crate::options::{Format, Loader, SourceMapOption, Target};
use crate::options::{CompileMode, Format, Loader, SourceMapOption, Target};
use crate::{
AdditionalFile, BundleV2, Chunk, CompileResultForSourceMap, ContentHasher, ImportTracker,
LinkerGraph, MangledProps, PartRange, StableRef, WrapKind,
Expand Down Expand Up @@ -1239,10 +1239,9 @@ pub struct LinkerOptions {
pub(crate) banner: &'static [u8],
pub(crate) footer: &'static [u8],
pub(crate) css_chunking: bool,
pub(crate) compile_to_standalone_html: bool,
pub(crate) source_maps: SourceMapOption,
pub(crate) target: Target,
pub(crate) compile: bool,
pub(crate) compile_mode: CompileMode,
pub(crate) metafile: bool,
/// Path to write JSON metafile (for Bun.build API)
pub(crate) metafile_json_path: &'static [u8],
Expand All @@ -1268,10 +1267,9 @@ impl Default for LinkerOptions {
banner: b"",
footer: b"",
css_chunking: false,
compile_to_standalone_html: false,
source_maps: SourceMapOption::None,
target: Target::Browser,
compile: false,
compile_mode: CompileMode::None,
metafile: false,
metafile_json_path: b"",
metafile_markdown_path: b"",
Expand Down
6 changes: 3 additions & 3 deletions src/bundler/ParseTask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ pub mod parse_worker {
source,
Some(b"text/plain"),
None,
topts.compile_to_standalone_html,
topts.compile_mode.is_standalone_html(),
);
return Ok(ast);
}
Expand Down Expand Up @@ -899,7 +899,7 @@ pub mod parse_worker {
source,
Some(b"text/html"),
None,
topts.compile_to_standalone_html,
topts.compile_mode.is_standalone_html(),
);
return Ok(ast);
}
Expand Down Expand Up @@ -1318,7 +1318,7 @@ pub mod parse_worker {
source,
None,
Some(unique_key),
topts.compile_to_standalone_html,
topts.compile_mode.is_standalone_html(),
);
return Ok(ast);
}
Expand Down
11 changes: 6 additions & 5 deletions src/bundler/bundle_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,7 @@ pub mod bv2_impl {
unsafe { bun_ptr::detach_lifetime_ref::<bun_alloc::Arena>(self.arena()) };

let this_transpiler: &Transpiler<'a> = &*self.transpiler;
let this_compile = this_transpiler.options.compile;
let this_compile = this_transpiler.options.compile_mode.is_executable();
let this_env = this_transpiler.env;

// SAFETY: `self.transpiler` (and the data its `&'a` fields borrow)
Expand Down Expand Up @@ -2803,8 +2803,6 @@ pub mod bv2_impl {
// SAFETY: same `'a`-owned `Transpiler` field as `banner` above.
this.linker.options.footer = unsafe { interned_slice(&this.transpiler.options.footer) };
this.linker.options.css_chunking = this.transpiler.options.css_chunking;
this.linker.options.compile_to_standalone_html =
this.transpiler.options.compile_to_standalone_html;
this.linker.options.source_maps = this.transpiler.options.source_map;
this.linker.options.tree_shaking = this.transpiler.options.tree_shaking;
// SAFETY: same `'a`-owned `Transpiler` field as `banner` above.
Expand All @@ -2813,7 +2811,7 @@ pub mod bv2_impl {
this.linker.options.target = this.transpiler.options.target;
this.linker.options.output_format = this.transpiler.options.output_format;
this.linker.options.generate_bytecode_cache = this.transpiler.options.bytecode;
this.linker.options.compile = this.transpiler.options.compile;
this.linker.options.compile_mode = this.transpiler.options.compile_mode;
this.linker.options.metafile = this.transpiler.options.metafile;
// SAFETY: same `'a`-owned `Transpiler` field as `banner` above.
this.linker.options.metafile_json_path =
Expand Down Expand Up @@ -4130,7 +4128,10 @@ pub mod bv2_impl {
}
let mut v = Vec::new();
template
.print(&mut v, !self.transpiler.options.compile)
.print(
&mut v,
!self.transpiler.options.compile_mode.is_executable(),
)
.expect("oom");
v.into_boxed_slice()
};
Expand Down
4 changes: 2 additions & 2 deletions src/bundler/linker_context/OutputFileListBuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,14 @@ impl OutputFileList {
// module_info is generated for ESM bytecode in --compile builds
let module_info_count: usize = if c.options.generate_bytecode_cache
&& c.options.output_format == Format::Esm
&& c.options.compile
&& c.options.compile_mode.is_executable()
{
bytecode_count
} else {
0
};

let additional_output_files_count: usize = if c.options.compile_to_standalone_html {
let additional_output_files_count: usize = if c.options.compile_mode.is_standalone_html() {
0
} else {
parse_graph.additional_output_files.len()
Expand Down
12 changes: 6 additions & 6 deletions src/bundler/linker_context/generateChunksInParallel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ pub(crate) fn generate_chunks_in_parallel<const IS_DEV_SERVER: bool>(
// runtime bunfs references to out-of-root entrypoints resolve.
chunk
.template
.print(&mut rel_path, !c.options.compile)
.print(&mut rel_path, !c.options.compile_mode.is_executable())
.expect("write to Vec<u8>");
path::resolve_path::platform_to_posix_in_place::<u8>(&mut rel_path);

Expand Down Expand Up @@ -451,7 +451,7 @@ pub(crate) fn generate_chunks_in_parallel<const IS_DEV_SERVER: bool>(
// those placeholders with the resolved paths and serialize.
if c.options.generate_bytecode_cache
&& c.options.output_format == options::Format::Esm
&& c.options.compile
&& c.options.compile_mode.is_executable()
{
// Build map from unique_key -> final resolved path
// SAFETY: c points to LinkerContext which is the `linker` field of BundleV2.
Expand Down Expand Up @@ -551,7 +551,7 @@ pub(crate) fn generate_chunks_in_parallel<const IS_DEV_SERVER: bool>(
// disjoint from anything `c` mutates.
let resolver = c.resolver.expect("resolver set in load()");
let root_path: &[u8] = &resolver.opts.output_dir;
let is_standalone = c.options.compile_to_standalone_html;
let is_standalone = c.options.compile_mode.is_standalone_html();
let more_than_one_output = !is_standalone
&& (c.parse_graph().additional_output_files.len() > 0
|| c.options.generate_bytecode_cache
Expand Down Expand Up @@ -734,7 +734,7 @@ pub(crate) fn generate_chunks_in_parallel<const IS_DEV_SERVER: bool>(
}

// Don't write to disk if compile mode is enabled - we need buffer values for compilation
let is_compile = bundler.transpiler.options.compile;
let is_compile = bundler.transpiler.options.compile_mode.is_executable();
if root_path.len() > 0 && !is_compile {
write_output_files_to_disk(
c,
Expand Down Expand Up @@ -1010,7 +1010,7 @@ pub(crate) fn generate_chunks_in_parallel<const IS_DEV_SERVER: bool>(
// from server builds, and normalize with cheapPrefixNormalizer for consistency
// with module_info path fixup.
// For non-compile builds, use the normal .jsc extension.
let source_provider_url = if c.options.compile {
let source_provider_url = if c.options.compile_mode.is_executable() {
let normalizer =
cheap_prefix_normalizer(public_path, &chunk.final_rel_path);
BunString::create_format(format_args!(
Expand Down Expand Up @@ -1101,7 +1101,7 @@ pub(crate) fn generate_chunks_in_parallel<const IS_DEV_SERVER: bool>(
let module_info_output_file: Option<options::OutputFile> = 'brk: {
if c.options.generate_bytecode_cache
&& c.options.output_format == options::Format::Esm
&& c.options.compile
&& c.options.compile_mode.is_executable()
{
let loader: Loader = if chunk.entry_point.is_entry_point() {
c.parse_graph().input_files.items_loader()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ fn generate_compile_result_for_html_chunk_impl<'a>(
// pointer today.
let log: *mut Log = c.log;
let minify_whitespace = c.options.minify_whitespace;
let compile_to_standalone_html = c.options.compile_to_standalone_html;
let compile_to_standalone_html = c.options.compile_mode.is_standalone_html();
let has_dev_server = c.dev_server.is_some();
let contents: &[u8] = &sources[source_index as usize].contents;
let records = import_records[source_index as usize].as_slice();
Expand Down
2 changes: 1 addition & 1 deletion src/bundler/linker_context/postProcessJSChunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub(crate) fn post_process_js_chunk(
// Create ModuleInfo for ESM bytecode in --compile builds
let generate_module_info = c.options.generate_bytecode_cache
&& c.options.output_format == options::OutputFormat::Esm
&& c.options.compile;
&& c.options.compile_mode.is_executable();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
let loader =
c.parse_graph().input_files.items_loader()[chunk.entry_point.source_index() as usize];
let is_typescript = loader.is_type_script();
Expand Down
39 changes: 27 additions & 12 deletions src/bundler/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub use defines::Define;
// traits into scope so the associated-fn call syntax below resolves.
use crate::defines::{DefineDataExt as _, DefineExt as _};
pub use bun_options_types::global_cache::GlobalCache;
pub use bun_options_types::offline_mode::OfflineMode;

// Canonical alias lives in the resolver.
pub use bun_resolver::package_json::ConditionsMap;
Expand Down Expand Up @@ -1116,6 +1117,26 @@ bun_core::comptime_string_map! {
};
}

/// What `--compile` resolved to for this bundle.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CompileMode {
#[default]
None,
Executable,
StandaloneHtml,
}

impl CompileMode {
#[inline]
pub const fn is_executable(self) -> bool {
matches!(self, CompileMode::Executable)
}
#[inline]
pub const fn is_standalone_html(self) -> bool {
matches!(self, CompileMode::StandaloneHtml)
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PackagesOption {
Bundle,
Expand Down Expand Up @@ -1242,8 +1263,7 @@ pub struct BundleOptions<'a> {
pub disable_transpilation: bool,

pub global_cache: GlobalCache,
pub prefer_offline_install: bool,
pub prefer_latest_install: bool,
pub install_preference: OfflineMode,
/// Stored as a raw
/// `NonNull` (not `Option<&'a _>`) because every CLI caller borrows the
/// process-lifetime `ctx.install: Box<BunInstall>` whose lifetime is
Expand Down Expand Up @@ -1273,8 +1293,7 @@ pub struct BundleOptions<'a> {
pub code_coverage: bool,
pub debugger: bool,

pub compile: bool,
pub compile_to_standalone_html: bool,
pub compile_mode: CompileMode,
pub metafile: bool,
/// Path to write JSON metafile (for Bun.build API)
pub metafile_json_path: Box<[u8]>,
Expand Down Expand Up @@ -1446,8 +1465,7 @@ impl<'a> BundleOptions<'a> {
packages: self.packages,
disable_transpilation: self.disable_transpilation,
global_cache: self.global_cache,
prefer_offline_install: self.prefer_offline_install,
prefer_latest_install: self.prefer_latest_install,
install_preference: self.install_preference,
install: self.install,
inlining: self.inlining,
inline_entrypoint_import_meta_main: self.inline_entrypoint_import_meta_main,
Expand All @@ -1463,8 +1481,7 @@ impl<'a> BundleOptions<'a> {
bytecode: self.bytecode,
code_coverage: self.code_coverage,
debugger: self.debugger,
compile: self.compile,
compile_to_standalone_html: self.compile_to_standalone_html,
compile_mode: self.compile_mode,
metafile: self.metafile,
metafile_json_path: self.metafile_json_path.clone(),
metafile_markdown_path: self.metafile_markdown_path.clone(),
Expand Down Expand Up @@ -1700,8 +1717,7 @@ impl<'a> BundleOptions<'a> {
packages: PackagesOption::Bundle,
disable_transpilation: false,
global_cache: GlobalCache::disable,
prefer_offline_install: false,
prefer_latest_install: false,
install_preference: OfflineMode::Online,
install: None,
inlining: false,
inline_entrypoint_import_meta_main: false,
Expand All @@ -1716,8 +1732,7 @@ impl<'a> BundleOptions<'a> {
bytecode: false,
code_coverage: false,
debugger: false,
compile: false,
compile_to_standalone_html: false,
compile_mode: CompileMode::None,
metafile: false,
metafile_json_path: Box::default(),
metafile_markdown_path: Box::default(),
Expand Down
4 changes: 2 additions & 2 deletions src/bundler/transpiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,7 @@ fn resolver_bundle_options_subset(
main_fields_is_default: src.transform_options.main_fields.is_empty(),
mark_builtins_as_external: src.mark_builtins_as_external,
polyfill_node_globals: src.polyfill_node_globals,
prefer_offline_install: src.prefer_offline_install,
install_preference: src.install_preference,
preserve_symlinks: src.preserve_symlinks,
rewrite_jest_for_tests: src.rewrite_jest_for_tests,
tsconfig_override: src.tsconfig_override.clone(),
Expand All @@ -1128,7 +1128,7 @@ fn resolver_bundle_options_subset(
output_dir: src.output_dir.clone(),
root_dir: src.root_dir.clone(),
public_path: src.public_path.clone(),
compile: src.compile,
compile: src.compile_mode.is_executable(),
supports_multiple_outputs: src.supports_multiple_outputs,
tree_shaking: src.tree_shaking,
allow_runtime: src.allow_runtime,
Expand Down
3 changes: 2 additions & 1 deletion src/options_types/offline_mode.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[repr(u8)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
pub enum OfflineMode {
#[default]
Online,
Latest,
Offline,
Expand Down
4 changes: 2 additions & 2 deletions src/resolver/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ pub struct BundleOptions {
pub main_fields_is_default: bool,
pub mark_builtins_as_external: bool,
pub polyfill_node_globals: bool,
pub prefer_offline_install: bool,
pub install_preference: bun_options_types::offline_mode::OfflineMode,
pub preserve_symlinks: bool,
pub rewrite_jest_for_tests: bool,
pub tsconfig_override: Option<Box<[u8]>>,
Expand Down Expand Up @@ -273,7 +273,7 @@ impl Default for BundleOptions {
main_fields_is_default: true,
mark_builtins_as_external: false,
polyfill_node_globals: false,
prefer_offline_install: false,
install_preference: Default::default(),
preserve_symlinks: false,
rewrite_jest_for_tests: false,
tsconfig_override: None,
Expand Down
2 changes: 1 addition & 1 deletion src/resolver/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3592,7 +3592,7 @@ impl<'a> Resolver<'a> {
}
}

if self.opts.prefer_offline_install {
if self.opts.install_preference == bun_options_types::offline_mode::OfflineMode::Offline {
if let Some(package_id) = pm!().resolve_from_disk_cache(esm.name, &version) {
*input_package_id_ = package_id;
return DependencyToResolve::Resolution(
Expand Down
14 changes: 9 additions & 5 deletions src/runtime/api/js_bundle_completion_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,11 @@ impl CompletionStruct for JSBundleCompletionTask {

transpiler.options.output_format = config.format;
transpiler.options.bytecode = config.bytecode;
transpiler.options.compile = config.compile.is_some();
transpiler.options.compile_mode = if config.compile.is_some() {
options::CompileMode::Executable
} else {
options::CompileMode::None
};

// For compile mode, set the public_path to the target-specific base path
// This ensures embedded resources like yoga.wasm are correctly found
Expand Down Expand Up @@ -919,7 +923,7 @@ impl CompletionStruct for JSBundleCompletionTask {
transpiler.options.ignore_dce_annotations = config.ignore_dce_annotations;
transpiler.options.tree_shaking_override = config.tree_shaking;
transpiler.options.css_chunking = config.css_chunking;
transpiler.options.compile_to_standalone_html = 'brk: {
let compile_to_standalone_html = 'brk: {
if config.compile.is_none() || config.target != bun_ast::Target::Browser {
break 'brk false;
}
Expand All @@ -932,8 +936,8 @@ impl CompletionStruct for JSBundleCompletionTask {
config.entry_points.count() > 0
};
// When compiling to standalone HTML, don't use the bun executable compile path
if transpiler.options.compile_to_standalone_html {
transpiler.options.compile = false;
if compile_to_standalone_html {
transpiler.options.compile_mode = options::CompileMode::StandaloneHtml;
config.compile = None;
}
// `BundleOptions.{banner,footer}` are `Cow<'static, [u8]>`; clone into
Expand Down Expand Up @@ -966,7 +970,7 @@ impl CompletionStruct for JSBundleCompletionTask {
Some(unsafe { &*core::ptr::from_ref(&config.optimize_imports) });
}

if transpiler.options.compile {
if transpiler.options.compile_mode.is_executable() {
// Emitting DCE annotations is nonsensical in --compile.
transpiler.options.emit_dce_annotations = false;
}
Expand Down
Loading
Loading