2018-05-17 03:18:45 -06:00
|
|
|
import { getOptions } from 'loader-utils';
|
2018-02-15 10:53:54 -07:00
|
|
|
|
2018-02-17 23:27:06 -07:00
|
|
|
function trim(text, g) {
|
|
|
|
return text.substring(1, text.length - (g ? 2 : 0));
|
2018-02-15 10:53:54 -07:00
|
|
|
}
|
|
|
|
|
2018-05-17 03:18:45 -06:00
|
|
|
export default function(src) {
|
2018-05-17 04:38:20 -06:00
|
|
|
const fn = options => {
|
|
|
|
const search = options.search;
|
2018-08-25 22:55:39 -06:00
|
|
|
const g = search.endsWith('g');
|
2018-05-17 04:38:20 -06:00
|
|
|
const file = this.resourcePath.replace(/\\/g, '/');
|
|
|
|
const replace = options.i18n ? global[options.replace].bind(null, {
|
|
|
|
src: file,
|
|
|
|
lang: options.lang
|
|
|
|
}) : global[options.replace];
|
|
|
|
if (typeof search != 'string' || search.length == 0) console.error('invalid search');
|
|
|
|
if (typeof replace != 'function') console.error('invalid replacer:', replace, this.request);
|
|
|
|
src = src.replace(new RegExp(trim(search, g), g ? 'g' : ''), replace);
|
|
|
|
};
|
|
|
|
|
2018-02-15 10:53:54 -07:00
|
|
|
this.cacheable();
|
2018-05-17 03:18:45 -06:00
|
|
|
const options = getOptions(this);
|
2018-05-17 04:38:20 -06:00
|
|
|
if (options.qs) {
|
|
|
|
options.qs.forEach(q => fn(q));
|
|
|
|
} else {
|
|
|
|
fn(options);
|
|
|
|
}
|
2018-02-15 10:53:54 -07:00
|
|
|
this.callback(null, src);
|
|
|
|
return src;
|
2018-05-17 03:18:45 -06:00
|
|
|
}
|