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

Render the contents of the <svg> tag using {@html} in the component mode #39

Closed
aradalvand opened this issue Jul 21, 2023 · 6 comments · Fixed by #40
Closed

Render the contents of the <svg> tag using {@html} in the component mode #39

aradalvand opened this issue Jul 21, 2023 · 6 comments · Fixed by #40

Comments

@aradalvand
Copy link
Contributor

aradalvand commented Jul 21, 2023

Importing an SVG file as a Svelte component with this plugin currently causes it to generate a Svelte component on the fly which will have the entire SVG directly as its content — it just adds {...$$props} on the <svg> tag:

function addComponentProps(data: string): string {
const parts = svgRegex.exec(data)
if (!parts) {
throw new Error('Invalid SVG')
}
const [, head, body] = parts
return `${head} {...$$props}${body}`
}

However, the problem with this is that when the Svelte compiler then looks at the generated component, it will parse all the elements inside the root <svg> tag, and will yield lots of compiler output with lots of function calls to create each element in the DOM. This is totally unnecessary. A better approach would be to render the contents of the <svg> tag using {@html}.

See this REPL, which demonstrates exactly this; take a look at AtHtmlSvg.svelte and RegularSvg.svelte and the JS output tab of each, and notice the difference.

Even some plugins have been created to address this, like svelte-preprocess-svg, for cases where you have inlined SVG elements in your Svelte components. But sveltekit-svg can make this improvement very easily.

Let me know if you're on board, and I will make a pull request.

@aradalvand aradalvand changed the title Render the contents of the <svg> tag using {@html} as string in the component mode Render the contents of the <svg> tag using {@html} in the component mode Jul 21, 2023
@poppa
Copy link
Owner

poppa commented Jul 23, 2023

Sorry for the delayed response. Vacation time and stuff so I've been out and about.

This sounds like an improvement to me, so I would gladly accept a PR 😍

@aradalvand
Copy link
Contributor Author

Hi @poppa, thank you for the response, no worries!
I just created #40.

@poppa
Copy link
Owner

poppa commented Jul 28, 2023

Thanks a bunch. I got another PR which is conflicting with this PR, but as soon as I have resolved both PR:s I will make a release.

I'm on vacation right now so it might take a few days.

@aradalvand
Copy link
Contributor Author

@poppa Got ya, no problem, enjoy the vacation!

@poppa
Copy link
Owner

poppa commented Aug 3, 2023

@aradalvand Thank you for your contribution. I have now released version 4.0.1 with your code changes 👍

@aradalvand
Copy link
Contributor Author

@poppa Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants