Skip to content

Commit 317ce8a

Browse files
author
Valentyn
committed
version 1.2.0. strict mode
1 parent c9f35af commit 317ce8a

File tree

3 files changed

+7
-36
lines changed

3 files changed

+7
-36
lines changed

index.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,12 @@ function processOptions(source, options) {
77
options.search = new RegExp(options.search, options.flags);
88
}
99

10-
newSource = source.replace(options.search, options.replace);
11-
if (options.strict === true && newSource === source) {
10+
var newSource = source.replace(options.search, options.replace);
11+
if (options.strict && (newSource === source)) {
1212
throw new Error('Cannot replace ' + options.search + ' → ' + options.replace);
1313
}
14-
}
15-
16-
if (options.strict === true && _.isUndefined(options.search)) {
17-
throw new Error('Cannot replace: search option is not defined → ' + JSON.stringify(options));
18-
}
19-
20-
if (options.strict === true && _.isUndefined(options.replace)) {
21-
throw new Error('Cannot replace: replace option is not defined → ' + JSON.stringify(options));
14+
} else if (options.strict) {
15+
throw new Error('Cannot replace: undefined search or/and option(s) → ' + JSON.stringify(options));
2216
}
2317

2418
return newSource;
@@ -31,7 +25,7 @@ module.exports = function (source) {
3125

3226
if (_.isArray(options.multiple)) {
3327
options.multiple.forEach(function (suboptions) {
34-
suboptions.strict = !_.isUndefined(suboptions.strict) ? suboptions.strict : options.strict;
28+
suboptions.strict = (!_.isUndefined(suboptions.strict) ? suboptions.strict : options.strict);
3529
source = processOptions(source, suboptions);
3630
});
3731
} else {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "string-replace-loader",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "Replace loader for Webpack",
55
"keywords": [
66
"webpack",

test/index.test.js

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -187,29 +187,6 @@ describe('Webpack replace loader ...', function () {
187187
);
188188
});
189189

190-
it('should not throw error when cannot replace in single mode and strict is object', function (done) {
191-
webpack(getTestWebPackConfig(
192-
{
193-
test: /\.js$/,
194-
loader: '__this-loader',
195-
query: {
196-
search: 'unexisting value',
197-
replace: 'var a',
198-
strict: 'some string'
199-
}
200-
}),
201-
function (error, stats) {
202-
expect(error).to.equal(null);
203-
204-
fs.readFile(outputFilePath, 'utf8', function (error, contents) {
205-
expect(error).to.equal(null);
206-
expect(contents).is.not.include('Cannot replace unexisting value → var a');
207-
done();
208-
});
209-
}
210-
);
211-
});
212-
213190
it('should not throw error when cannot replace in multiple mode', function (done) {
214191
webpack(getTestWebPackConfig(
215192
{
@@ -311,7 +288,7 @@ describe('Webpack replace loader ...', function () {
311288
fs.readFile(outputFilePath, 'utf8', function (error, contents) {
312289
expect(error).to.equal(null);
313290
expect(contents).to.be.a('string');
314-
expect(contents).is.include('Cannot replace: search option is not defined');
291+
expect(contents).is.include('Cannot replace: undefined search or/and option(s)');
315292
done();
316293
});
317294
}

0 commit comments

Comments
 (0)