Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base64 output and class parameter #140

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 15 additions & 4 deletions index.js
Expand Up @@ -8,19 +8,22 @@ const debug = _debug('vite-svg-loader')
module.exports = function svgLoader (options = {}) {
const { svgoConfig, svgo, defaultImport } = options

const svgRegex = /\.svg(\?(raw|component|skipsvgo))?$/
const svgRegex = /\.svg(\?(raw|component|skipsvgo|base64))?(\=([a-z]+))?$/

return {
name: 'svg-loader',
enforce: 'pre',

async load (id) {
if (!id.match(svgRegex)) {
const regexResult = id.match(svgRegex);
if (! regexResult) {
return
}

const [path, query] = id.split('?', 2)

const [path] = id.split('?', 2)
const query = regexResult[2]
const parameter = regexResult[4]

const importType = query || defaultImport

if (importType === 'url') {
Expand All @@ -37,10 +40,18 @@ module.exports = function svgLoader (options = {}) {
return
}

if (parameter){
svg = svg.replace(/<svg/, `<svg class="${parameter}" `)
}

if (importType === 'raw') {
return `export default ${JSON.stringify(svg)}`
}

if (importType === 'base64') {
return `export default ${JSON.stringify(btoa(svg))}`
}

if (svgo !== false && query !== 'skipsvgo') {
svg = optimizeSvg(svg, {
...svgoConfig,
Expand Down