Skip to content

Commit

Permalink
node v4+ rewrite; options validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Va1 committed Feb 25, 2018
1 parent 4783923 commit a5321d0
Show file tree
Hide file tree
Showing 8 changed files with 3,497 additions and 183 deletions.
39 changes: 2 additions & 37 deletions index.js
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
50 changes: 50 additions & 0 deletions lib/getOptionsArray.js
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.

Copy link
@pioug

pioug Mar 6, 2018

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!

},
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
17 changes: 17 additions & 0 deletions lib/processChunk.js
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
23 changes: 23 additions & 0 deletions lib/replace.js
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
1 change: 0 additions & 1 deletion node_modules/__this-loader/index.js

This file was deleted.

Loading

0 comments on commit a5321d0

Please sign in to comment.