Skip to content

Commit a5321d0

Browse files
committed
node v4+ rewrite; options validation
1 parent 4783923 commit a5321d0

File tree

8 files changed

+3497
-183
lines changed

8 files changed

+3497
-183
lines changed

index.js

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,3 @@
1-
var _ = require('lodash');
2-
var loaderUtils = require('loader-utils');
1+
const processChuck = require('./lib/processChunk')
32

4-
function processOptions(source, options) {
5-
if (!_.isUndefined(options.search) && !_.isUndefined(options.replace)) {
6-
var search = options.search;
7-
8-
if (!_.isUndefined(options.flags)) {
9-
search = new RegExp(options.search, options.flags);
10-
}
11-
12-
var newSource = source.replace(search, options.replace);
13-
if (options.strict && (newSource === source)) {
14-
throw new Error('Cannot replace ' + options.search + ' → ' + options.replace);
15-
}
16-
} else if (options.strict) {
17-
throw new Error('Cannot replace: undefined search or/and option(s) → ' + JSON.stringify(options));
18-
}
19-
20-
return newSource;
21-
}
22-
23-
module.exports = function (source, map) {
24-
this.cacheable();
25-
26-
var options = loaderUtils.getOptions(this);
27-
28-
if (_.isArray(options.multiple)) {
29-
options.multiple.forEach(function (suboptions) {
30-
suboptions.strict = (!_.isUndefined(suboptions.strict) ? suboptions.strict : options.strict);
31-
source = processOptions(source, suboptions);
32-
});
33-
} else {
34-
source = processOptions(source, options);
35-
}
36-
37-
this.callback(null, source, map);
38-
};
3+
module.exports = processChuck

lib/getOptionsArray.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const { getOptions } = require('loader-utils')
2+
const validateOptions = require('schema-utils')
3+
4+
const loaderName = 'string-replace-loader'
5+
6+
const optionsSchema = {
7+
type: 'object',
8+
properties: {
9+
search: {
10+
type: 'string'
11+
},
12+
replace: {
13+
type: 'string'
14+
},
15+
flags: {
16+
type: 'string',
17+
},
18+
strict: {
19+
type: 'boolean'
20+
}
21+
},
22+
additionalProperties: false
23+
}
24+
25+
const defaultOptions = {
26+
search: null,
27+
replace: null,
28+
flags: null,
29+
strict: false
30+
}
31+
32+
function getOptionsArray (config) {
33+
const rawOptions = getOptions(config)
34+
const rawOptionsArray = (
35+
typeof rawOptions.multiple !== 'undefined'
36+
? rawOptions.multiple
37+
: [rawOptions]
38+
)
39+
const optionsArray = []
40+
41+
for (const optionsIndex in rawOptionsArray) {
42+
validateOptions(optionsSchema, rawOptionsArray[optionsIndex], loaderName)
43+
44+
optionsArray[optionsIndex] = Object.assign({}, defaultOptions, rawOptionsArray[optionsIndex])
45+
}
46+
47+
return optionsArray
48+
}
49+
50+
module.exports = getOptionsArray

lib/processChunk.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const getOptionsArray = require('./getOptionsArray')
2+
const replace = require('./replace')
3+
4+
function processChunk (source, map) {
5+
this.cacheable()
6+
7+
const optionsArray = getOptionsArray(this)
8+
let newSource = source
9+
10+
for (const options of optionsArray) {
11+
newSource = replace(newSource, options)
12+
}
13+
14+
this.callback(null, newSource, map)
15+
}
16+
17+
module.exports = processChunk

lib/replace.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
function replace (source, options) {
3+
const { replace, flags, strict } = options
4+
const search = (
5+
flags === null
6+
? options.search
7+
: new RegExp(options.search, flags)
8+
)
9+
10+
if (strict && (search === null || replace === null)) {
11+
throw new Error('Replace failed (strict mode) : options.search and options.replace are required')
12+
}
13+
14+
const newSource = source.replace(search, replace)
15+
16+
if (strict && (newSource === source)) {
17+
throw new Error('Replace failed (strict mode) : ' + options.search + ' → ' + options.replace)
18+
}
19+
20+
return newSource
21+
}
22+
23+
module.exports = replace

node_modules/__this-loader/index.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)