Skip to content

Add SVGR support to wp-scripts - #18243

Merged
mkaz merged 6 commits into
masterfrom
add/svgr-scripts
Jan 8, 2020
Merged

mkaz merged 6 commits into
masterfrom
add/svgr-scripts

Conversation

@mkaz

@mkaz mkaz commented Nov 1, 2019

Copy link
Copy Markdown
Member

Description

This PR adds @svgr/webpack plugin to compile SVG icons to React Components during the build step. This allows for loading an svg direct from a file and using as an component, for example:

import MyIcon from './icon.svg'
...
edit: () => {
    <>
        <MyIcon />
    </>
}

Related to #14628

How has this been tested?

In a separate project using wp-scripts build added a svg file and loaded and displayed.

Types of changes

Enhancement to wp-scripts

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.

@mkaz mkaz added the [Status] In Progress Tracking issues with work in progress label Nov 1, 2019
@mkaz mkaz added the [Tool] WP Scripts /packages/scripts label Nov 1, 2019
@mkaz

mkaz commented Nov 1, 2019

Copy link
Copy Markdown
Member Author

@gziolo I got this to work here - but struggled with getting it to work with the main Gutenberg webpack.config.js. It is relatively straight-forward to get to work here, as you can see by the change. However, due to the nature of how files get copied and built in the main project/packages/modules I wasn't able to get working there yet.

@gziolo

gziolo commented Nov 4, 2019

Copy link
Copy Markdown
Member

I don't have experience with this webpack plugin so I can't help much here. You can ask in #core-js channel for help. I also anticipate a bit of a challenge in terms of using it inside Gutenberg. This comes from the fact that we use only Babel to generate code shipped to npm so this would create invalid imports which Node can't process out of the box. At the same time, Jest will also need a custom handler to make such code execute without errors, we have something similar for CSS imports:
https://github.com/WordPress/gutenberg/blob/master/packages/jest-preset-default/jest-preset.json#L3

Aside: we don't use CSS imports anymore in the web part, but I think React Native does that :)

@gziolo

gziolo commented Dec 17, 2019

Copy link
Copy Markdown
Member

This reminds me of the addition to the docs introduced by @mor10:

https://github.com/WordPress/gutenberg/tree/master/packages/scripts#extending-the-webpack-config

It had something else integrated, so it would be nice to follow the example. It also means we would have to update this example to use some other loader 😄

@mkaz

mkaz commented Jan 7, 2020

Copy link
Copy Markdown
Member Author

@gziolo updated with url-loader which was included in @mor10 example.
I switched the extended example to use CSS and Style loaders.

I'm not sure how the changelog and version number should be updated.

@mkaz
mkaz force-pushed the add/svgr-scripts branch from a71c54b to 7e48a19 Compare January 7, 2020 18:06

@gziolo gziolo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure how the changelog and version number should be updated.

It's all documented in https://github.com/WordPress/gutenberg/tree/master/packages#maintaining-changelogs. Long story short, you add ## Master section and a section for changes. In this case, it'll be New Features. The version of the package should stay as is until we do next npm release.

Comment thread packages/scripts/package.json Resolved

@gziolo gziolo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I haven't tested it by it looks good as far as I can tell.

It would be great to schedule some testing once it's published to npm.

@mkaz
mkaz merged commit 416f1c5 into master Jan 8, 2020
@mkaz
mkaz deleted the add/svgr-scripts branch January 8, 2020 20:27
@ellatrix ellatrix added this to the Gutenberg 7.3 milestone Jan 20, 2020
swissspidy added a commit to GoogleForCreators/web-stories-wp that referenced this pull request Feb 6, 2020
Less magic, less dependencies, less security warnings.

See WordPress/gutenberg#18243
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Tool] WP Scripts /packages/scripts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants