Comments (12)
@vitalyiegorov You can set a custom template for SVGR.
// svgr.config.js
const isExportNamedDeclaration = item => item.type === "ExportNamedDeclaration";
const hasExportNamedDeclaration = (items) => {
if (items === null) {
return false;
}
if (items instanceof Array) {
return !!(items && items.find(isExportNamedDeclaration))
}
return isExportNamedDeclaration(items)
}
const getName = (component) => {
if (component instanceof Object) {
return component.name;
}
return component;
}
const customTemplate = (
{ template },
opts,
{ imports, componentName, props, jsx, exports },
) => {
const ReactComponent = hasExportNamedDeclaration(exports)
? ''
: 'export const ReactComponent = ' + getName(componentName);
return template.ast`
${imports}
function ${componentName}(${props}) {
return ${jsx};
}
${ReactComponent}
${exports}
`
}
module.exports = {
template: customTemplate
}
@kristerkari Maybe it will be useful, so let's add it to the README file.
from react-native-svg-transformer.
this modification will influence only React Native projects which are using this transformer and thus adding named export won't break any existing React Native projects because the case with returning default as a string is not used in React Native projects, but this will give the ability to add react-native-web support to existing React Native CLI projects by changing default imports to named imports.
I get that, but the change that you are suggesting also means that there would be types mismatch when sharing the same code between React Native and React Web with CRA and using Typescript or Flow.
I understand that many people are using CRA as their boilerplate, but what they do for Web doesn't make sense on the React Native side.
I use Typescript in most of my projects, and I'm not keen on making a change that only partially matches what CRA is doing, as all the existing users of this library are already using the default import for the React component.
I would suggest that you try to use @svgr/webpack
directly instead, like it's set up in the demo project:
https://github.com/kristerkari/react-native-svg-example/blob/master/webpack.config.js#L28-L38
from react-native-svg-transformer.
Since SVGR v6.0.0, switching to named exports is just a matter of adding exportType: 'named'
to your svgrrc file
from react-native-svg-transformer.
Not sure what library/plugin CRA is using for the SVG images, but the idea with this transformer is that you can use SVGR in you Web projects, and the images and properties will work 100% the same between Web and React Native.
from react-native-svg-transformer.
Can SVGR easily be added to CRA? That would fix the problem for you.
The fix that you are suggesting is a breaking change, so I'm not that keen on making it if it can be avoided.
from react-native-svg-transformer.
Why do you think that this is a breaking change? It would leave default export SvgComponent
and same usage import Logo from './logo.svg';
unchanged, but also it will give ability to use named export: import {ReactComponent as Logo} from './logo.svg';
which is supported by default by CRA.
CRA uses SVGR but with some modifications, I am just trying to enable the simple bridge to use this plugin with react-native-web and CRA.
from react-native-svg-transformer.
Why do you think that this is a breaking change? It would leave
default export SvgComponent
and same usageimport Logo from './logo.svg';
unchanged
True, that would not be a breaking change, but the Typescript types will not be compatible with CRA as in CRA the default import seems to be a string, and the named import is a React component.
I need to look into the CRA docs a bit more to see if I'm looking at the correct things.
from react-native-svg-transformer.
Actually, I'm not completely sure if the default import in CRA is the url or not, I tried to look at this, but I'm not sure:
facebook/create-react-app#3722
from react-native-svg-transformer.
@kristerkari Referring to CRA docs, you could use default export for SVG usage as image source:
import Logo from './logo.svg';
...
return <image src={Logo}/>
or use SVG as react component:
import {ReactComponent as Logo} from './logo.svg';
...
return <Logo/>
from react-native-svg-transformer.
Yes exactly, and that's the problem: it works differently from the changes that you are suggesting. Which means that the default import would return a component in React Native, but a url in Web.
from react-native-svg-transformer.
Yes, but this transformer is not used in any CRA projects, they have their own configuration and transformer, its recommended to use this transformer only with React Native CLI projects, so this modification will influence only React Native projects which are using this transformer and thus adding named export won't break any existing React Native projects because the case with returning default as a string is not used in React Native projects, but this will give the ability to add react-native-web support to existing React Native CLI projects by changing default imports to named imports.
from react-native-svg-transformer.
@vitalyiegorov You can set a custom template for SVGR.
// svgr.config.js const isExportNamedDeclaration = item => item.type === "ExportNamedDeclaration"; const hasExportNamedDeclaration = (items) => { if (items === null) { return false; } if (items instanceof Array) { return !!(items && items.find(isExportNamedDeclaration)) } return isExportNamedDeclaration(items) } const getName = (component) => { if (component instanceof Object) { return component.name; } return component; } const customTemplate = ( { template }, opts, { imports, componentName, props, jsx, exports }, ) => { const ReactComponent = hasExportNamedDeclaration(exports) ? '' : 'export const ReactComponent = ' + getName(componentName); return template.ast` ${imports} function ${componentName}(${props}) { return ${jsx}; } ${ReactComponent} ${exports} ` } module.exports = { template: customTemplate }@kristerkari Maybe it will be useful, so let's add it to the README file.
Can you add this into the root of your project to handle the SVGs for web? Do you need to do anything else?
from react-native-svg-transformer.
Related Issues (20)
- Cannot render SvgURI Component on React Native HOT 3
- SVG files with base64 code are being render properly
- ERROR SVG, [TypeError: Network request failed]
- Paints all svg blue
- Importing SVG image ends up with base64 error in console and the whole site not loads HOT 1
- After adding config code in metro config it give an error HOT 1
- Duplicate __self prop found. HOT 4
- Android fill is strange HOT 1
- Support for React Native 0.72.0+ HOT 1
- Anyone have new metro setup for react native 0.72.0 HOT 1
- [Question] Should additional SVGO plugins work? HOT 1
- Transformation should generate default viewBox property based on SVG width/height
- how to configure metro.config.js in 0.72.0 HOT 18
- After install package react-native-svg-transformer unable to run project HOT 1
- Identifier 'Svg' has already been declared HOT 6
- Import local svg without extension
- TypeError: Cannot read properties of undefined (reading 'has') HOT 3
- svg does not display properly when used with styled-components HOT 2
- Could we add support for optional import svg as a string not a `SvgComponent`? HOT 1
- Fails to find react-native-babel-transformer when used with React Native 0.73 because the package has been renamed HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-svg-transformer.