@@ -718,7 +718,7 @@ export default class MagicString {
718
718
}
719
719
720
720
replace ( searchValue , replacement ) {
721
- function getReplacement ( match ) {
721
+ function getReplacement ( match , str ) {
722
722
if ( typeof replacement === 'string' ) {
723
723
return replacement . replace ( / \$ ( \$ | & | \d + ) / g, ( _ , i ) => {
724
724
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter
@@ -733,7 +733,7 @@ export default class MagicString {
733
733
} ) ;
734
734
}
735
735
else {
736
- return replacement ( ...match ) ;
736
+ return replacement ( ...match , match . index , str , match . groups ) ;
737
737
}
738
738
}
739
739
function matchAll ( re , str ) {
@@ -748,13 +748,13 @@ export default class MagicString {
748
748
const matches = matchAll ( searchValue , this . original ) ;
749
749
matches . forEach ( ( match ) => {
750
750
if ( match . index != null )
751
- this . overwrite ( match . index , match . index + match [ 0 ] . length , getReplacement ( match ) ) ;
751
+ this . overwrite ( match . index , match . index + match [ 0 ] . length , getReplacement ( match , this . original ) ) ;
752
752
} ) ;
753
753
}
754
754
else {
755
755
const match = this . original . match ( searchValue ) ;
756
756
if ( match && match . index != null )
757
- this . overwrite ( match . index , match . index + match [ 0 ] . length , getReplacement ( match ) ) ;
757
+ this . overwrite ( match . index , match . index + match [ 0 ] . length , getReplacement ( match , this . original ) ) ;
758
758
}
759
759
return this ;
760
760
}
0 commit comments