Skip to content

Instantly share code, notes, and snippets.

@Excalibaard
Created May 6, 2022 08:33
Show Gist options
  • Save Excalibaard/5ec706565788ed9c118e08794b28149c to your computer and use it in GitHub Desktop.
Save Excalibaard/5ec706565788ed9c118e08794b28149c to your computer and use it in GitHub Desktop.
SVGO + Vue inline SVG loader
const { optimize, loadConfig } = require('svgo');
const { getOptions } = require('loader-utils');
async function loader(svg) {
const { configFile, ...options } = getOptions(this) || {};
let config;
if (typeof configFile === 'string') {
config = await loadConfig(configFile, this.context);
} else if (configFile !== false) {
config = await loadConfig(null, this.context);
}
const result = optimize(svg, {
path: this.resourcePath,
...config,
...options,
});
if (result.error) throw Error(result.error);
return `<template>${result.data}</template>`;
}
module.exports = function(svg) {
const callback = this.async();
loader
.call(this, svg)
.then(result => callback(null, result))
.catch(error => callback(error));
};
@dschrul
Copy link

dschrul commented Aug 3, 2022

thanks a lot @Excalibaard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment