1
0
mirror of https://github.com/hotomoe/hotomoe synced 2024-12-03 01:08:15 +09:00
hotomoe/webpack/loaders/replace.js
syuilo d253df4574 ✌️
2018-05-17 18:18:45 +09:00

23 lines
770 B
JavaScript

import { getOptions } from 'loader-utils';
function trim(text, g) {
return text.substring(1, text.length - (g ? 2 : 0));
}
export default function(src) {
this.cacheable();
const options = getOptions(this);
const search = options.search;
const g = search[search.length - 1] == 'g';
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);
this.callback(null, src);
return src;
}