Skip to content

Commit

Permalink
chore(rust): Unite using filename instead of file_name (#1178)
Browse files Browse the repository at this point in the history
<!-- Thank you for contributing! -->

### Description

<!-- Please insert your description here and provide especially info about the "what" this PR is solving -->
  • Loading branch information
hyf0 committed May 20, 2024
1 parent 2eda7bc commit 9204e6d
Show file tree
Hide file tree
Showing 25 changed files with 66 additions and 69 deletions.
4 changes: 2 additions & 2 deletions crates/rolldown/src/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ impl Bundler {
.context(err)
})?;
for chunk in &output.assets {
let dest = dir.as_path().join(chunk.file_name());
let dest = dir.as_path().join(chunk.filename());
if let Some(p) = dest.parent() {
if !self.fs.exists(p) {
self.fs.create_dir_all(p).unwrap();
}
};
self.fs.write(dest.as_path(), chunk.content().as_bytes()).map_err(|err| {
anyhow::anyhow!("Failed to write file in {:?}", dir.as_path().join(chunk.file_name()))
anyhow::anyhow!("Failed to write file in {:?}", dir.as_path().join(chunk.filename()))
.context(err)
})?;
}
Expand Down
30 changes: 15 additions & 15 deletions crates/rolldown/src/stages/generate_stage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ impl<'a> GenerateStage<'a> {
let chunks = finalize_chunks(&mut chunk_graph, chunks);

let mut assets = vec![];
for ChunkRenderReturn { mut map, rendered_chunk, mut code, file_dir, preliminary_file_name } in
for ChunkRenderReturn { mut map, rendered_chunk, mut code, file_dir, preliminary_filename } in
chunks
{
if let Some(map) = map.as_mut() {
map.set_file(&rendered_chunk.file_name);
map.set_file(&rendered_chunk.filename);

let map_file_name = format!("{}.map", rendered_chunk.file_name.as_str());
let map_path = file_dir.join(&map_file_name);
let map_filename = format!("{}.map", rendered_chunk.filename.as_str());
let map_path = file_dir.join(&map_filename);

if let Some(source_map_ignore_list) = &self.options.sourcemap_ignore_list {
let mut x_google_ignore_list = vec![];
Expand Down Expand Up @@ -146,10 +146,10 @@ impl<'a> GenerateStage<'a> {
}
};
assets.push(Output::Asset(Box::new(OutputAsset {
file_name: map_file_name.clone(),
filename: map_filename.clone(),
source,
})));
code.push_str(&format!("\n//# sourceMappingURL={map_file_name}"));
code.push_str(&format!("\n//# sourceMappingURL={map_filename}"));
}
SourceMapType::Inline => {
let data_url = match map.to_data_url().map_err(BuildError::sourcemap_error) {
Expand All @@ -164,10 +164,10 @@ impl<'a> GenerateStage<'a> {
SourceMapType::Hidden => {}
}
}
let sourcemap_file_name =
map.as_ref().map(|_| format!("{}.map", rendered_chunk.file_name.as_str()));
let sourcemap_filename =
map.as_ref().map(|_| format!("{}.map", rendered_chunk.filename.as_str()));
assets.push(Output::Chunk(Box::new(OutputChunk {
file_name: rendered_chunk.file_name,
filename: rendered_chunk.filename,
code,
is_entry: rendered_chunk.is_entry,
is_dynamic_entry: rendered_chunk.is_dynamic_entry,
Expand All @@ -178,16 +178,16 @@ impl<'a> GenerateStage<'a> {
imports: rendered_chunk.imports,
dynamic_imports: rendered_chunk.dynamic_imports,
map,
sourcemap_file_name,
preliminary_file_name: preliminary_file_name.to_string(),
sourcemap_filename,
preliminary_filename: preliminary_filename.to_string(),
})));
}

// Make sure order of assets are deterministic
assets.sort_by_cached_key(|item| match item {
// TODO: use `preliminary_file_name` instead
Output::Asset(asset) => asset.file_name.to_string(),
Output::Chunk(chunk) => chunk.preliminary_file_name.to_string(),
// TODO: use `preliminary_filename` instead
Output::Asset(asset) => asset.filename.to_string(),
Output::Chunk(chunk) => chunk.preliminary_filename.to_string(),
});

Ok(BundleOutput {
Expand Down Expand Up @@ -260,7 +260,7 @@ impl<'a> GenerateStage<'a> {
}
let runtime_id = self.link_output.runtime.id();

let filename_template = chunk.file_name_template(self.options);
let filename_template = chunk.filename_template(self.options);

let mut chunk_name =
ensure_chunk_name(chunk, runtime_id, &self.link_output.module_table.normal_modules);
Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown/src/utils/chunk/finalize_chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub fn finalize_chunks(
replace_facade_hash_replacement(preliminary_filename_raw, &final_hashes_by_placeholder)
.into();
chunk.filename = Some(filename.clone());
chunk_render_return.rendered_chunk.file_name = filename;
chunk_render_return.rendered_chunk.filename = filename;
chunk_render_return.code = replace_facade_hash_replacement(
std::mem::take(&mut chunk_render_return.code),
&final_hashes_by_placeholder,
Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown/src/utils/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn generate_rendered_chunk(
facade_module_id: pre_rendered_chunk.facade_module_id,
module_ids: pre_rendered_chunk.module_ids,
exports: pre_rendered_chunk.exports,
file_name: chunk
filename: chunk
.preliminary_filename
.as_deref()
.expect("should have preliminary_filename")
Expand Down
4 changes: 2 additions & 2 deletions crates/rolldown/src/utils/chunk/render_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct ChunkRenderReturn {
pub map: Option<SourceMap>,
pub rendered_chunk: RenderedChunk,
pub file_dir: PathBuf,
pub preliminary_file_name: ResourceId,
pub preliminary_filename: ResourceId,
}

use super::{
Expand Down Expand Up @@ -155,7 +155,7 @@ pub async fn render_chunk(
map,
rendered_chunk,
file_dir: file_dir.to_path_buf(),
preliminary_file_name: this
preliminary_filename: this
.preliminary_filename
.as_deref()
.expect("should have preliminary filename")
Expand Down
9 changes: 3 additions & 6 deletions crates/rolldown/src/utils/normalize_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ pub fn normalize_options(mut raw_options: crate::BundlerOptions) -> NormalizeOpt
external: raw_options.external,
treeshake: raw_options.treeshake.unwrap_or(true),
platform: raw_options.platform.unwrap_or(Platform::Browser),
entry_file_names: raw_options
.entry_file_names
.unwrap_or_else(|| "[name].js".to_string())
.into(),
chunk_file_names: raw_options
.chunk_file_names
entry_filenames: raw_options.entry_filenames.unwrap_or_else(|| "[name].js".to_string()).into(),
chunk_filenames: raw_options
.chunk_filenames
.unwrap_or_else(|| "[name]-[hash].js".to_string())
.into(),
banner: raw_options.banner,
Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown/src/utils/render_chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub async fn render_chunks<'a>(
},
rendered_chunk: chunk.rendered_chunk,
file_dir: chunk.file_dir,
preliminary_file_name: chunk.preliminary_file_name,
preliminary_filename: chunk.preliminary_filename,
})
}))
.await
Expand Down
8 changes: 4 additions & 4 deletions crates/rolldown/tests/common/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ impl Case {
}

self.snapshot.push_str("# Assets\n\n");
assets.sort_by_key(|c| c.file_name().to_string());
assets.sort_by_key(|c| c.filename().to_string());
let artifacts = assets
.iter()
.filter(|asset| !asset.file_name().contains("$runtime$") && matches!(asset, Output::Chunk(_)))
.filter(|asset| !asset.filename().contains("$runtime$") && matches!(asset, Output::Chunk(_)))
.flat_map(|asset| {
[
Cow::Owned(format!("## {}\n", asset.file_name())),
Cow::Owned(format!("## {}\n", asset.filename())),
"```js".into(),
Cow::Borrowed(asset.content().trim()),
"```".into(),
Expand All @@ -102,7 +102,7 @@ impl Case {
Output::Chunk(chunk) => {
vec![Cow::Owned(format!(
"- {}, is_entry {}, is_dynamic_entry {}, exports {:?}",
chunk.file_name.as_str(),
chunk.filename.as_str(),
chunk.is_entry,
chunk.is_dynamic_entry,
chunk.exports
Expand Down
12 changes: 6 additions & 6 deletions crates/rolldown/tests/common/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,19 @@ impl Fixture {
_ => "mjs",
};

if bundle_options.entry_file_names.is_none() {
if bundle_options.entry_filenames.is_none() {
if with_hash {
bundle_options.entry_file_names = Some(format!("[name]-[hash].{output_ext}"));
bundle_options.entry_filenames = Some(format!("[name]-[hash].{output_ext}"));
} else {
bundle_options.entry_file_names = Some(format!("[name].{output_ext}"));
bundle_options.entry_filenames = Some(format!("[name].{output_ext}"));
}
}

if bundle_options.chunk_file_names.is_none() {
if bundle_options.chunk_filenames.is_none() {
if with_hash {
bundle_options.chunk_file_names = Some(format!("[name]-[hash].{output_ext}"));
bundle_options.chunk_filenames = Some(format!("[name]-[hash].{output_ext}"));
} else {
bundle_options.chunk_file_names = Some(format!("[name].{output_ext}"));
bundle_options.chunk_filenames = Some(format!("[name].{output_ext}"));
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/rolldown/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ async fn filename_with_hash() {

assets.assets.iter().for_each(|asset| match asset {
Output::Asset(asset) => {
snapshot_output.push_str(&format!("- {}\n", asset.file_name));
snapshot_output.push_str(&format!("- {}\n", asset.filename));
}
Output::Chunk(chunk) => {
snapshot_output.push_str(&format!(
"- {} => {}\n",
chunk.preliminary_file_name.as_str(),
chunk.file_name.as_str()
chunk.preliminary_filename.as_str(),
chunk.filename.as_str()
));
}
});
Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown_binding/src/types/binding_output_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl BindingOutputAsset {

#[napi(getter)]
pub fn file_name(&self) -> String {
self.inner.file_name.clone()
self.inner.filename.clone()
}

#[napi(getter)]
Expand Down
6 changes: 3 additions & 3 deletions crates/rolldown_binding/src/types/binding_output_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl BindingOutputChunk {
// RenderedChunk
#[napi(getter)]
pub fn file_name(&self) -> String {
self.inner.file_name.to_string()
self.inner.filename.to_string()
}

#[napi(getter)]
Expand Down Expand Up @@ -105,11 +105,11 @@ impl BindingOutputChunk {

#[napi(getter)]
pub fn sourcemap_file_name(&self) -> Option<String> {
self.inner.sourcemap_file_name.clone()
self.inner.sourcemap_filename.clone()
}

#[napi(getter)]
pub fn preliminary_file_name(&self) -> String {
self.inner.preliminary_file_name.to_string()
self.inner.preliminary_filename.to_string()
}
}
2 changes: 1 addition & 1 deletion crates/rolldown_binding/src/types/binding_outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl BindingOutputs {

#[napi]
pub fn delete(&mut self, file_name: String) {
if let Some(index) = self.inner.iter().position(|o| o.file_name() == file_name) {
if let Some(index) = self.inner.iter().position(|o| o.filename() == file_name) {
self.inner.remove(index);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl From<rolldown_common::RenderedChunk> for RenderedChunk {
facade_module_id: value.facade_module_id.map(|x| x.to_string()),
module_ids: value.module_ids.into_iter().map(|x| x.to_string()).collect(),
exports: value.exports,
file_name: value.file_name.to_string(),
file_name: value.filename.to_string(),
modules: value
.modules
.into_iter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ pub fn normalize_binding_options(
.transpose()
.map_err(|err| napi::Error::new(napi::Status::GenericFailure, err))?,
shim_missing_exports: input_options.shim_missing_exports,
entry_file_names: output_options.entry_file_names,
chunk_file_names: output_options.chunk_file_names,
entry_filenames: output_options.entry_file_names,
chunk_filenames: output_options.chunk_file_names,
dir: output_options.dir,
sourcemap: output_options.sourcemap.map(Into::into),
banner: normalize_addon_option(output_options.banner),
Expand Down
6 changes: 3 additions & 3 deletions crates/rolldown_common/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ impl Chunk {
Self { modules, name, bits, kind, ..Self::default() }
}

pub fn file_name_template<'a>(
pub fn filename_template<'a>(
&mut self,
options: &'a NormalizedBundlerOptions,
) -> &'a FilenameTemplate {
if matches!(self.kind, ChunkKind::EntryPoint { is_user_defined, .. } if is_user_defined) {
&options.entry_file_names
&options.entry_filenames
} else {
&options.chunk_file_names
&options.chunk_filenames
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/rolldown_common/src/inner_bundler_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ pub struct BundlerOptions {
pub platform: Option<Platform>,
pub shim_missing_exports: Option<bool>,
// --- options for output
pub entry_file_names: Option<String>,
pub chunk_file_names: Option<String>,
pub entry_filenames: Option<String>,
pub chunk_filenames: Option<String>,
pub dir: Option<String>,
pub format: Option<OutputFormat>,
pub sourcemap: Option<SourceMapType>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod file_name_template;
pub mod filename_template;
pub mod input_item;
pub mod is_external;
pub mod normalized_bundler_options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::path::PathBuf;

use super::{
file_name_template::FilenameTemplate, input_item::InputItem, is_external::IsExternal,
filename_template::FilenameTemplate, input_item::InputItem, is_external::IsExternal,
output_format::OutputFormat, output_option::AddonOutputOption, platform::Platform,
source_map_type::SourceMapType, sourcemap_ignore_list::SourceMapIgnoreList,
sourcemap_path_transform::SourceMapPathTransform,
Expand All @@ -20,8 +20,8 @@ pub struct NormalizedBundlerOptions {
pub platform: Platform,
pub shim_missing_exports: bool,
// --- Output
pub entry_file_names: FilenameTemplate,
pub chunk_file_names: FilenameTemplate,
pub entry_filenames: FilenameTemplate,
pub chunk_filenames: FilenameTemplate,
pub dir: String,
pub format: OutputFormat,
pub sourcemap: SourceMapType,
Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod types;
pub mod bundler_options {
pub use crate::inner_bundler_options::{
types::{
file_name_template::{FileNameRenderOptions, FilenameTemplate},
filename_template::{FileNameRenderOptions, FilenameTemplate},
input_item::InputItem,
is_external::IsExternal,
normalized_bundler_options::NormalizedBundlerOptions,
Expand Down
8 changes: 4 additions & 4 deletions crates/rolldown_common/src/types/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::OutputChunk;

#[derive(Debug)]
pub struct OutputAsset {
pub file_name: String,
pub filename: String,
pub source: String,
}

Expand All @@ -13,10 +13,10 @@ pub enum Output {
}

impl Output {
pub fn file_name(&self) -> &str {
pub fn filename(&self) -> &str {
match self {
Self::Chunk(chunk) => &chunk.file_name,
Self::Asset(asset) => &asset.file_name,
Self::Chunk(chunk) => &chunk.filename,
Self::Asset(asset) => &asset.filename,
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/rolldown_common/src/types/output_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ pub struct OutputChunk {
pub module_ids: Vec<ResourceId>,
pub exports: Vec<String>,
// RenderedChunk
pub file_name: ResourceId,
pub filename: ResourceId,
pub modules: FxHashMap<ResourceId, RenderedModule>,
pub imports: Vec<ResourceId>,
pub dynamic_imports: Vec<ResourceId>,
// OutputChunk
pub code: String,
pub map: Option<SourceMap>,
pub sourcemap_file_name: Option<String>,
pub preliminary_file_name: String,
pub sourcemap_filename: Option<String>,
pub preliminary_filename: String,
}
2 changes: 1 addition & 1 deletion crates/rolldown_common/src/types/rendered_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct RenderedChunk {
pub module_ids: Vec<ResourceId>,
pub exports: Vec<String>,
// RenderedChunk
pub file_name: ResourceId,
pub filename: ResourceId,
pub modules: FxHashMap<ResourceId, RenderedModule>,
pub imports: Vec<ResourceId>,
pub dynamic_imports: Vec<ResourceId>,
Expand Down

0 comments on commit 9204e6d

Please sign in to comment.