-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
node v4+ rewrite; options validation
- Loading branch information
Showing
8 changed files
with
3,497 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,3 @@ | ||
var _ = require('lodash'); | ||
var loaderUtils = require('loader-utils'); | ||
const processChuck = require('./lib/processChunk') | ||
|
||
function processOptions(source, options) { | ||
if (!_.isUndefined(options.search) && !_.isUndefined(options.replace)) { | ||
var search = options.search; | ||
|
||
if (!_.isUndefined(options.flags)) { | ||
search = new RegExp(options.search, options.flags); | ||
} | ||
|
||
var newSource = source.replace(search, options.replace); | ||
if (options.strict && (newSource === source)) { | ||
throw new Error('Cannot replace ' + options.search + ' → ' + options.replace); | ||
} | ||
} else if (options.strict) { | ||
throw new Error('Cannot replace: undefined search or/and option(s) → ' + JSON.stringify(options)); | ||
} | ||
|
||
return newSource; | ||
} | ||
|
||
module.exports = function (source, map) { | ||
this.cacheable(); | ||
|
||
var options = loaderUtils.getOptions(this); | ||
|
||
if (_.isArray(options.multiple)) { | ||
options.multiple.forEach(function (suboptions) { | ||
suboptions.strict = (!_.isUndefined(suboptions.strict) ? suboptions.strict : options.strict); | ||
source = processOptions(source, suboptions); | ||
}); | ||
} else { | ||
source = processOptions(source, options); | ||
} | ||
|
||
this.callback(null, source, map); | ||
}; | ||
module.exports = processChuck |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const { getOptions } = require('loader-utils') | ||
const validateOptions = require('schema-utils') | ||
|
||
const loaderName = 'string-replace-loader' | ||
|
||
const optionsSchema = { | ||
type: 'object', | ||
properties: { | ||
search: { | ||
type: 'string' | ||
}, | ||
replace: { | ||
type: 'string' | ||
This comment has been minimized.
Sorry, something went wrong. |
||
}, | ||
flags: { | ||
type: 'string', | ||
}, | ||
strict: { | ||
type: 'boolean' | ||
} | ||
}, | ||
additionalProperties: false | ||
} | ||
|
||
const defaultOptions = { | ||
search: null, | ||
replace: null, | ||
flags: null, | ||
strict: false | ||
} | ||
|
||
function getOptionsArray (config) { | ||
const rawOptions = getOptions(config) | ||
const rawOptionsArray = ( | ||
typeof rawOptions.multiple !== 'undefined' | ||
? rawOptions.multiple | ||
: [rawOptions] | ||
) | ||
const optionsArray = [] | ||
|
||
for (const optionsIndex in rawOptionsArray) { | ||
validateOptions(optionsSchema, rawOptionsArray[optionsIndex], loaderName) | ||
|
||
optionsArray[optionsIndex] = Object.assign({}, defaultOptions, rawOptionsArray[optionsIndex]) | ||
} | ||
|
||
return optionsArray | ||
} | ||
|
||
module.exports = getOptionsArray |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const getOptionsArray = require('./getOptionsArray') | ||
const replace = require('./replace') | ||
|
||
function processChunk (source, map) { | ||
this.cacheable() | ||
|
||
const optionsArray = getOptionsArray(this) | ||
let newSource = source | ||
|
||
for (const options of optionsArray) { | ||
newSource = replace(newSource, options) | ||
} | ||
|
||
this.callback(null, newSource, map) | ||
} | ||
|
||
module.exports = processChunk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
function replace (source, options) { | ||
const { replace, flags, strict } = options | ||
const search = ( | ||
flags === null | ||
? options.search | ||
: new RegExp(options.search, flags) | ||
) | ||
|
||
if (strict && (search === null || replace === null)) { | ||
throw new Error('Replace failed (strict mode) : options.search and options.replace are required') | ||
} | ||
|
||
const newSource = source.replace(search, replace) | ||
|
||
if (strict && (newSource === source)) { | ||
throw new Error('Replace failed (strict mode) : ' + options.search + ' → ' + options.replace) | ||
} | ||
|
||
return newSource | ||
} | ||
|
||
module.exports = replace |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Hello @Va1! I am wondering why it is not allowed to pass a function anymore. I will probably roll out a fork for that so I can give Webpack 4 a try. I want to be sure that I won't break more things 💦
Thanks for maintaining this loader!