1
0
forked from mirror/misskey
misskey/webpack/loaders/replace.js

18 lines
572 B
JavaScript
Raw Normal View History

2018-02-16 02:53:54 +09:00
const loaderUtils = require('loader-utils');
function trim(text) {
return text.substring(1, text.length - 2);
}
module.exports = function(src) {
this.cacheable();
const options = loaderUtils.getOptions(this);
2018-02-16 03:23:10 +09:00
const search = options.search;
const replace = 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'), replace);
2018-02-16 02:53:54 +09:00
this.callback(null, src);
return src;
};