Skip to content

Commit c4e833d

Browse files
committed
chore: update
1 parent 995abc6 commit c4e833d

File tree

25 files changed

+432
-93
lines changed

25 files changed

+432
-93
lines changed

crates/node_binding/binding.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,7 @@ export interface RawAssetGeneratorOptions {
11431143
outputPath?: JsFilename
11441144
publicPath?: "auto" | JsFilename
11451145
dataUrl?: RawAssetGeneratorDataUrlOptions | ((source: Buffer, context: RawAssetGeneratorDataUrlFnCtx) => string)
1146+
experimentalLibPreserveImport?: boolean
11461147
}
11471148

11481149
export interface RawAssetInlineGeneratorOptions {
@@ -1167,6 +1168,7 @@ export interface RawAssetResourceGeneratorOptions {
11671168
filename?: JsFilename
11681169
outputPath?: JsFilename
11691170
publicPath?: "auto" | JsFilename
1171+
experimentalLibPreserveImport?: boolean
11701172
}
11711173

11721174
export interface RawBannerPluginOptions {

crates/rspack_binding_values/src/raw_options/raw_module/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,8 @@ pub struct RawAssetGeneratorOptions {
541541
ts_type = "RawAssetGeneratorDataUrlOptions | ((source: Buffer, context: RawAssetGeneratorDataUrlFnCtx) => string)"
542542
)]
543543
pub data_url: Option<RawAssetGeneratorDataUrl>,
544+
545+
pub experimental_lib_preserve_import: Option<bool>,
544546
}
545547

546548
impl From<RawAssetGeneratorOptions> for AssetGeneratorOptions {
@@ -553,6 +555,7 @@ impl From<RawAssetGeneratorOptions> for AssetGeneratorOptions {
553555
data_url: value
554556
.data_url
555557
.map(|i| RawAssetGeneratorDataUrlWrapper(i).into()),
558+
experimental_lib_preserve_import: value.experimental_lib_preserve_import,
556559
}
557560
}
558561
}
@@ -585,6 +588,8 @@ pub struct RawAssetResourceGeneratorOptions {
585588
pub output_path: Option<JsFilename>,
586589
#[napi(ts_type = "\"auto\" | JsFilename")]
587590
pub public_path: Option<JsFilename>,
591+
592+
pub experimental_lib_preserve_import: Option<bool>,
588593
}
589594

590595
impl From<RawAssetResourceGeneratorOptions> for AssetResourceGeneratorOptions {
@@ -594,6 +599,7 @@ impl From<RawAssetResourceGeneratorOptions> for AssetResourceGeneratorOptions {
594599
filename: value.filename.map(|i| i.into()),
595600
output_path: value.output_path.map(|i| i.into()),
596601
public_path: value.public_path.map(|i| i.into()),
602+
experimental_lib_preserve_import: value.experimental_lib_preserve_import,
597603
}
598604
}
599605
}

crates/rspack_core/src/artifacts/code_generation_results.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ impl CodeGenerationDataUrl {
3131
}
3232
}
3333

34+
// For performance, mark the js modules contains AUTO_PUBLIC_PATH_PLACEHOLDER, and replace it
35+
#[derive(Clone, Debug)]
36+
pub struct CodeGenerationPublicPathAutoReplace(pub bool);
37+
3438
#[derive(Clone, Debug)]
3539
pub struct CodeGenerationDataFilename {
3640
filename: String,

crates/rspack_core/src/concatenated_module.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ use crate::{
4545
subtract_runtime_condition, to_identifier, AsyncDependenciesBlockIdentifier, BoxDependency,
4646
BuildContext, BuildInfo, BuildMeta, BuildMetaDefaultObject, BuildMetaExportsType, BuildResult,
4747
ChunkGraph, ChunkInitFragments, CodeGenerationDataTopLevelDeclarations,
48-
CodeGenerationExportsFinalNames, CodeGenerationResult, Compilation, ConcatenatedModuleIdent,
49-
ConcatenationScope, ConnectionState, Context, DependenciesBlock, DependencyId,
50-
DependencyTemplate, DependencyType, ErrorSpan, ExportInfoProvided, ExportsArgument, ExportsType,
51-
FactoryMeta, IdentCollector, LibIdentOptions, MaybeDynamicTargetExportInfoHashKey, Module,
52-
ModuleDependency, ModuleGraph, ModuleGraphConnection, ModuleIdentifier, ModuleLayer, ModuleType,
53-
Resolve, RuntimeCondition, RuntimeGlobals, RuntimeSpec, SourceType, SpanExt, Template,
54-
UsageState, UsedName, DEFAULT_EXPORT, NAMESPACE_OBJECT_EXPORT,
48+
CodeGenerationExportsFinalNames, CodeGenerationPublicPathAutoReplace, CodeGenerationResult,
49+
Compilation, ConcatenatedModuleIdent, ConcatenationScope, ConnectionState, Context,
50+
DependenciesBlock, DependencyId, DependencyTemplate, DependencyType, ErrorSpan,
51+
ExportInfoProvided, ExportsArgument, ExportsType, FactoryMeta, IdentCollector, LibIdentOptions,
52+
MaybeDynamicTargetExportInfoHashKey, Module, ModuleDependency, ModuleGraph,
53+
ModuleGraphConnection, ModuleIdentifier, ModuleLayer, ModuleType, Resolve, RuntimeCondition,
54+
RuntimeGlobals, RuntimeSpec, SourceType, SpanExt, Template, UsageState, UsedName, DEFAULT_EXPORT,
55+
NAMESPACE_OBJECT_EXPORT,
5556
};
5657

5758
type ExportsDefinitionArgs = Vec<(String, String)>;
@@ -193,6 +194,8 @@ pub struct ConcatenatedModuleInfo {
193194
pub global_scope_ident: Vec<ConcatenatedModuleIdent>,
194195
pub idents: Vec<ConcatenatedModuleIdent>,
195196
pub binding_to_ref: HashMap<(Atom, SyntaxContext), Vec<ConcatenatedModuleIdent>>,
197+
198+
pub public_path_auto_replace: Option<bool>,
196199
}
197200

198201
#[derive(Debug, Clone)]
@@ -672,6 +675,7 @@ impl Module for ConcatenatedModule {
672675

673676
let mut all_used_names: HashSet<Atom> = RESERVED_NAMES.iter().map(|s| Atom::new(*s)).collect();
674677
let mut top_level_declarations: HashSet<Atom> = HashSet::default();
678+
let mut public_path_auto_replace: bool = false;
675679

676680
for module_info_id in modules_with_info.iter() {
677681
let Some(ModuleInfo::Concatenated(info)) = module_to_info_map.get_mut(module_info_id) else {
@@ -800,6 +804,11 @@ impl Module for ConcatenatedModule {
800804
info.namespace_object_name = Some(namespace_object_name.clone());
801805
top_level_declarations.insert(namespace_object_name);
802806
}
807+
808+
// Handle publicPathAutoReplace for perf
809+
if let Some(info_auto) = info.public_path_auto_replace {
810+
public_path_auto_replace = public_path_auto_replace || info_auto;
811+
}
803812
}
804813

805814
// Handle external type
@@ -1288,6 +1297,13 @@ impl Module for ConcatenatedModule {
12881297
code_generation_result.add(SourceType::JavaScript, CachedSource::new(result).boxed());
12891298
code_generation_result.chunk_init_fragments = chunk_init_fragments;
12901299
code_generation_result.runtime_requirements = runtime_requirements;
1300+
1301+
if public_path_auto_replace {
1302+
code_generation_result
1303+
.data
1304+
.insert(CodeGenerationPublicPathAutoReplace(true));
1305+
}
1306+
12911307
code_generation_result
12921308
.data
12931309
.insert(CodeGenerationDataTopLevelDeclarations::new(
@@ -1701,6 +1717,7 @@ impl ConcatenatedModule {
17011717
.module_by_identifier(&module_id)
17021718
.unwrap_or_else(|| panic!("should have module {module_id}"));
17031719
let codegen_res = module.code_generation(compilation, runtime, Some(concatenation_scope))?;
1720+
17041721
let CodeGenerationResult {
17051722
mut inner,
17061723
mut chunk_init_fragments,
@@ -1779,6 +1796,12 @@ impl ConcatenatedModule {
17791796
module_info.internal_source = Some(source);
17801797
module_info.source = Some(result_source);
17811798
module_info.chunk_init_fragments = chunk_init_fragments;
1799+
if let Some(CodeGenerationPublicPathAutoReplace(true)) = codegen_res
1800+
.data
1801+
.get::<CodeGenerationPublicPathAutoReplace>(
1802+
) {
1803+
module_info.public_path_auto_replace = Some(true);
1804+
}
17821805
Ok(ModuleInfo::Concatenated(Box::new(module_info)))
17831806
} else {
17841807
Ok(info)

crates/rspack_core/src/options/module.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ pub struct AssetResourceGeneratorOptions {
479479
pub filename: Option<Filename>,
480480
pub output_path: Option<Filename>,
481481
pub public_path: Option<PublicPath>,
482+
pub experimental_lib_preserve_import: Option<bool>,
482483
}
483484

484485
impl From<AssetGeneratorOptions> for AssetResourceGeneratorOptions {
@@ -488,6 +489,7 @@ impl From<AssetGeneratorOptions> for AssetResourceGeneratorOptions {
488489
filename: value.filename,
489490
output_path: value.output_path,
490491
public_path: value.public_path,
492+
experimental_lib_preserve_import: value.experimental_lib_preserve_import,
491493
}
492494
}
493495
}
@@ -500,6 +502,7 @@ pub struct AssetGeneratorOptions {
500502
pub output_path: Option<Filename>,
501503
pub public_path: Option<PublicPath>,
502504
pub data_url: Option<AssetGeneratorDataUrl>,
505+
pub experimental_lib_preserve_import: Option<bool>,
503506
}
504507

505508
pub struct AssetGeneratorDataUrlFnCtx<'a> {

crates/rspack_plugin_asset/Cargo.toml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ version = "0.2.0"
88
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
99

1010
[dependencies]
11-
async-trait = { workspace = true }
12-
mime_guess = { workspace = true }
13-
rayon = { workspace = true }
14-
rspack_base64 = { workspace = true }
11+
async-trait = { workspace = true }
12+
mime_guess = { workspace = true }
13+
rayon = { workspace = true }
14+
rspack_base64 = { workspace = true }
1515
rspack_cacheable = { workspace = true }
16-
rspack_core = { workspace = true }
17-
rspack_error = { workspace = true }
18-
rspack_hash = { workspace = true }
19-
rspack_hook = { workspace = true }
20-
rspack_util = { workspace = true }
21-
serde_json = { workspace = true }
22-
tracing = { workspace = true }
23-
urlencoding = { workspace = true }
16+
rspack_core = { workspace = true }
17+
rspack_error = { workspace = true }
18+
rspack_hash = { workspace = true }
19+
rspack_hook = { workspace = true }
20+
rspack_util = { workspace = true }
21+
serde_json = { workspace = true }
22+
tracing = { workspace = true }
23+
urlencoding = { workspace = true }
2424

2525
[package.metadata.cargo-shear]
2626
ignored = ["tracing"]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use rspack_cacheable::{cacheable, cacheable_dyn};
2+
use rspack_core::{
3+
AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyId,
4+
DependencyTemplate, ExportNameOrSpec, ExportSpec, ExportsOfExportsSpec, ExportsSpec, ModuleGraph,
5+
RuntimeSpec, TemplateContext, TemplateReplaceSource,
6+
};
7+
use rspack_plugin_javascript::dependency::ESMExportExpressionDependency;
8+
9+
#[cacheable]
10+
#[derive(Debug, Clone, Default)]
11+
pub struct AssetExportsDependency {
12+
id: DependencyId,
13+
}
14+
15+
impl AssetExportsDependency {
16+
pub fn new() -> Self {
17+
Self {
18+
id: DependencyId::new(),
19+
}
20+
}
21+
}
22+
23+
#[cacheable_dyn]
24+
impl Dependency for AssetExportsDependency {
25+
fn id(&self) -> &rspack_core::DependencyId {
26+
&self.id
27+
}
28+
29+
fn get_exports(&self, _mg: &ModuleGraph) -> Option<ExportsSpec> {
30+
Some(ExportsSpec {
31+
exports: ExportsOfExportsSpec::Array(vec![ExportNameOrSpec::String("default".into())]),
32+
priority: Some(1),
33+
terminal_binding: Some(true),
34+
..Default::default()
35+
})
36+
}
37+
38+
fn could_affect_referencing_module(&self) -> rspack_core::AffectType {
39+
rspack_core::AffectType::False
40+
}
41+
}
42+
43+
impl AsModuleDependency for AssetExportsDependency {}
44+
impl AsContextDependency for AssetExportsDependency {}
45+
46+
#[cacheable_dyn]
47+
impl DependencyTemplate for AssetExportsDependency {
48+
fn apply(
49+
&self,
50+
_source: &mut TemplateReplaceSource,
51+
_code_generatable_context: &mut TemplateContext,
52+
) {
53+
}
54+
55+
fn dependency_id(&self) -> Option<DependencyId> {
56+
Some(self.id)
57+
}
58+
59+
fn update_hash(
60+
&self,
61+
_hasher: &mut dyn std::hash::Hasher,
62+
_compilation: &Compilation,
63+
_runtime: Option<&RuntimeSpec>,
64+
) {
65+
}
66+
}

0 commit comments

Comments
 (0)