Skip to content

Commit 5aef05d

Browse files
committed
chore: update
1 parent dfb1734 commit 5aef05d

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
@@ -1129,6 +1129,8 @@ export interface RawAssetGeneratorOptions {
11291129
outputPath?: JsFilename
11301130
publicPath?: "auto" | JsFilename
11311131
dataUrl?: RawAssetGeneratorDataUrlOptions | ((source: Buffer, context: RawAssetGeneratorDataUrlFnCtx) => string)
1132+
experimentalLibReExport?: boolean
1133+
experimentalLibPreserveImport?: boolean
11321134
}
11331135

11341136
export interface RawAssetInlineGeneratorOptions {
@@ -1153,6 +1155,8 @@ export interface RawAssetResourceGeneratorOptions {
11531155
filename?: JsFilename
11541156
outputPath?: JsFilename
11551157
publicPath?: "auto" | JsFilename
1158+
experimentalLibPreserveImport?: boolean
1159+
experimentalLibReExport?: boolean
11561160
}
11571161

11581162
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
@@ -504,6 +504,9 @@ pub struct RawAssetGeneratorOptions {
504504
ts_type = "RawAssetGeneratorDataUrlOptions | ((source: Buffer, context: RawAssetGeneratorDataUrlFnCtx) => string)"
505505
)]
506506
pub data_url: Option<RawAssetGeneratorDataUrl>,
507+
508+
pub experimental_lib_re_export: Option<bool>,
509+
pub experimental_lib_preserve_import: Option<bool>,
507510
}
508511

509512
impl From<RawAssetGeneratorOptions> for AssetGeneratorOptions {
@@ -516,6 +519,8 @@ impl From<RawAssetGeneratorOptions> for AssetGeneratorOptions {
516519
data_url: value
517520
.data_url
518521
.map(|i| RawAssetGeneratorDataUrlWrapper(i).into()),
522+
experimental_lib_re_export: value.experimental_lib_re_export,
523+
experimental_lib_preserve_import: value.experimental_lib_preserve_import,
519524
}
520525
}
521526
}
@@ -548,6 +553,9 @@ pub struct RawAssetResourceGeneratorOptions {
548553
pub output_path: Option<JsFilename>,
549554
#[napi(ts_type = "\"auto\" | JsFilename")]
550555
pub public_path: Option<JsFilename>,
556+
557+
pub experimental_lib_preserve_import: Option<bool>,
558+
pub experimental_lib_re_export: Option<bool>,
551559
}
552560

553561
impl From<RawAssetResourceGeneratorOptions> for AssetResourceGeneratorOptions {
@@ -557,6 +565,8 @@ impl From<RawAssetResourceGeneratorOptions> for AssetResourceGeneratorOptions {
557565
filename: value.filename.map(|i| i.into()),
558566
output_path: value.output_path.map(|i| i.into()),
559567
public_path: value.public_path.map(|i| i.into()),
568+
experimental_lib_preserve_import: value.experimental_lib_preserve_import,
569+
experimental_lib_re_export: value.experimental_lib_re_export,
560570
}
561571
}
562572
}

crates/rspack_core/src/options/module.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,8 @@ pub struct AssetResourceGeneratorOptions {
383383
pub filename: Option<Filename>,
384384
pub output_path: Option<Filename>,
385385
pub public_path: Option<PublicPath>,
386+
pub experimental_lib_re_export: Option<bool>,
387+
pub experimental_lib_preserve_import: Option<bool>,
386388
}
387389

388390
#[cacheable]
@@ -393,6 +395,8 @@ pub struct AssetGeneratorOptions {
393395
pub output_path: Option<Filename>,
394396
pub public_path: Option<PublicPath>,
395397
pub data_url: Option<AssetGeneratorDataUrl>,
398+
pub experimental_lib_re_export: Option<bool>,
399+
pub experimental_lib_preserve_import: Option<bool>,
396400
}
397401

398402
pub struct AssetGeneratorDataUrlFnCtx<'a> {

crates/rspack_plugin_asset/src/lib.rs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,13 +426,13 @@ impl ParserAndGenerator for AssetParserAndGenerator {
426426
.as_normal_module()
427427
.expect("module should be a NormalModule in AssetParserAndGenerator");
428428
let module_generator_options = normal_module.get_generator_options();
429+
dbg!(module_generator_options);
429430

430431
let result = match generate_context.requested_source_type {
431432
SourceType::JavaScript => {
432433
let exported_content = if parsed_asset_config.is_inline() {
433434
let resource_data: &ResourceData = normal_module.resource_resolved_data();
434435
let data_url = module_generator_options.and_then(|x| x.asset_data_url());
435-
436436
let encoded_source: String;
437437

438438
if let Some(custom_data_url) =
@@ -522,6 +522,61 @@ impl ParserAndGenerator for AssetParserAndGenerator {
522522
} else {
523523
unreachable!()
524524
};
525+
526+
let experimental_lib_preserve_import = module_generator_options
527+
.and_then(|x| x.get_asset())
528+
.and_then(|x| x.experimental_lib_preserve_import)
529+
.or_else(|| {
530+
module_generator_options
531+
.and_then(|x| x.get_asset_resource())
532+
.and_then(|x| x.experimental_lib_preserve_import)
533+
})
534+
.unwrap_or(false);
535+
let experimental_lib_re_export = module_generator_options
536+
.and_then(|x| x.get_asset())
537+
.and_then(|x| x.experimental_lib_re_export)
538+
.or_else(|| {
539+
module_generator_options
540+
.and_then(|x| x.get_asset_resource())
541+
.and_then(|x| x.experimental_lib_re_export)
542+
})
543+
.unwrap_or(false);
544+
545+
dbg!(experimental_lib_preserve_import, experimental_lib_re_export);
546+
547+
if experimental_lib_preserve_import || experimental_lib_re_export {
548+
let Some(PublicPath::Auto) = module_generator_options.and_then(|x| x.asset_public_path())
549+
else {
550+
return Err(error!(
551+
"`experimentalLibPreserveImport` and `experimentalLibReExport` can only be used with `asset/resource` and `publicPath: 'auto'`"
552+
));
553+
};
554+
555+
if let Some(ref mut scope) = generate_context.concatenation_scope {
556+
scope.register_namespace_export(NAMESPACE_OBJECT_EXPORT);
557+
return Ok(if experimental_lib_re_export {
558+
RawStringSource::from(format!(
559+
r#"import {NAMESPACE_OBJECT_EXPORT} from {exported_content};
560+
export default {NAMESPACE_OBJECT_EXPORT};"#
561+
))
562+
.boxed()
563+
} else {
564+
RawStringSource::from(format!(
565+
r#"import {NAMESPACE_OBJECT_EXPORT} from {exported_content};"#
566+
))
567+
.boxed()
568+
});
569+
} else {
570+
generate_context
571+
.runtime_requirements
572+
.insert(RuntimeGlobals::MODULE);
573+
return Ok(
574+
RawStringSource::from(format!(r#"module.exports = require({exported_content})"#))
575+
.boxed(),
576+
);
577+
}
578+
}
579+
525580
if let Some(ref mut scope) = generate_context.concatenation_scope {
526581
scope.register_namespace_export(NAMESPACE_OBJECT_EXPORT);
527582
let supports_const = compilation.options.output.environment.supports_const();
@@ -646,6 +701,7 @@ async fn render_manifest(
646701
.get::<CodeGenerationDataAssetInfo>()
647702
.expect("should have asset_info")
648703
.inner();
704+
dbg!(&asset_filename, &asset_info);
649705
RenderManifestEntry {
650706
source: source.clone(),
651707
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
@@ -203,6 +203,8 @@ export type AssetResourceGeneratorOptions = {
203203
filename?: Filename;
204204
outputPath?: AssetModuleOutputPath;
205205
publicPath?: PublicPath;
206+
experimentalLibReExport?: boolean;
207+
experimentalLibPreserveImport?: boolean;
206208
};
207209

208210
// @public (undocumented)

packages/rspack/src/config/adapter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,9 @@ function getRawAssetResourceGeneratorOptions(
816816
emit: options.emit,
817817
filename: options.filename,
818818
outputPath: options.outputPath,
819-
publicPath: options.publicPath
819+
publicPath: options.publicPath,
820+
experimentalLibReExport: options.experimentalLibReExport,
821+
experimentalLibPreserveImport: options.experimentalLibPreserveImport
820822
};
821823
}
822824

packages/rspack/src/config/types.ts

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

11481148
/** This option determines the URL prefix of the referenced 'asset' or 'asset/resource'*/
11491149
publicPath?: PublicPath;
1150+
1151+
/** */
1152+
experimentalLibReExport?: boolean;
1153+
/** */
1154+
experimentalLibPreserveImport?: boolean;
11501155
};
11511156

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

0 commit comments

Comments
 (0)