Va1 / string-replace-loader

Replace loader for Webpack
MIT License
250 stars 57 forks source link

Unexpected output with webpack2 #38

Closed hydra1983 closed 6 years ago

hydra1983 commented 6 years ago

The code below will assign the created RegExp to options.search, in webpack2, the options.search will be reused for next file processing. So, the RegExp will be another one other than the first one expected.

function processOptions(source, options) {
  if (!_.isUndefined(options.search) && !_.isUndefined(options.replace)) {
    if (!_.isUndefined(options.flags)) {
      options.search = new RegExp(options.search, options.flags);
    }

    var newSource = source.replace(options.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;
}
Va1 commented 6 years ago

@hydra1983 PR merged and code will be available with next patch release. thank you