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

Importing images/SVG files #73

Closed
2 tasks done
edemaine opened this issue May 19, 2022 · 0 comments
Closed
2 tasks done

Importing images/SVG files #73

edemaine opened this issue May 19, 2022 · 0 comments

Comments

@edemaine
Copy link
Owner

edemaine commented May 19, 2022

Context: In the past I've written mapping files that manually load SVG or PNG assets via fs.readFileSync and __dirname. This is useful e.g. when assets are drawn in another tool like Inkscape, but the filenames are computed programmatically and they need to be wrapped/modified. (Otherwise we have the "return a filename" shortcut.)

Proposal: Use Babel plugins to make loading these as easy as import or require.

babel-plugin-transform-import-to-read-file-sync would be sufficient for SVG files. (The resulting strings could be used directly in other SVG.) For PNG/JPEG, we'd want to instead convert imports to <image href="..."/> strings, probably not inlined to allow the inlining options to do their thing. So babel-plugin-transform-media-imports is closer to what we want, but need to modify the output.


New approach is to use pirates (which is what babel/register uses anyway, and is better for runtime-generated filenames passed to require) to just handle these extensions directly.

  • PNG/JPEG/GIF turns into <image> tag: Preact DOM object, with .svg attribute to get raw string
  • SVG turns into SVG content via preact-html-converter Babel JSX transform, with .svg attribute to get raw string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant