File tree Expand file tree Collapse file tree 2 files changed +29
-2
lines changed Expand file tree Collapse file tree 2 files changed +29
-2
lines changed Original file line number Diff line number Diff line change @@ -16,8 +16,8 @@ module.exports = function(source) {
16
16
17
17
if ( typeof source === "string" ) {
18
18
options . replacements . forEach ( function ( repl ) {
19
- source = source . replace ( repl . pattern , repl . replacement ) ;
20
- } ) ;
19
+ source = source . replace ( repl . pattern , repl . replacement . bind ( this ) ) ;
20
+ } , this ) ;
21
21
} else {
22
22
this . emitWarning ( "'source' received by loader was not a string" ) ;
23
23
}
Original file line number Diff line number Diff line change @@ -90,5 +90,32 @@ describe('StringReplacePlugin', function(){
90
90
replaced = loader . call ( mockConfig , "some <!-- @secret stuff --> string" ) ;
91
91
assert . equal ( replaced , "some replaced ==>stuff<== string" , "replaces matches" ) ;
92
92
} ) ;
93
+
94
+ it ( 'should replace strings in source via options' , function ( ) {
95
+ mockConfig . options . replacement = {
96
+ before : 'replaced ==>' ,
97
+ after : '<=='
98
+ } ;
99
+ plugin . apply ( mockConfig ) ;
100
+
101
+ var replOpts = mockConfig . options [ StringReplacePlugin . REPLACE_OPTIONS ] ;
102
+
103
+ replOpts [ id ] = {
104
+ replacements : [ {
105
+ pattern : / < ! - - @ s e c r e t ( \w * ?) - - > / ig,
106
+ replacement : function ( match , p1 ) {
107
+ return this . options . replacement . before + p1 + this . options . replacement . after ;
108
+ }
109
+ } ]
110
+ } ;
111
+
112
+ mockConfig . query = query ;
113
+
114
+ var replaced = loader . call ( mockConfig , "some string" ) ;
115
+ assert ( replaced === "some string" , "doesn't modify when there are no matches" ) ;
116
+
117
+ replaced = loader . call ( mockConfig , "some <!-- @secret stuff --> string" ) ;
118
+ assert . equal ( replaced , "some replaced ==>stuff<== string" , "replaces matches" ) ;
119
+ } ) ;
93
120
} )
94
121
} ) ;
You can’t perform that action at this time.
0 commit comments