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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option for preserving kebab-case attributes (for Preact) #450

Closed
komima opened this issue May 5, 2020 · 6 comments 路 May be fixed by #927
Closed

Option for preserving kebab-case attributes (for Preact) #450

komima opened this issue May 5, 2020 · 6 comments 路 May be fixed by #927

Comments

@komima
Copy link

komima commented May 5, 2020

馃殌 Feature Proposal

I'd like to see webpack loader support optionally preserving kebab-case attribute names. Maybe with a opt-in boolean flag in loader options (to be used together with babel: false as shown here.

Motivation

I'm using @svgr/webpack for a Preact project (not using preact/compat) and ran into problems when kebab-case attributes used in svg file are transformed into react-style camelCase. As a result in Preact these attributes appear in html as-is (camelCased) and have no effect.

My current solution is to use babel: false and a custom babel config doing auto-import preact when JSX is seen (with babel-plugin-jsx-pragmatic). To fix the casing problem I had to add a custom babel plugin to transform these JSX attributes names back.

Example

When @svgr/webpack loader is used with an option possibly something like preserveKebabCase: true, svg file having content <path d="..." stroke-width="15%" /> would output JSX having name preserved as stroke-width instead of mapping it to strokeWidth.

Maybe even preact: true could be considered? Both to preserve kebab-case names and to import { h } from 'preact';. That would probably fit easier into existing babel configs if auto-import plugin is not used and only transform-react-jsx is present.

Pitch

To make this library more versatile. And not to have to use a additional custom babel plugin to transform names back and forth.

@TheKnarf
Copy link

I need this as well for usage with my library isomorphic-jsx.

@stale
Copy link

stale bot commented Jul 21, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jul 21, 2020
@gregberge
Copy link
Owner

Hello @TheKnarf, I am open to extend the support for Preact. The simplest is to add an option Preact and automatically apply the correct configuration. Since I don't use Preact, I trust the community on this!

@stale
Copy link

stale bot commented Sep 25, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@bensaufley
Copy link

It seems like, maybe, between the time this issue was opened and closed, this was resolved and then unresolved again? I have been using vite-plugin-svgr 2.4.0 (which used @svgr/core v6.5.1) with Preact and the following config:

    svgr({
      svgrOptions: {
        jsxRuntimeImport: {
          importSource: 'preact',
          specifiers: ['h'],
        },
        svgProps: {
          role: 'img',
        },
      },
      exportAsDefault: true,
    }),

This was outputting kebab-case attributes.

Upgrading to vite-plugin-svgr 3.0.1 bumped @svgr/core to 7.0.0 and now with an updated config that looks like this, attributes are camelCase. Is there a way around this?

    svgr({
      svgrOptions: {
        exportType: 'default',
        jsxRuntimeImport: {
          source: 'preact',
          specifiers: ['h'],
        },
        svgProps: {
          role: 'img',
        },
      },
      esbuildOptions: {
        jsxFactory: 'h',
        jsxFragment: 'Fragment',
      },
      exportAsDefault: true,
    }),

I tried adding @svgr/plugin-jsx because I see that's been removed from @svgr/core as of 7.x, but it appears to have no effect.

As far as I can tell, this is not an issue with the Vite plugin: the diff between the two versions doesn't seem to show any difference in how options are being passed to svgr or how svgr is being configured by default.

@bensaufley
Copy link

Just to follow up here: I figured out that my issue was related to vite-plugin-svgr or svgr not fully respecting my configuration for using Preact in the earlier version; so React was getting inserted then re-routed to preact/compat, which was handling the camelCasing. Updating versions must have fixed that configuration issue so that it was using Preact out of the box, which does not handle camelCasing and expects standard attribute casing.

Hopefully #927 is a viable solution

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

Successfully merging a pull request may close this issue.

4 participants