Uh oh!
There was an error while loading. Please reload this page.
Named asset import for SVG files - #3907
Conversation
gaearon
left a comment
There was a problem hiding this comment.
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.
Why? Maybe this should be a peer dependency?
There was a problem hiding this comment.
I originally wrote this plugin outside create-react-app so I needed this. I can change it to a peer dependency.
iansu
commented
Jan 23, 2018
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. |
| regenerator: true, | ||
| }, | ||
| ], | ||
| [ |
There was a problem hiding this comment.
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.
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.
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.
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.
gaearon
commented
Jan 25, 2018
What's the pending work here? |
iansu
commented
Jan 25, 2018
Remaining items:
|
gaearon
commented
Jan 25, 2018
What was the reason we didn't use |
iansu
commented
Jan 25, 2018
I tried |
iansu
commented
Jan 25, 2018
@gaearon What are your thoughts on how to handle point number 4? |
gaearon
commented
Jan 25, 2018
To be honest |
iansu
commented
Jan 25, 2018
I agree, that was a concern I had with Ping @neoziro. |
gregberge
commented
Jan 26, 2018
Why not passing |
iansu
commented
Jan 26, 2018
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. |
gregberge
commented
Jan 27, 2018
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 |
gaearon
commented
Jan 27, 2018
Is there a large difference in possible output though? As far as I can tell just emitting ES5 with |
gregberge
commented
Jan 27, 2018
Yes it is. But what about modules? Should I use |
gaearon
commented
Jan 27, 2018
That seems like the only point of contention so it could be toggled with a flag |
gaearon
commented
Jan 27, 2018
Always emitting ESM also sounds good to me. |
gregberge
commented
Jan 27, 2018
@gaearon OK if you need it, I can implement it. It would also reduce boilerplate https://github.com/smooth-code/svgr#webpack-usage 😉. |
gregberge
commented
Jan 27, 2018
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. |
gaearon
commented
Jan 27, 2018
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';varcontent=createElement(/* everything inside svg */);functionMyIcon(props){returncreateElement('svg',props,content);}The benefit is that if |
gregberge
commented
Jan 31, 2018
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 |
iansu
commented
Jan 31, 2018
Awesome! I'll update this PR to use the new version. |
gaearon
commented
Feb 1, 2018
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. |
iansu
commented
Feb 1, 2018
The new |
gregberge
commented
Feb 1, 2018
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 |
gaearon
commented
Feb 1, 2018
Makes sense. Yeah should be fine. |
| }, | ||
| ], | ||
| [ | ||
| require('babel-plugin-named-asset-import'), |
There was a problem hiding this comment.
This shouldn't be enabled in the test environment.
There was a problem hiding this comment.
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.
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.
Maybe we can do getComputedStyle here?
There was a problem hiding this comment.
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.
Oh okay. Then can’t we test that innerHTML contains some SVG tags?
There was a problem hiding this comment.
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.
If that works let’s test for that
There was a problem hiding this comment.
I have no idea if that works (or how to do it) but I'll give it a try. 😀
| "react-dev-utils": "^5.0.0", | ||
| "style-loader": "0.19.1", | ||
| "svgr": "1.6.0", | ||
| "svgr": "^1.8.1", |
| }, | ||
| ], | ||
| !isEnvTest && [ | ||
| require('babel-plugin-named-asset-import'), |
There was a problem hiding this comment.
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.
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.
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.
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.
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?
gaearon
left a comment
There was a problem hiding this comment.
Feel free to merge if you figure out how to move this out of the Babel preset
iansu
commented
Feb 1, 2018
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. |
iansu
commented
Feb 2, 2018
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 |
gaearon
commented
Feb 2, 2018
Let’s make the config serializable then. loaderMap: {svg: {ReactComponent: 'svgr/webpack![path]',},}Then do a string replacement for |
| babelrc: false, | ||
| // @remove-on-eject-end | ||
| presets: [require.resolve('babel-preset-react-app')], | ||
| plugins: [ |
There was a problem hiding this comment.
Follow up: this will need an explanatory comment for what it does
| babelrc: false, | ||
| // @remove-on-eject-end | ||
| presets: [require.resolve('babel-preset-react-app')], | ||
| plugins: [ |
| const doc = await initDOM('svg-component'); | ||
| expect(doc.getElementById('feature-svg-component').textContent).to.equal( | ||
| '' |
There was a problem hiding this comment.
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-loaderdoesn't provide a named export which is why we have to importdefaultabove. This can potentially be fixed in the new babel plugin, by making a PR tosvg-react-loaderor by using a different loader.Closes#3856