使用webpackurl加载程序加载svg会返回js文件

Loading svg with webpack url-loader returns js file

本文关键字:加载 返回 js 文件 svg webpackurl 程序 使用      更新时间:2023-09-26

我正试图用webpack加载svg映像,如下所示:

const logo = require('./images/logo-white.svg')

请记住,这适用于我们所有的其他图像,它们都不是svg。无论我使用哪种加载程序组合,当我加载它放入的url或数据uri时,源代码如下所示:

module.exports = 'data-uri-that-is-the-actual-image'

我找到了一个"解决方案",实际上这是一个可怕的破解,但考虑到我还没有找到任何其他方法,它是:

function webpackSvgFixer(svg) {
  // Remove data...base64, headers, leaving just the base64
  svg = /data.*base64,(.*)/.exec(svg)[1]; 
  // Convert base64 to string, then remove module.exports = and quotes
  return /module'.exports = "(.*)"/.exec(atob(svg))[1];
};
const logo = webpackSvgFixer(require('./images/logo-white.svg'))