Skip to content

Commit 8648d12

Browse files
committed
refactor: support asset module using plugin
1 parent 3a64ec4 commit 8648d12

File tree

46 files changed

+613
-352
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+613
-352
lines changed

Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ rolldown_fs = { version = "0.1.0", path = "crates/rolldown_fs" }
108108
rolldown_fs_watcher = { version = "0.1.0", path = "crates/rolldown_fs_watcher" }
109109
rolldown_plugin = { version = "0.1.0", path = "crates/rolldown_plugin" }
110110
rolldown_plugin_chunk_import_map = { version = "0.1.0", path = "crates/rolldown_plugin_chunk_import_map" }
111+
rolldown_plugin_asset_module = { version = "0.1.0", path = "crates/rolldown_plugin_asset_module" }
111112
rolldown_plugin_copy_module = { version = "0.1.0", path = "crates/rolldown_plugin_copy_module" }
112113
rolldown_plugin_bundle_analyzer = { version = "0.1.0", path = "crates/rolldown_plugin_bundle_analyzer" }
113114
rolldown_plugin_data_url = { version = "0.1.0", path = "crates/rolldown_plugin_data_url" }

crates/rolldown/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ rolldown_error = { workspace = true }
5151
rolldown_fs = { workspace = true, features = ["os"] }
5252
rolldown_plugin = { workspace = true }
5353
rolldown_plugin_chunk_import_map = { workspace = true }
54+
rolldown_plugin_asset_module = { workspace = true }
5455
rolldown_plugin_copy_module = { workspace = true }
5556
rolldown_plugin_data_url = { workspace = true }
5657
rolldown_plugin_hmr = { workspace = true }

crates/rolldown/src/asset/asset_generator.rs

Lines changed: 0 additions & 42 deletions
This file was deleted.

crates/rolldown/src/asset/mod.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
use rolldown_common::{AssetView, StrOrBytes};
21

3-
pub mod asset_generator;
4-
5-
pub fn create_asset_view(source: StrOrBytes) -> AssetView {
6-
AssetView { source }
7-
}

crates/rolldown/src/module_finalizers/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -816,10 +816,11 @@ impl<'me, 'ast> ScopeHoistingFinalizer<'me, 'ast> {
816816
let importee =
817817
rec.resolved_module.and_then(|module_idx| self.ctx.modules[module_idx].as_normal())?;
818818

819-
let chunk_idx = &self.ctx.chunk_graph.module_to_chunk[importee.idx]?;
820-
let chunk = &self.ctx.chunk_graph.chunk_table[*chunk_idx];
821-
let asset_filename = &chunk.asset_absolute_preliminary_filenames[&importee.idx];
822-
let import_path = self.ctx.chunk.relative_path_for(asset_filename.as_path());
819+
// Look up the emitted asset filename via the FileEmitter bridge
820+
let ref_id = self.ctx.file_emitter.file_ref_for_module(&importee.id)?;
821+
let filename = self.ctx.file_emitter.get_file_name(&ref_id).ok()?;
822+
let abs_path = self.ctx.options.cwd.join(&self.ctx.options.out_dir).join(filename.as_str());
823+
let import_path = self.ctx.chunk.relative_path_for(abs_path.as_path());
823824

824825
first_arg_string_literal.value = self.snippet.atom(&import_path);
825826
None

crates/rolldown/src/module_loader/module_task.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use rolldown_std_utils::PathExt as _;
1616
use rolldown_utils::{ecmascript::legitimize_identifier_name, indexmap::FxIndexSet};
1717

1818
use crate::{
19-
asset::create_asset_view,
2019
ecmascript::ecma_module_view_factory::{CreateEcmaViewReturn, create_ecma_view},
2120
types::module_factory::{CreateModuleContext, CreateModuleViewArgs},
2221
utils::{load_source::load_source, transform_source::transform_source},
@@ -109,26 +108,17 @@ impl ModuleTask {
109108

110109
let mut sourcemap_chain = vec![];
111110
let mut hook_side_effects = self.resolved_id.side_effects.take();
112-
let (mut source, module_type) = self
111+
let (source, module_type) = self
113112
.load_source(&mut sourcemap_chain, &mut hook_side_effects, self.magic_string_tx.clone())
114113
.await?;
115114

116115
let stable_id = id.stabilize(&self.ctx.options.cwd);
117116

118-
let asset_view = match module_type {
119-
ModuleType::Asset => {
120-
let asset_view = create_asset_view(source);
121-
source = StrOrBytes::Str(String::new());
122-
Some(asset_view)
123-
}
124-
ModuleType::Css => {
125-
Err(anyhow::anyhow!(
126-
"Bundling CSS is no longer supported (experimental support has been removed). See https://github.com/rolldown/rolldown/issues/4271 for details."
127-
))?;
128-
unreachable!()
129-
}
130-
_ => None,
131-
};
117+
if matches!(module_type, ModuleType::Css) {
118+
Err(anyhow::anyhow!(
119+
"Bundling CSS is no longer supported (experimental support has been removed). See https://github.com/rolldown/rolldown/issues/4271 for details."
120+
))?;
121+
}
132122

133123
let mut warnings = vec![];
134124

@@ -204,7 +194,6 @@ impl ModuleTask {
204194
exec_order: u32::MAX,
205195
module_type: module_type.clone(),
206196
ecma_view,
207-
asset_view,
208197
originative_resolved_id: self.resolved_id.clone(),
209198
};
210199

crates/rolldown/src/module_loader/runtime_module_task.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ impl RuntimeModuleTask {
198198
import_attribute_map: FxHashMap::default(),
199199
json_module_none_self_reference_included_symbol: None,
200200
},
201-
asset_view: None,
202201
// TODO(hyf0/hmr): We might need to find a better way to handle this.
203202
originative_resolved_id: resolved_id,
204203
};

crates/rolldown/src/stages/generate_stage/mod.rs

Lines changed: 2 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
use std::{path::PathBuf, sync::Arc};
1+
use std::path::PathBuf;
22

33
use arcstr::ArcStr;
44
use futures::future::try_join_all;
55
use oxc_index::IndexVec;
66
use render_chunk_to_assets::set_emitted_chunk_preliminary_filenames;
7-
use rolldown_common::{
8-
ChunkIdx, ChunkKind, ImportMetaRolldownAssetReplacer, Module, OutputExports, PathsOutputOption,
9-
PreliminaryFilename, RollupPreRenderedAsset,
10-
};
7+
use rolldown_common::{ChunkIdx, ChunkKind, OutputExports, PathsOutputOption};
118
use rolldown_devtools::{action, trace_action, trace_action_enabled};
129
use rolldown_error::{BuildDiagnostic, BuildResult};
1310
use rolldown_plugin::SharedPluginDriver;
14-
use rolldown_std_utils::OptionExt as _;
1511
use rolldown_std_utils::{
1612
PathBufExt as _, PathExt as _, representative_file_name_for_preserve_modules,
1713
};
1814
use rolldown_utils::{
1915
dashmap::FxDashMap,
2016
hash_placeholder::HashPlaceholderGenerator,
21-
make_unique_name::make_unique_name,
2217
rayon::{IntoParallelRefMutIterator as _, ParallelIterator as _},
2318
};
2419
use rustc_hash::FxHashMap;
@@ -112,7 +107,6 @@ impl<'a> GenerateStage<'a> {
112107

113108
let index_chunk_id_to_name =
114109
self.generate_chunk_name_and_preliminary_filenames(&mut chunk_graph).await?;
115-
self.patch_asset_modules(&chunk_graph);
116110
set_emitted_chunk_preliminary_filenames(&self.plugin_driver.file_emitter, &chunk_graph);
117111

118112
debug_span!("deconflict_chunk_symbols").in_scope(|| {
@@ -309,53 +303,6 @@ impl<'a> GenerateStage<'a> {
309303
// if user provided one.
310304
chunk.name = Some(pre_generated_chunk_name.chunk_name.clone());
311305

312-
for module in chunk.modules.iter().copied().filter_map(|idx| modules[idx].as_normal()) {
313-
if let Some(asset_view) = module.asset_view.as_ref() {
314-
let name = self
315-
.options
316-
.sanitize_filename
317-
.call(module.id.as_path().file_stem().and_then(|s| s.to_str()).unpack())
318-
.await?;
319-
let asset_filename_template = self
320-
.options
321-
.asset_filename_template(&RollupPreRenderedAsset {
322-
names: vec![name.clone()],
323-
original_file_names: vec![],
324-
source: asset_view.source.clone(),
325-
})
326-
.await?;
327-
328-
let has_hash_pattern = asset_filename_template.has_hash_pattern();
329-
let extension = module.id.as_path().extension().and_then(|s| s.to_str());
330-
331-
let mut hash_placeholder = has_hash_pattern.then_some(vec![]);
332-
let hash_replacer = has_hash_pattern.then(|| {
333-
let pattern_name = asset_filename_template.pattern_name();
334-
|len: Option<usize>| {
335-
let hash = hash_placeholder_generator.generate(len, pattern_name)?;
336-
if let Some(hash_placeholder) = hash_placeholder.as_mut() {
337-
hash_placeholder.push(hash.clone());
338-
}
339-
Ok(hash)
340-
}
341-
});
342-
343-
let mut filename =
344-
asset_filename_template.render(Some(&name), None, extension, hash_replacer)?.into();
345-
filename = make_unique_name(&filename, &used_name_counts);
346-
let preliminary = PreliminaryFilename::new(filename, hash_placeholder);
347-
348-
chunk.asset_absolute_preliminary_filenames.insert(
349-
module.idx,
350-
preliminary
351-
.absolutize_with(self.options.cwd.join(&self.options.out_dir))
352-
.into_owned()
353-
.expect_into_string(),
354-
);
355-
chunk.asset_preliminary_filenames.insert(module.idx, preliminary);
356-
}
357-
}
358-
359306
chunk.pre_rendered_chunk = Some(pre_rendered_chunk);
360307

361308
chunk.absolute_preliminary_filename = Some(
@@ -369,22 +316,6 @@ impl<'a> GenerateStage<'a> {
369316
Ok(index_chunk_id_to_representative_name)
370317
}
371318

372-
pub fn patch_asset_modules(&mut self, chunk_graph: &ChunkGraph) {
373-
chunk_graph.chunk_table.iter().for_each(|chunk| {
374-
// replace asset name in ecma view
375-
chunk.asset_preliminary_filenames.iter().for_each(|(module_idx, preliminary)| {
376-
let Module::Normal(module) = &mut self.link_output.module_table[*module_idx] else {
377-
return;
378-
};
379-
let asset_filename: ArcStr = preliminary.as_str().into();
380-
module
381-
.ecma_view
382-
.mutations
383-
.push(Arc::new(ImportMetaRolldownAssetReplacer { asset_filename }));
384-
});
385-
});
386-
}
387-
388319
fn compute_chunk_output_exports(
389320
&self,
390321
chunk_graph: &mut ChunkGraph,

crates/rolldown/src/stages/generate_stage/render_chunk_to_assets.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use rolldown_utils::{
1717

1818
use crate::{
1919
BundleOutput,
20-
asset::asset_generator::AssetGenerator,
2120
chunk_graph::ChunkGraph,
2221
ecmascript::ecma_generator::EcmaGenerator,
2322
type_alias::{AssetVec, IndexChunkToInstances, IndexInstantiatedChunks},
@@ -196,23 +195,7 @@ impl GenerateStage<'_> {
196195
let ecma_chunks = ecma_chunks_future.await?;
197196
Ok(ecma_chunks)
198197
});
199-
let asset_chunks_future: ChunkGeneratorFuture = Box::pin(async move {
200-
let mut asset_ctx = GenerateContext {
201-
chunk_idx,
202-
chunk,
203-
options: self.options,
204-
link_output: self.link_output,
205-
chunk_graph,
206-
plugin_driver: self.plugin_driver,
207-
module_id_to_codegen_ret: vec![],
208-
render_export_items_index_vec: &index_vec![],
209-
resolved_paths,
210-
};
211-
let asset_chunks_future = AssetGenerator::instantiate_chunk(&mut asset_ctx);
212-
let asset_chunks = asset_chunks_future.await?;
213-
Ok(asset_chunks)
214-
});
215-
[ecma_chunks_future, asset_chunks_future]
198+
[ecma_chunks_future]
216199
}),
217200
)
218201
.await?

0 commit comments

Comments
 (0)