UNPKG

hrpng-loader

Version:

auto resolve High resolution icon when import an icon

45 lines (40 loc) 1.21 kB
/* * @Author: dyxuan * @Date: 2020-03-17 11:41:06 * @LastEditTime: 2020-09-23 17:08:48 * @LastEditors: dyxuan */ let loaderUtils = require('loader-utils') const fs = require('fs'); function getHDFile(src) { try { if(!src) return src; if(process.platform == 'win32') { src = src.replace('\\', '/') } let [, dir, file, ext] = src.match(/(.+\/)(.+?)\.(.+)$/); let filelist = fs.readdirSync(dir), target = `${file}.${ext}`; let reg = new RegExp(`${file}@(\\d?)[xX]\\.${ext}`) filelist.forEach((file, i)=>{ reg.test(file)&&(target = file) }) return `${dir}${target}` } catch (error) { throw new Error(`hrpng-loader error:${error}`) } } module.exports = function (source, map){ try { this.cacheable && this.cacheable(); const options = loaderUtils.getOptions(this); if(options.hr) { let src = getHDFile(this.resourcePath); source = fs.readFileSync(src) } else { source = fs.readFileSync(this.resourcePath) } this.callback(null, source, map); } catch (error) { this.callback(error); } };