@@ -48,6 +48,17 @@ export class ExternalsPlugin extends RspackBuiltinPlugin {
48
48
return Object . assign ( { } , resolveRequest ) ;
49
49
} ;
50
50
51
+ // Reference: webpack/enhanced-resolve#255
52
+ // Handle fragment escaping in resolve results:
53
+ // - `#` can be escaped as `\0#` to prevent fragment parsing
54
+ // - enhanced-resolve resolves `#` ambiguously as both path and fragment
55
+ // - Example: `./some#thing` could resolve to `.../some.js#thing` or `.../some#thing.js`
56
+ // - When `#` is part of the path, it gets escaped as `\0#` in the result
57
+ // - We replace `\0#` with zero-width space + `#` (\u200b#) for compatibility
58
+ #processRequest( req : ResolveRequest ) : string {
59
+ return `${ req . path . replace ( / # / g, "\u200b#" ) } ${ req . query . replace ( / # / g, "\u200b#" ) } ${ req . fragment } ` ;
60
+ }
61
+
51
62
#getRawExternalItem = ( item : ExternalItem | undefined ) : RawExternalItem => {
52
63
if ( typeof item === "string" || item instanceof RegExp ) {
53
64
return item ;
@@ -68,7 +79,7 @@ export class ExternalsPlugin extends RspackBuiltinPlugin {
68
79
issuer : data . contextInfo . issuer ,
69
80
issuerLayer : data . contextInfo . issuerLayer ?? null
70
81
} ,
71
- getResolve ( options ) {
82
+ getResolve : options => {
72
83
const rawResolve = options ? getRawResolve ( options ) : undefined ;
73
84
const resolve = ctx . getResolve ( rawResolve ) ;
74
85
@@ -83,7 +94,11 @@ export class ExternalsPlugin extends RspackBuiltinPlugin {
83
94
callback ( error ) ;
84
95
} else {
85
96
const req = processResolveResult ( text ) ;
86
- callback ( null , req ?. path ?? false , req ) ;
97
+ callback (
98
+ null ,
99
+ req ? this . #processRequest( req ) : false ,
100
+ req
101
+ ) ;
87
102
}
88
103
} ) ;
89
104
} else {
@@ -93,7 +108,9 @@ export class ExternalsPlugin extends RspackBuiltinPlugin {
93
108
promiseReject ( error ) ;
94
109
} else {
95
110
const req = processResolveResult ( text ) ;
96
- promiseResolve ( req ?. path ) ;
111
+ promiseResolve (
112
+ req ? this . #processRequest( req ) : undefined
113
+ ) ;
97
114
}
98
115
} ) ;
99
116
} ) ;
0 commit comments