1
+ use std:: sync:: LazyLock ;
2
+
3
+ use regex:: Regex ;
1
4
use rspack_cacheable:: { cacheable, cacheable_dyn, with:: AsPreset } ;
2
5
use 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,
8
12
} ;
9
13
use swc_core:: ecma:: atoms:: Atom ;
10
14
11
- use crate :: connection_active_used_by_exports;
15
+ use crate :: { connection_active_used_by_exports, runtime :: AUTO_PUBLIC_PATH_PLACEHOLDER } ;
12
16
13
17
#[ cacheable]
14
18
#[ derive( Debug , Clone ) ]
@@ -19,7 +23,7 @@ pub struct URLDependency {
19
23
range : DependencyRange ,
20
24
range_url : DependencyRange ,
21
25
used_by_exports : Option < UsedByExports > ,
22
- relative : bool ,
26
+ mode : Option < JavascriptParserUrl > ,
23
27
factorize_info : FactorizeInfo ,
24
28
}
25
29
@@ -28,15 +32,15 @@ impl URLDependency {
28
32
request : Atom ,
29
33
range : DependencyRange ,
30
34
range_url : DependencyRange ,
31
- relative : bool ,
35
+ mode : Option < JavascriptParserUrl > ,
32
36
) -> Self {
33
37
Self {
34
38
id : DependencyId :: new ( ) ,
35
39
request,
36
40
range,
37
41
range_url,
38
42
used_by_exports : None ,
39
- relative ,
43
+ mode ,
40
44
factorize_info : Default :: default ( ) ,
41
45
}
42
46
}
@@ -105,6 +109,11 @@ impl AsContextDependency for URLDependency {}
105
109
#[ derive( Debug , Clone , Default ) ]
106
110
pub struct URLDependencyTemplate ;
107
111
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
+
108
117
impl URLDependencyTemplate {
109
118
pub fn template_type ( ) -> DependencyTemplateType {
110
119
DependencyTemplateType :: Dependency ( DependencyType :: NewUrl )
@@ -130,34 +139,57 @@ impl DependencyTemplate for URLDependencyTemplate {
130
139
131
140
runtime_requirements. insert ( RuntimeGlobals :: REQUIRE ) ;
132
141
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
+ }
161
193
}
162
194
}
163
195
}
0 commit comments