Skip to content

Commit f481b0a

Browse files
committed
chore: update
1 parent de9c5ef commit f481b0a

File tree

8 files changed

+109
-3
lines changed

8 files changed

+109
-3
lines changed

crates/node_binding/binding.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,8 @@ export interface RawAssetGeneratorOptions {
11281128
filename?: JsFilename
11291129
publicPath?: "auto" | JsFilename
11301130
dataUrl?: RawAssetGeneratorDataUrlOptions | ((arg: RawAssetGeneratorDataUrlFnArgs) => string)
1131+
experimentalLibReExport?: boolean
1132+
experimentalLibPreserveImport?: boolean
11311133
}
11321134

11331135
export interface RawAssetInlineGeneratorOptions {
@@ -1151,6 +1153,8 @@ export interface RawAssetResourceGeneratorOptions {
11511153
emit?: boolean
11521154
filename?: JsFilename
11531155
publicPath?: "auto" | JsFilename
1156+
experimentalLibPreserveImport?: boolean
1157+
experimentalLibReExport?: boolean
11541158
}
11551159

11561160
export interface RawBannerPluginOptions {

crates/rspack_binding_options/src/options/raw_module/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,9 @@ pub struct RawAssetGeneratorOptions {
503503
ts_type = "RawAssetGeneratorDataUrlOptions | ((arg: RawAssetGeneratorDataUrlFnArgs) => string)"
504504
)]
505505
pub data_url: Option<RawAssetGeneratorDataUrl>,
506+
507+
pub experimental_lib_re_export: Option<bool>,
508+
pub experimental_lib_preserve_import: Option<bool>,
506509
}
507510

508511
impl From<RawAssetGeneratorOptions> for AssetGeneratorOptions {
@@ -514,6 +517,8 @@ impl From<RawAssetGeneratorOptions> for AssetGeneratorOptions {
514517
data_url: value
515518
.data_url
516519
.map(|i| RawAssetGeneratorDataUrlWrapper(i).into()),
520+
experimental_lib_re_export: value.experimental_lib_re_export,
521+
experimental_lib_preserve_import: value.experimental_lib_preserve_import,
517522
}
518523
}
519524
}
@@ -545,6 +550,9 @@ pub struct RawAssetResourceGeneratorOptions {
545550
pub filename: Option<JsFilename>,
546551
#[napi(ts_type = "\"auto\" | JsFilename")]
547552
pub public_path: Option<JsFilename>,
553+
554+
pub experimental_lib_preserve_import: Option<bool>,
555+
pub experimental_lib_re_export: Option<bool>,
548556
}
549557

550558
impl From<RawAssetResourceGeneratorOptions> for AssetResourceGeneratorOptions {
@@ -553,6 +561,8 @@ impl From<RawAssetResourceGeneratorOptions> for AssetResourceGeneratorOptions {
553561
emit: value.emit,
554562
filename: value.filename.map(|i| i.into()),
555563
public_path: value.public_path.map(|i| i.into()),
564+
experimental_lib_preserve_import: value.experimental_lib_preserve_import,
565+
experimental_lib_re_export: value.experimental_lib_re_export,
556566
}
557567
}
558568
}

crates/rspack_core/src/options/module.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ pub struct AssetResourceGeneratorOptions {
371371
pub emit: Option<bool>,
372372
pub filename: Option<Filename>,
373373
pub public_path: Option<PublicPath>,
374+
pub experimental_lib_re_export: Option<bool>,
375+
pub experimental_lib_preserve_import: Option<bool>,
374376
}
375377

376378
#[cacheable]
@@ -380,6 +382,8 @@ pub struct AssetGeneratorOptions {
380382
pub filename: Option<Filename>,
381383
pub public_path: Option<PublicPath>,
382384
pub data_url: Option<AssetGeneratorDataUrl>,
385+
pub experimental_lib_re_export: Option<bool>,
386+
pub experimental_lib_preserve_import: Option<bool>,
383387
}
384388

385389
pub struct AssetGeneratorDataUrlFnArgs {

crates/rspack_plugin_asset/src/lib.rs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,13 +409,13 @@ impl ParserAndGenerator for AssetParserAndGenerator {
409409
.as_normal_module()
410410
.expect("module should be a NormalModule in AssetParserAndGenerator");
411411
let module_generator_options = normal_module.get_generator_options();
412+
dbg!(module_generator_options);
412413

413414
let result = match generate_context.requested_source_type {
414415
SourceType::JavaScript => {
415416
let exported_content = if parsed_asset_config.is_inline() {
416417
let resource_data: &ResourceData = normal_module.resource_resolved_data();
417418
let data_url = module_generator_options.and_then(|x| x.asset_data_url());
418-
419419
let encoded_source: String;
420420

421421
if let Some(custom_data_url) = self.get_data_url(resource_data, data_url, source) {
@@ -498,6 +498,61 @@ impl ParserAndGenerator for AssetParserAndGenerator {
498498
} else {
499499
unreachable!()
500500
};
501+
502+
let experimental_lib_preserve_import = module_generator_options
503+
.and_then(|x| x.get_asset())
504+
.and_then(|x| x.experimental_lib_preserve_import)
505+
.or_else(|| {
506+
module_generator_options
507+
.and_then(|x| x.get_asset_resource())
508+
.and_then(|x| x.experimental_lib_preserve_import)
509+
})
510+
.unwrap_or(false);
511+
let experimental_lib_re_export = module_generator_options
512+
.and_then(|x| x.get_asset())
513+
.and_then(|x| x.experimental_lib_re_export)
514+
.or_else(|| {
515+
module_generator_options
516+
.and_then(|x| x.get_asset_resource())
517+
.and_then(|x| x.experimental_lib_re_export)
518+
})
519+
.unwrap_or(false);
520+
521+
dbg!(experimental_lib_preserve_import, experimental_lib_re_export);
522+
523+
if experimental_lib_preserve_import || experimental_lib_re_export {
524+
let Some(PublicPath::Auto) = module_generator_options.and_then(|x| x.asset_public_path())
525+
else {
526+
return Err(error!(
527+
"`experimentalLibPreserveImport` and `experimentalLibReExport` can only be used with `asset/resource` and `publicPath: 'auto'`"
528+
));
529+
};
530+
531+
if let Some(ref mut scope) = generate_context.concatenation_scope {
532+
scope.register_namespace_export(NAMESPACE_OBJECT_EXPORT);
533+
return Ok(if experimental_lib_re_export {
534+
RawStringSource::from(format!(
535+
r#"import {NAMESPACE_OBJECT_EXPORT} from {exported_content};
536+
export default {NAMESPACE_OBJECT_EXPORT};"#
537+
))
538+
.boxed()
539+
} else {
540+
RawStringSource::from(format!(
541+
r#"import {NAMESPACE_OBJECT_EXPORT} from {exported_content};"#
542+
))
543+
.boxed()
544+
});
545+
} else {
546+
generate_context
547+
.runtime_requirements
548+
.insert(RuntimeGlobals::MODULE);
549+
return Ok(
550+
RawStringSource::from(format!(r#"module.exports = require({exported_content})"#))
551+
.boxed(),
552+
);
553+
}
554+
}
555+
501556
if let Some(ref mut scope) = generate_context.concatenation_scope {
502557
scope.register_namespace_export(NAMESPACE_OBJECT_EXPORT);
503558
let supports_const = compilation.options.output.environment.supports_const();
@@ -621,6 +676,7 @@ async fn render_manifest(
621676
.get::<CodeGenerationDataAssetInfo>()
622677
.expect("should have asset_info")
623678
.inner();
679+
dbg!(&asset_filename, &asset_info);
624680
RenderManifestEntry {
625681
source: source.clone(),
626682
filename: asset_filename.to_owned(),

crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,9 @@ impl ModuleConcatenationPlugin {
555555
let box_module = module_graph
556556
.module_by_identifier(&root_module_id)
557557
.expect("should have module");
558+
let root_module_source_types = box_module.source_types();
559+
let is_root_module_asset_module = root_module_source_types.contains(&SourceType::Asset);
560+
558561
let root_module_ctxt = RootModuleContext {
559562
id: root_module_id,
560563
readable_identifier: box_module
@@ -677,8 +680,28 @@ impl ModuleConcatenationPlugin {
677680
// .module_identifier_to_module
678681
// .remove(&root_module_id);
679682
// compilation.chunk_graph.clear
683+
if is_root_module_asset_module {
684+
chunk_graph.replace_module(&root_module_id, &new_module.id());
685+
chunk_graph.add_module(root_module_id);
686+
for chunk_ukey in chunk_graph.get_module_chunks(new_module.id()).clone() {
687+
let module = module_graph
688+
.module_by_identifier(&root_module_id)
689+
.expect("should exist module");
680690

681-
chunk_graph.replace_module(&root_module_id, &new_module.id());
691+
dbg!(&module, &new_module);
692+
693+
let source_types = chunk_graph.get_chunk_module_source_types(&chunk_ukey, module);
694+
let new_source_types = source_types
695+
.iter()
696+
.filter(|source_type| !matches!(source_type, SourceType::JavaScript))
697+
.copied()
698+
.collect();
699+
chunk_graph.set_chunk_modules_source_types(&chunk_ukey, root_module_id, new_source_types);
700+
chunk_graph.connect_chunk_and_module(chunk_ukey, root_module_id);
701+
}
702+
} else {
703+
chunk_graph.replace_module(&root_module_id, &new_module.id());
704+
}
682705

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

packages/rspack/etc/core.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ export type AssetResourceGeneratorOptions = {
199199
emit?: boolean;
200200
filename?: Filename;
201201
publicPath?: PublicPath;
202+
experimentalLibReExport?: boolean;
203+
experimentalLibPreserveImport?: boolean;
202204
};
203205

204206
// @public (undocumented)

packages/rspack/src/config/adapter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,9 @@ function getRawAssetResourceGeneratorOptions(
813813
return {
814814
emit: options.emit,
815815
filename: options.filename,
816-
publicPath: options.publicPath
816+
publicPath: options.publicPath,
817+
experimentalLibReExport: options.experimentalLibReExport,
818+
experimentalLibPreserveImport: options.experimentalLibPreserveImport
817819
};
818820
}
819821

packages/rspack/src/config/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,11 @@ export type AssetResourceGeneratorOptions = {
11381138

11391139
/** This option determines the URL prefix of the referenced 'asset' or 'asset/resource'*/
11401140
publicPath?: PublicPath;
1141+
1142+
/** */
1143+
experimentalLibReExport?: boolean;
1144+
/** */
1145+
experimentalLibPreserveImport?: boolean;
11411146
};
11421147

11431148
/** Generator options for asset modules. */

0 commit comments

Comments
 (0)