1+ use std:: sync:: LazyLock ;
2+
3+ use regex:: Regex ;
14use rspack_cacheable:: { cacheable, cacheable_dyn, with:: AsPreset } ;
25use rspack_core:: {
3- AsContextDependency , ConnectionState , Dependency , DependencyCategory , DependencyCodeGeneration ,
4- DependencyCondition , DependencyConditionFn , DependencyId , DependencyRange , DependencyTemplate ,
5- DependencyTemplateType , DependencyType , FactorizeInfo , ModuleDependency , ModuleGraph ,
6- ModuleGraphCacheArtifact , ModuleGraphConnection , RuntimeGlobals , RuntimeSpec , TemplateContext ,
7- TemplateReplaceSource , UsedByExports , module_id,
6+ AsContextDependency , CodeGenerationPublicPathAutoReplace , ConnectionState , Dependency ,
7+ DependencyCategory , DependencyCodeGeneration , DependencyCondition , DependencyConditionFn ,
8+ DependencyId , DependencyRange , DependencyTemplate , DependencyTemplateType , DependencyType ,
9+ FactorizeInfo , JavascriptParserUrl , ModuleDependency , ModuleGraph , ModuleGraphCacheArtifact ,
10+ ModuleGraphConnection , RuntimeGlobals , RuntimeSpec , TemplateContext , TemplateReplaceSource ,
11+ URLStaticMode , UsedByExports , module_id,
812} ;
913use swc_core:: ecma:: atoms:: Atom ;
1014
11- use crate :: connection_active_used_by_exports;
15+ use crate :: { connection_active_used_by_exports, runtime :: AUTO_PUBLIC_PATH_PLACEHOLDER } ;
1216
1317#[ cacheable]
1418#[ derive( Debug , Clone ) ]
@@ -19,7 +23,7 @@ pub struct URLDependency {
1923 range : DependencyRange ,
2024 range_url : DependencyRange ,
2125 used_by_exports : Option < UsedByExports > ,
22- relative : bool ,
26+ mode : Option < JavascriptParserUrl > ,
2327 factorize_info : FactorizeInfo ,
2428}
2529
@@ -28,15 +32,15 @@ impl URLDependency {
2832 request : Atom ,
2933 range : DependencyRange ,
3034 range_url : DependencyRange ,
31- relative : bool ,
35+ mode : Option < JavascriptParserUrl > ,
3236 ) -> Self {
3337 Self {
3438 id : DependencyId :: new ( ) ,
3539 request,
3640 range,
3741 range_url,
3842 used_by_exports : None ,
39- relative ,
43+ mode ,
4044 factorize_info : Default :: default ( ) ,
4145 }
4246 }
@@ -105,6 +109,11 @@ impl AsContextDependency for URLDependency {}
105109#[ derive( Debug , Clone , Default ) ]
106110pub struct URLDependencyTemplate ;
107111
112+ pub static URL_STATIC_PLACEHOLDER : & str = "RSPACK_AUTO_URL_STATIC_PLACEHOLDER_" ;
113+ pub static URL_STATIC_PLACEHOLDER_RE : LazyLock < Regex > = LazyLock :: new ( || {
114+ Regex :: new ( & format ! ( r#"{}(?<dep>\d+)"# , URL_STATIC_PLACEHOLDER ) ) . expect ( "should be valid regex" )
115+ } ) ;
116+
108117impl URLDependencyTemplate {
109118 pub fn template_type ( ) -> DependencyTemplateType {
110119 DependencyTemplateType :: Dependency ( DependencyType :: NewUrl )
@@ -130,34 +139,57 @@ impl DependencyTemplate for URLDependencyTemplate {
130139
131140 runtime_requirements. insert ( RuntimeGlobals :: REQUIRE ) ;
132141
133- if dep. relative {
134- runtime_requirements. insert ( RuntimeGlobals :: RELATIVE_URL ) ;
135- source. replace (
136- dep. range . start ,
137- dep. range . end ,
138- format ! (
139- "/* asset import */ new {}({}({}))" ,
140- RuntimeGlobals :: RELATIVE_URL ,
141- RuntimeGlobals :: REQUIRE ,
142- module_id( compilation, & dep. id, & dep. request, false ) ,
143- )
144- . as_str ( ) ,
145- None ,
146- ) ;
147- } else {
148- runtime_requirements. insert ( RuntimeGlobals :: BASE_URI ) ;
149- source. replace (
150- dep. range_url . start ,
151- dep. range_url . end ,
152- format ! (
153- "/* asset import */{}({}), {}" ,
154- RuntimeGlobals :: REQUIRE ,
155- module_id( compilation, & dep. id, & dep. request, false ) ,
156- RuntimeGlobals :: BASE_URI
157- )
158- . as_str ( ) ,
159- None ,
160- ) ;
142+ match dep. mode {
143+ Some ( JavascriptParserUrl :: Relative ) => {
144+ runtime_requirements. insert ( RuntimeGlobals :: RELATIVE_URL ) ;
145+ source. replace (
146+ dep. range . start ,
147+ dep. range . end ,
148+ format ! (
149+ "/* asset import */ new {}({}({}))" ,
150+ RuntimeGlobals :: RELATIVE_URL ,
151+ RuntimeGlobals :: REQUIRE ,
152+ module_id( compilation, & dep. id, & dep. request, false ) ,
153+ )
154+ . as_str ( ) ,
155+ None ,
156+ ) ;
157+ }
158+ Some ( JavascriptParserUrl :: NewUrlRelative ) => {
159+ code_generatable_context. data . insert ( URLStaticMode ) ;
160+ code_generatable_context
161+ . data
162+ . insert ( CodeGenerationPublicPathAutoReplace ( true ) ) ;
163+ source. replace (
164+ dep. range . start ,
165+ dep. range . end ,
166+ format ! (
167+ "new URL({}, import.meta.url)" ,
168+ serde_json:: to_string( & format!(
169+ "{AUTO_PUBLIC_PATH_PLACEHOLDER}{URL_STATIC_PLACEHOLDER}{}" ,
170+ & dep. id. as_u32( )
171+ ) )
172+ . expect( "should serde" ) ,
173+ )
174+ . as_str ( ) ,
175+ None ,
176+ ) ;
177+ }
178+ _ => {
179+ runtime_requirements. insert ( RuntimeGlobals :: BASE_URI ) ;
180+ source. replace (
181+ dep. range_url . start ,
182+ dep. range_url . end ,
183+ format ! (
184+ "/* asset import */{}({}), {}" ,
185+ RuntimeGlobals :: REQUIRE ,
186+ module_id( compilation, & dep. id, & dep. request, false ) ,
187+ RuntimeGlobals :: BASE_URI
188+ )
189+ . as_str ( ) ,
190+ None ,
191+ ) ;
192+ }
161193 }
162194 }
163195}
0 commit comments