Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin-asset): support re-export and preserve-import in asset for Rslib #8724

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,8 @@ export interface RawAssetGeneratorOptions {
outputPath?: JsFilename
publicPath?: "auto" | JsFilename
dataUrl?: RawAssetGeneratorDataUrlOptions | ((source: Buffer, context: RawAssetGeneratorDataUrlFnCtx) => string)
experimentalLibReExport?: boolean
experimentalLibPreserveImport?: boolean
}

export interface RawAssetInlineGeneratorOptions {
Expand All @@ -1153,6 +1155,8 @@ export interface RawAssetResourceGeneratorOptions {
filename?: JsFilename
outputPath?: JsFilename
publicPath?: "auto" | JsFilename
experimentalLibPreserveImport?: boolean
experimentalLibReExport?: boolean
}

export interface RawBannerPluginOptions {
Expand Down
10 changes: 10 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 @@ -504,6 +504,9 @@ pub struct RawAssetGeneratorOptions {
ts_type = "RawAssetGeneratorDataUrlOptions | ((source: Buffer, context: RawAssetGeneratorDataUrlFnCtx) => string)"
)]
pub data_url: Option<RawAssetGeneratorDataUrl>,

pub experimental_lib_re_export: Option<bool>,
pub experimental_lib_preserve_import: Option<bool>,
}

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

pub experimental_lib_preserve_import: Option<bool>,
pub experimental_lib_re_export: Option<bool>,
}

impl From<RawAssetResourceGeneratorOptions> for AssetResourceGeneratorOptions {
Expand All @@ -557,6 +565,8 @@ impl From<RawAssetResourceGeneratorOptions> for AssetResourceGeneratorOptions {
filename: value.filename.map(|i| i.into()),
output_path: value.output_path.map(|i| i.into()),
public_path: value.public_path.map(|i| i.into()),
experimental_lib_preserve_import: value.experimental_lib_preserve_import,
experimental_lib_re_export: value.experimental_lib_re_export,
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_core/src/options/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ pub struct AssetResourceGeneratorOptions {
pub filename: Option<Filename>,
pub output_path: Option<Filename>,
pub public_path: Option<PublicPath>,
pub experimental_lib_re_export: Option<bool>,
pub experimental_lib_preserve_import: Option<bool>,
}

#[cacheable]
Expand All @@ -393,6 +395,8 @@ pub struct AssetGeneratorOptions {
pub output_path: Option<Filename>,
pub public_path: Option<PublicPath>,
pub data_url: Option<AssetGeneratorDataUrl>,
pub experimental_lib_re_export: Option<bool>,
pub experimental_lib_preserve_import: Option<bool>,
}

pub struct AssetGeneratorDataUrlFnCtx<'a> {
Expand Down
54 changes: 53 additions & 1 deletion crates/rspack_plugin_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,6 @@ impl ParserAndGenerator for AssetParserAndGenerator {
let exported_content = if parsed_asset_config.is_inline() {
let resource_data: &ResourceData = normal_module.resource_resolved_data();
let data_url = module_generator_options.and_then(|x| x.asset_data_url());

let encoded_source: String;

if let Some(custom_data_url) =
Expand Down Expand Up @@ -522,6 +521,59 @@ impl ParserAndGenerator for AssetParserAndGenerator {
} else {
unreachable!()
};

let experimental_lib_preserve_import = module_generator_options
.and_then(|x| x.get_asset())
.and_then(|x| x.experimental_lib_preserve_import)
.or_else(|| {
module_generator_options
.and_then(|x| x.get_asset_resource())
.and_then(|x| x.experimental_lib_preserve_import)
})
.unwrap_or(false);
let experimental_lib_re_export = module_generator_options
.and_then(|x| x.get_asset())
.and_then(|x| x.experimental_lib_re_export)
.or_else(|| {
module_generator_options
.and_then(|x| x.get_asset_resource())
.and_then(|x| x.experimental_lib_re_export)
})
.unwrap_or(false);

if experimental_lib_preserve_import || experimental_lib_re_export {
let Some(PublicPath::Auto) = module_generator_options.and_then(|x| x.asset_public_path())
else {
return Err(error!(
"`experimentalLibPreserveImport` and `experimentalLibReExport` can only be used with `asset/resource` and `publicPath: 'auto'`"
));
};

if let Some(ref mut scope) = generate_context.concatenation_scope {
scope.register_namespace_export(NAMESPACE_OBJECT_EXPORT);
return Ok(if experimental_lib_re_export {
RawStringSource::from(format!(
r#"import {NAMESPACE_OBJECT_EXPORT} from {exported_content};
export default {NAMESPACE_OBJECT_EXPORT};"#
))
.boxed()
} else {
RawStringSource::from(format!(
r#"import {NAMESPACE_OBJECT_EXPORT} from {exported_content};"#
))
.boxed()
});
} else {
generate_context
.runtime_requirements
.insert(RuntimeGlobals::MODULE);
return Ok(
RawStringSource::from(format!(r#"module.exports = require({exported_content})"#))
.boxed(),
);
}
}

if let Some(ref mut scope) = generate_context.concatenation_scope {
scope.register_namespace_export(NAMESPACE_OBJECT_EXPORT);
let supports_const = compilation.options.output.environment.supports_const();
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();
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
2 changes: 2 additions & 0 deletions packages/rspack/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ export type AssetResourceGeneratorOptions = {
filename?: Filename;
outputPath?: AssetModuleOutputPath;
publicPath?: PublicPath;
experimentalLibReExport?: boolean;
experimentalLibPreserveImport?: boolean;
};

// @public (undocumented)
Expand Down
4 changes: 3 additions & 1 deletion packages/rspack/src/config/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,9 @@ function getRawAssetResourceGeneratorOptions(
emit: options.emit,
filename: options.filename,
outputPath: options.outputPath,
publicPath: options.publicPath
publicPath: options.publicPath,
experimentalLibReExport: options.experimentalLibReExport,
experimentalLibPreserveImport: options.experimentalLibPreserveImport
};
}

Expand Down
5 changes: 5 additions & 0 deletions packages/rspack/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,11 @@ export type AssetResourceGeneratorOptions = {

/** This option determines the URL prefix of the referenced 'asset' or 'asset/resource'*/
publicPath?: PublicPath;

/** */
experimentalLibReExport?: boolean;
/** */
experimentalLibPreserveImport?: boolean;
};

/** Generator options for asset modules. */
Expand Down
Loading