Skip to content

Commit

Permalink
feat: export asset
Browse files Browse the repository at this point in the history
  • Loading branch information
SoonIter committed Dec 16, 2024
1 parent de9c5ef commit 8453ef5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
2 changes: 2 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,7 @@ export interface RawAssetGeneratorOptions {
filename?: JsFilename
publicPath?: "auto" | JsFilename
dataUrl?: RawAssetGeneratorDataUrlOptions | ((arg: RawAssetGeneratorDataUrlFnArgs) => string)
experimentalLibReExport?: boolean
}

export interface RawAssetInlineGeneratorOptions {
Expand All @@ -1151,6 +1152,7 @@ export interface RawAssetResourceGeneratorOptions {
emit?: boolean
filename?: JsFilename
publicPath?: "auto" | JsFilename
experimentalLibReExport?: boolean
}

export interface RawBannerPluginOptions {
Expand Down
6 changes: 6 additions & 0 deletions crates/rspack_binding_options/src/options/raw_module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,8 @@ pub struct RawAssetGeneratorOptions {
ts_type = "RawAssetGeneratorDataUrlOptions | ((arg: RawAssetGeneratorDataUrlFnArgs) => string)"
)]
pub data_url: Option<RawAssetGeneratorDataUrl>,

pub __experimental_lib_re_export: Option<bool>,
}

impl From<RawAssetGeneratorOptions> for AssetGeneratorOptions {
Expand All @@ -514,6 +516,7 @@ impl From<RawAssetGeneratorOptions> for AssetGeneratorOptions {
data_url: value
.data_url
.map(|i| RawAssetGeneratorDataUrlWrapper(i).into()),
__experimental_lib_re_export: value.__experimental_lib_re_export.map(|i| i.into()),
}
}
}
Expand Down Expand Up @@ -545,6 +548,8 @@ pub struct RawAssetResourceGeneratorOptions {
pub filename: Option<JsFilename>,
#[napi(ts_type = "\"auto\" | JsFilename")]
pub public_path: Option<JsFilename>,

pub __experimental_lib_re_export: Option<bool>,
}

impl From<RawAssetResourceGeneratorOptions> for AssetResourceGeneratorOptions {
Expand All @@ -553,6 +558,7 @@ impl From<RawAssetResourceGeneratorOptions> for AssetResourceGeneratorOptions {
emit: value.emit,
filename: value.filename.map(|i| i.into()),
public_path: value.public_path.map(|i| i.into()),
__experimental_lib_re_export: value.__experimental_lib_re_export.map(|i| i.into()),
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions crates/rspack_core/src/options/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,17 @@ impl GeneratorOptions {
.and_then(|x| x.emit)
.or_else(|| self.get_asset_resource().and_then(|x| x.emit))
}

pub fn asset_experimental_lib_re_export(&self) -> Option<bool> {
self
.get_asset()
.and_then(|x| x.__experimental_lib_re_export)
.or_else(|| {
self
.get_asset_resource()
.and_then(|x| x.__experimental_lib_re_export)
})
}
}

#[cacheable]
Expand All @@ -371,6 +382,7 @@ pub struct AssetResourceGeneratorOptions {
pub emit: Option<bool>,
pub filename: Option<Filename>,
pub public_path: Option<PublicPath>,
pub __experimental_lib_re_export: Option<bool>,
}

#[cacheable]
Expand All @@ -380,6 +392,7 @@ pub struct AssetGeneratorOptions {
pub filename: Option<Filename>,
pub public_path: Option<PublicPath>,
pub data_url: Option<AssetGeneratorDataUrl>,
pub __experimental_lib_re_export: Option<bool>,
}

pub struct AssetGeneratorDataUrlFnArgs {
Expand Down
7 changes: 7 additions & 0 deletions crates/rspack_plugin_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,12 @@ impl ParserAndGenerator for AssetParserAndGenerator {
&source_file_name,
)?;

dbg!(&module_generator_options);

Check failure on line 455 in crates/rspack_plugin_asset/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust check

the `dbg!` macro is intended as a debugging tool

let experimental_lib_re_export = module_generator_options

Check failure on line 457 in crates/rspack_plugin_asset/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust check

unused variable: `experimental_lib_re_export`
.and_then(|x| x.asset_experimental_lib_re_export())
.unwrap_or(false);

let asset_path = if let Some(public_path) =
module_generator_options.and_then(|x| x.asset_public_path())
{
Expand Down Expand Up @@ -621,6 +627,7 @@ async fn render_manifest(
.get::<CodeGenerationDataAssetInfo>()
.expect("should have asset_info")
.inner();
dbg!(&asset_filename, &asset_info);

Check failure on line 630 in crates/rspack_plugin_asset/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust check

the `dbg!` macro is intended as a debugging tool
RenderManifestEntry {
source: source.clone(),
filename: asset_filename.to_owned(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,9 @@ impl ModuleConcatenationPlugin {
let box_module = module_graph
.module_by_identifier(&root_module_id)
.expect("should have module");
let root_module_source_types = box_module.source_types().clone();

Check failure on line 558 in crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs

View workflow job for this annotation

GitHub Actions / Rust check

call to `.clone()` on a reference in this situation does nothing
let is_root_module_asset_module = root_module_source_types.contains(&SourceType::Asset);

let root_module_ctxt = RootModuleContext {
id: root_module_id,
readable_identifier: box_module
Expand Down Expand Up @@ -677,8 +680,26 @@ impl ModuleConcatenationPlugin {
// .module_identifier_to_module
// .remove(&root_module_id);
// compilation.chunk_graph.clear
if is_root_module_asset_module {
chunk_graph.replace_module(&root_module_id, &new_module.id());
chunk_graph.add_module(root_module_id);
for chunk_ukey in chunk_graph.get_module_chunks(new_module.id()).clone() {
let module = module_graph
.module_by_identifier(&root_module_id)
.expect("should exist module");

chunk_graph.replace_module(&root_module_id, &new_module.id());
let source_types = chunk_graph.get_chunk_module_source_types(&chunk_ukey, module);
let new_source_types = source_types
.iter()
.filter(|source_type| !matches!(source_type, SourceType::JavaScript))
.copied()
.collect();
chunk_graph.set_chunk_modules_source_types(&chunk_ukey, root_module_id, new_source_types);
chunk_graph.connect_chunk_and_module(chunk_ukey, root_module_id);
}
} else {
chunk_graph.replace_module(&root_module_id, &new_module.id());
}

module_graph.move_module_connections(&root_module_id, &new_module.id(), |c, dep| {
let other_module = if *c.module_identifier() == root_module_id {
Expand Down

0 comments on commit 8453ef5

Please sign in to comment.