Skip to content

Commit fa5278a

Browse files
committed
chore: update
1 parent 1c0a097 commit fa5278a

File tree

26 files changed

+607
-127
lines changed

26 files changed

+607
-127
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+
importMode?: "url" | "preserve"
11461147
}
11471148

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

11721174
export interface RawBannerPluginOptions {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,9 @@ pub struct RawAssetGeneratorOptions {
541541
ts_type = "RawAssetGeneratorDataUrlOptions | ((source: Buffer, context: RawAssetGeneratorDataUrlFnCtx) => string)"
542542
)]
543543
pub data_url: Option<RawAssetGeneratorDataUrl>,
544+
545+
#[napi(ts_type = r#""url" | "preserve""#)]
546+
pub import_mode: Option<String>,
544547
}
545548

546549
impl From<RawAssetGeneratorOptions> for AssetGeneratorOptions {
@@ -553,6 +556,7 @@ impl From<RawAssetGeneratorOptions> for AssetGeneratorOptions {
553556
data_url: value
554557
.data_url
555558
.map(|i| RawAssetGeneratorDataUrlWrapper(i).into()),
559+
import_mode: value.import_mode.map(|n| n.into()),
556560
}
557561
}
558562
}
@@ -585,6 +589,8 @@ pub struct RawAssetResourceGeneratorOptions {
585589
pub output_path: Option<JsFilename>,
586590
#[napi(ts_type = "\"auto\" | JsFilename")]
587591
pub public_path: Option<JsFilename>,
592+
#[napi(ts_type = r#""url" | "preserve""#)]
593+
pub import_mode: Option<String>,
588594
}
589595

590596
impl From<RawAssetResourceGeneratorOptions> for AssetResourceGeneratorOptions {
@@ -594,6 +600,7 @@ impl From<RawAssetResourceGeneratorOptions> for AssetResourceGeneratorOptions {
594600
filename: value.filename.map(|i| i.into()),
595601
output_path: value.output_path.map(|i| i.into()),
596602
public_path: value.public_path.map(|i| i.into()),
603+
import_mode: value.import_mode.map(|i| i.into()),
597604
}
598605
}
599606
}

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 containing AUTO_PUBLIC_PATH_PLACEHOLDER
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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,13 +472,53 @@ impl From<AssetGeneratorOptions> for AssetInlineGeneratorOptions {
472472
}
473473
}
474474

475+
#[cacheable]
476+
#[derive(Debug, Clone, Copy, MergeFrom)]
477+
struct AssetGeneratorImportModeFlags(u8);
478+
bitflags! {
479+
impl AssetGeneratorImportModeFlags: u8 {
480+
const URL = 1 << 0;
481+
const PRESERVE = 1 << 1;
482+
}
483+
}
484+
485+
#[cacheable]
486+
#[derive(Debug, Clone, Copy, MergeFrom)]
487+
pub struct AssetGeneratorImportMode(AssetGeneratorImportModeFlags);
488+
489+
impl AssetGeneratorImportMode {
490+
pub fn is_url(&self) -> bool {
491+
self.0.contains(AssetGeneratorImportModeFlags::URL)
492+
}
493+
pub fn is_preserve(&self) -> bool {
494+
self.0.contains(AssetGeneratorImportModeFlags::PRESERVE)
495+
}
496+
}
497+
498+
impl From<String> for AssetGeneratorImportMode {
499+
fn from(s: String) -> Self {
500+
match s.as_str() {
501+
"url" => Self(AssetGeneratorImportModeFlags::URL),
502+
"preserve" => Self(AssetGeneratorImportModeFlags::PRESERVE),
503+
_ => unreachable!("AssetGeneratorImportMode error"),
504+
}
505+
}
506+
}
507+
508+
impl Default for AssetGeneratorImportMode {
509+
fn default() -> Self {
510+
Self(AssetGeneratorImportModeFlags::URL)
511+
}
512+
}
513+
475514
#[cacheable]
476515
#[derive(Debug, Clone, MergeFrom)]
477516
pub struct AssetResourceGeneratorOptions {
478517
pub emit: Option<bool>,
479518
pub filename: Option<Filename>,
480519
pub output_path: Option<Filename>,
481520
pub public_path: Option<PublicPath>,
521+
pub import_mode: Option<AssetGeneratorImportMode>,
482522
}
483523

484524
impl From<AssetGeneratorOptions> for AssetResourceGeneratorOptions {
@@ -488,6 +528,7 @@ impl From<AssetGeneratorOptions> for AssetResourceGeneratorOptions {
488528
filename: value.filename,
489529
output_path: value.output_path,
490530
public_path: value.public_path,
531+
import_mode: value.import_mode,
491532
}
492533
}
493534
}
@@ -500,6 +541,7 @@ pub struct AssetGeneratorOptions {
500541
pub output_path: Option<Filename>,
501542
pub public_path: Option<PublicPath>,
502543
pub data_url: Option<AssetGeneratorDataUrl>,
544+
pub import_mode: Option<AssetGeneratorImportMode>,
503545
}
504546

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

0 commit comments

Comments
 (0)