-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
Named asset import for SVG files #3907
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we also add an end to end test case that would have failed if we regressed on SVG URLs again?
We probably have an existing one that just doesn’t check the computed style. Maybe we could change it to use getComputedStyle
and verify the string contains a proper URL. (You’d need to first check that this is implemented in jsdom.)
"index.js" | ||
], | ||
"dependencies": { | ||
"@babel/core": "7.0.0-beta.38" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? Maybe this should be a peer dependency?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I originally wrote this plugin outside create-react-app so I needed this. I can change it to a peer dependency.
I haven't added any tests yet. I wanted to start getting feedback as soon as possible. An e2e test for the CSS use case is definitely on this list. I'd also like to add some tests to the babel plugin itself. |
@@ -115,6 +115,14 @@ module.exports = function(api, opts) { | |||
regenerator: true, | |||
}, | |||
], | |||
[ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably have an opt-out flag in options. Just like the flow flag we now have.
{ | ||
loaderMap: { | ||
svg: filename => `-!svg-react-loader!${filename}`, | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, maybe this should be in webpack Babel config and not in our preset? Since it’s directly related to webpack specifically. This also would make a peer dependency on the loader unnecessary (it currently would be).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, either works. The only nice thing about keeping it here is that we don't have to duplicate it in the dev and prod webpack configs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can solve it in other ways (e.g. by actually starting to modularizing configs a little bit). Doesn't have to be solved here.
What's the pending work here? |
Remaining items:
|
What was the reason we didn't use |
I tried |
@gaearon What are your thoughts on how to handle point number 4? |
To be honest |
I agree, that was a concern I had with Ping @neoziro. |
Why not passing |
I tried passing the loader in the query string but that means we also have to pass it a bunch of config too and I wasn't able to get it to work. |
You can't avoid running Babel on these files, I can't decide for you the config to apply and what is the browser targeted. react-svg-loader applies an arbitrary Babel config and I think it is not a good approach. Personally I think that the babel + Webpack query string is too tricky. As I said in #3856, it could be solved using Webpack |
Is there a large difference in possible output though? As far as I can tell just emitting ES5 with |
Yes it is. But what about modules? Should I use |
That seems like the only point of contention so it could be toggled with a flag |
Always emitting ESM also sounds good to me. |
@gaearon OK if you need it, I can implement it. It would also reduce boilerplate https://github.com/smooth-code/svgr#webpack-usage 😉. |
I created an issue on SVGR. I will do it tomorrow if nobody has taken it. gregberge/svgr#45 @gaearon @iansu feel free to precise the issue on SVGR, the first goal is to be compatible with your work. |
Here's another thing probably worth doing. React has certain optimizations when the element is referentially equal. I see you allow overriding import {createElement} from 'react';
var content = createElement(/* everything inside svg */);
function MyIcon(props) {
return createElement('svg', props, content);
} The benefit is that if |
This is done and released in svgr v1.8, I included babel-transform-react-constant-elements as you suggested @gaearon. It should work out of the box without |
Awesome! I'll update this PR to use the new version. |
One thing to keep in mind is that It probably is fine when the output is simple and fully under our control (i.e. just SVG nodes). Just something to be aware of in case the generated code is more sophisticated. It usually breaks down in combination with other complex transforms. |
The new |
I know, using it on a complete codebase is kind of tricky (and even trickier if you do not use it from start). But as you said, I think the plugin can do the job for SVG. I can't apply optimization in a dummy way because I have several options in SVGR that make SVG depending on |
Makes sense. Yeah should be fine. |
@@ -115,6 +115,16 @@ module.exports = function(api, opts) { | |||
regenerator: true, | |||
}, | |||
], | |||
[ | |||
require('babel-plugin-named-asset-import'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be enabled in the test environment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was just about to ask for suggestions on how to fix these tests. 😀
I was trying to find a way to use Jest's moduleNameMapper
to do something with the webpack loader strings but I will try just disabling the plugin in the test environment.
it('svg component', async () => { | ||
const doc = await initDOM('svg-component'); | ||
|
||
expect(doc.getElementById('feature-svg-component').textContent).to.equal( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realize this test is not terribly useful but I'm not sure what I can check for here. I can try and improve it or just remove it entirely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can do getComputedStyle
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for the SVG component not SVG in CSS so I don't think it will have any style?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh okay. Then can’t we test that innerHTML contains some SVG tags?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're not getting a real SVG here because we mock it in fileTransform.js
like so: ReactComponent: () => ${assetFilename}
. That's why I initially checked the textContent
for logo.svg
but it's empty. I think the parent element would contain the filename but I'm not sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If that works let’s test for that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no idea if that works (or how to do it) but I'll give it a try. 😀
packages/react-scripts/package.json
Outdated
@@ -56,7 +56,7 @@ | |||
"raf": "3.4.0", | |||
"react-dev-utils": "^5.0.0", | |||
"style-loader": "0.19.1", | |||
"svgr": "1.6.0", | |||
"svgr": "^1.8.1", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please keep the version pinned
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do.
@@ -115,6 +115,16 @@ module.exports = function(api, opts) { | |||
regenerator: true, | |||
}, | |||
], | |||
!isEnvTest && [ | |||
require('babel-plugin-named-asset-import'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about moving this to webpack configs instead of keeping it in the preset? That would make more sense to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know we talked about that before and I tried it but I couldn't get it to work. For some reason when I move this to the webpack config it strips out functions. In this case the svg
key would be present in the loaderMap
object but it would just be an empty object. So the ReactComponent
key would be removed presumably because its value is a function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you push a commit that tries to implement it, even if it doesn’t work? I think we need to figure out what breaks there. Fine to do as another PR against your existing PR if you prefer so
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually try rebasing. We don’t write babelrc anymore (afaik) since monorepos landed. So it doesn’t get serialized on eject and thus it may be solved now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I merged the next
branch (with the monorepo stuff) into this branch a couple of commits ago. That should have the same effect right? You're saying I should try moving this to the webpack config again and see if it works now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to merge if you figure out how to move this out of the Babel preset
Okay, I'll work on it tonight and see if I can get it working. If not I'll push what I've got and we can try and figure it out. |
I figured out the issue with passing a function to plugin options in the webpack config. It's I'm not really sure how to proceed. I don't think we want to remove |
Let’s make the config serializable then. loaderMap: {
svg: {
ReactComponent: 'svgr/webpack![path]',
},
} Then do a string replacement for |
@@ -200,6 +200,18 @@ module.exports = { | |||
babelrc: false, | |||
// @remove-on-eject-end | |||
presets: [require.resolve('babel-preset-react-app')], | |||
plugins: [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow up: this will need an explanatory comment for what it does
@@ -192,6 +192,18 @@ module.exports = { | |||
babelrc: false, | |||
// @remove-on-eject-end | |||
presets: [require.resolve('babel-preset-react-app')], | |||
plugins: [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment below
const doc = await initDOM('svg-component'); | ||
|
||
expect(doc.getElementById('feature-svg-component').textContent).to.equal( | ||
'' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still want a better test here
* Add named asset import for svg files via babel plugin and webpack loader. * Fix failing e2e test * Switched to svgr loader * Updated SVG component test * Disable named asset import plugin in test environment * Added tests for including SVG in CSS * Update tests * Moved babel plugin config into webpack config
* Add named asset import for svg files via babel plugin and webpack loader. * Fix failing e2e test * Switched to svgr loader * Updated SVG component test * Disable named asset import plugin in test environment * Added tests for including SVG in CSS * Update tests * Moved babel plugin config into webpack config
Adds a babel plugin that transforms named SVG imports into components via a webpack loader. Usage:
import { default as Logo } from './logo.svg';
The loader currently being used
svg-react-loader
doesn't provide a named export which is why we have to importdefault
above. This can potentially be fixed in the new babel plugin, by making a PR tosvg-react-loader
or by using a different loader.Closes #3856