Skip to content

Commit 1a365a2

Browse files
author
James Andersen
committed
Merge branch 'master' of github.com:jamesandersen/string-replace-webpack-plugin
2 parents cd632d5 + 2af7f42 commit 1a365a2

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

loader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ module.exports = function(source) {
1616

1717
if(typeof source === "string") {
1818
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);
2121
} else {
2222
this.emitWarning("'source' received by loader was not a string");
2323
}

test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,32 @@ describe('StringReplacePlugin', function(){
9090
replaced = loader.call(mockConfig, "some <!-- @secret stuff --> string");
9191
assert.equal(replaced, "some replaced ==>stuff<== string", "replaces matches");
9292
});
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: /<!-- @secret (\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+
});
93120
})
94121
});

0 commit comments

Comments
 (0)