2018-05-17 04:18:45 -05:00
|
|
|
import { getOptions } from 'loader-utils';
|
2018-02-15 11:53:54 -06:00
|
|
|
|
2018-02-18 00:27:06 -06:00
|
|
|
function trim(text, g) {
|
|
|
|
return text.substring(1, text.length - (g ? 2 : 0));
|
2018-02-15 11:53:54 -06:00
|
|
|
}
|
|
|
|
|
2018-05-17 04:18:45 -05:00
|
|
|
export default function(src) {
|
2018-02-15 11:53:54 -06:00
|
|
|
this.cacheable();
|
2018-05-17 04:18:45 -05:00
|
|
|
const options = getOptions(this);
|
2018-02-15 12:23:10 -06:00
|
|
|
const search = options.search;
|
2018-02-18 00:27:06 -06:00
|
|
|
const g = search[search.length - 1] == 'g';
|
2018-04-14 11:04:40 -05:00
|
|
|
const file = this.resourcePath.replace(/\\/g, '/');
|
2018-04-14 12:51:29 -05:00
|
|
|
const replace = options.i18n ? global[options.replace].bind(null, {
|
2018-04-14 16:16:50 -05:00
|
|
|
src: file,
|
|
|
|
lang: options.lang
|
2018-04-14 12:51:29 -05:00
|
|
|
}) : global[options.replace];
|
2018-02-15 12:23:10 -06:00
|
|
|
if (typeof search != 'string' || search.length == 0) console.error('invalid search');
|
|
|
|
if (typeof replace != 'function') console.error('invalid replacer:', replace, this.request);
|
2018-02-18 00:27:06 -06:00
|
|
|
src = src.replace(new RegExp(trim(search, g), g ? 'g' : ''), replace);
|
2018-02-15 11:53:54 -06:00
|
|
|
this.callback(null, src);
|
|
|
|
return src;
|
2018-05-17 04:18:45 -05:00
|
|
|
}
|