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

Update method for handling SVGs via webpack #45

Merged
merged 6 commits into from
Jul 3, 2022
Merged

Conversation

g-elwell
Copy link
Member

@g-elwell g-elwell commented Jun 7, 2022

Description

Fixes #34. This PR updates the way that SVGs are processed via Webpack, following the methods outlined in the SVGR documentation related to handling both SVG and SVGR components in the same project.

With this change, SVGs are processed differently depending on whether you include ?url as a resourceQuery on the end of your import file path.

If ?url is present, the SVG will be processed as an asset (file or dataURI depending on file size), otherwise it will run through SVGR and be converted into a React component.

The previous method could handle both import types, but there are a few benefits in making this change:

  • babel-loader is not required when using @svgr/webpack (source)
  • url-loader is being deprecated in favour of asset modules (source)
  • The syntax for importing SVGs into React components is simplified
  • The amount of processing for each SVG import is minimised

This PR introduces a breaking change as existing SVG imports would need to be updated from:

import mySvg from './images/my-svg.svg'; // import as a URL
import { ReactComponent as MySvg } from './images/my-svg.svg'; // import as React component

to:

import mySvg from './images/my-svg.svg?url'; // import as a URL
import MySvg from './images/my-svg.svg'; // import as React component

How has this been tested?

  • SVGs imported as assets work as expected
  • SVGs imported as React components work as expected

@g-elwell g-elwell added the bug Something isn't working label Jun 7, 2022
@g-elwell g-elwell requested a review from ampersarnie June 7, 2022 08:28
@g-elwell g-elwell self-assigned this Jun 7, 2022
@ampersarnie ampersarnie added this to the v1.0 milestone Jun 13, 2022
Copy link
Member

@ampersarnie ampersarnie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added in an example of how this works under the example-site. This is also used for an integration test to ensure we're accidentally introducing breaking changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

[Bug]: SVG conversion back and forth.
2 participants