Created
May 6, 2022 08:33
-
-
Save Excalibaard/5ec706565788ed9c118e08794b28149c to your computer and use it in GitHub Desktop.
SVGO + Vue inline SVG loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks a lot @Excalibaard