Skip to content

Irrelevant assets are incorrectly marked as SVG #74

@rbozan

Description

@rbozan

I think there is a bug somewhere in the code around determining whether an imported module is actually a SVG or not:

sveltekit-svg/src/index.ts

Lines 241 to 250 in 2d7b509

if (isType(type, 'url')) {
if (isDevelopmentMode()) {
const msg = `"url" imports are not optimized in development mode`
this.info({
id,
message: msg,
})
}
return { code: source, map: null, meta: { isSvgUrl: true } }

I am loading .wav files using the following code before in Sveltekit which gets transformed to URLs so I can use them in an <audio> element:

<script>
	import hintAudio from '$lib/audio/hint.wav';
</script>
<audio src={hintAudio} bind:volume autoplay />

And for some reason in my recent builds the above import seems to trigger this plugin and returns me a svgo error during a production build.. Which is weird as it's completely unrelated to SVG.

#22 18.31 error during build:
#22 18.31 [sveltekit-svg] <input>:1:1: Non-whitespace before first tag.
#22 18.31     at sax.onerror (/app/node_modules/svgo/lib/parser.js:247:19)
#22 18.31     at emit (/app/node_modules/@trysound/sax/lib/sax.js:518:35)
#22 18.31     at error (/app/node_modules/@trysound/sax/lib/sax.js:549:5)
#22 18.31     at strictFail (/app/node_modules/@trysound/sax/lib/sax.js:573:7)
#22 18.31     at beginWhiteSpace (/app/node_modules/@trysound/sax/lib/sax.js:847:7)
#22 18.31     at SAXParser.write (/app/node_modules/@trysound/sax/lib/sax.js:902:11)
#22 18.31     at parseSvg (/app/node_modules/svgo/lib/parser.js:259:7)
#22 18.31     at optimize (/app/node_modules/svgo/lib/svgo.js:66:17)
#22 18.31     at optimize (/app/node_modules/svgo/lib/svgo-node.js:76:10)
#22 18.31     at Object.generateBundle (/app/node_modules/@poppanator/sveltekit-svg/dist/index.js:102:52)
#22 18.31     at Object.handler (file:///app/node_modules/vite/dist/node/chunks/dep-Bm2ujbhY.js:34064:13)
#22 18.31     at file:///app/node_modules/rollup/dist/es/shared/node-entry.js:22425:40

But if I add this patch to this plugin:

diff --git a/node_modules/@poppanator/sveltekit-svg/dist/index.js b/node_modules/@poppanator/sveltekit-svg/dist/index.js
index e34832c..1f49f90 100644
--- a/node_modules/@poppanator/sveltekit-svg/dist/index.js
+++ b/node_modules/@poppanator/sveltekit-svg/dist/index.js
@@ -96,7 +96,7 @@ function readSvg(options = { type: 'component' }) {
             // Resolve assets that should be SVGO optimized before written to disk.
             // Such assets should exist in the `optimizeUrl` lookup data structure.
             for (const [k, v] of Object.entries(bundle)) {
-                if (!optmizeUrls.has(k) || v.type !== 'asset') {
+                if (!optmizeUrls.has(k) || v.type !== 'asset' || !k.endsWith('.svg')) {
                     continue;
                 }
                 const optSvg = (0, svgo_1.optimize)(v.source.toString(), options.svgoOptions || undefined);

Everything builds like normal again.

Sub-issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions