Skip to content

Commit fcd4229

Browse files
committed
feat(native): import only relevant components
1 parent b53b6cf commit fcd4229

5 files changed

Lines changed: 32 additions & 46 deletions

File tree

src/cli/__snapshots__/index.test.js.snap

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,7 @@ export default One;
4343

4444
exports[`cli --native 1`] = `
4545
"import React from \\"react\\";
46-
import Svg, {
47-
Circle,
48-
Ellipse,
49-
G,
50-
LinearGradient,
51-
RadialGradient,
52-
Line,
53-
Path,
54-
Polygon,
55-
Polyline,
56-
Rect,
57-
Symbol,
58-
Text,
59-
Use,
60-
Defs,
61-
Stop
62-
} from \\"react-native-svg\\";
46+
import Svg, { Path } from \\"react-native-svg\\";
6347
6448
const One = props => (
6549
<Svg width={48} height={1} viewBox=\\"0 0 48 1\\" {...props}>

src/h2x/toReactNative.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const supportedElements = {
1+
const elementToComponent = {
22
svg: 'Svg',
33
circle: 'Circle',
44
ellipse: 'Ellipse',
@@ -17,18 +17,28 @@ const supportedElements = {
1717
stop: 'Stop',
1818
}
1919

20-
const reverse = Object.keys(supportedElements).reduce(
21-
(map, key) => ({ ...map, [supportedElements[key]]: key }),
20+
const componentToElement = Object.keys(elementToComponent).reduce(
21+
(map, key) => ({ ...map, [elementToComponent[key]]: key }),
2222
{},
2323
)
2424

2525
const toReactNative = () => ({
2626
visitor: {
2727
JSXElement: {
28-
enter(path) {
29-
if (supportedElements[path.node.name]) {
30-
path.node.name = supportedElements[path.node.name]
31-
} else if (!reverse[path.node.name]) {
28+
enter(path, state) {
29+
// Replace element by react-native-svg components
30+
const component = elementToComponent[path.node.name]
31+
if (component) {
32+
path.node.name = component
33+
state.reactNativeSvgReplacedComponents = state.reactNativeSvgReplacedComponents || new Set()
34+
state.reactNativeSvgReplacedComponents.add(component)
35+
return
36+
}
37+
38+
// Remove element if not supported
39+
if (!componentToElement[path.node.name]) {
40+
state.unsupportedComponents = state.unsupportedComponents || new Set()
41+
state.unsupportedComponents.add(component)
3242
path.remove()
3343
}
3444
},

src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export {
2727

2828
function expandState(state) {
2929
const componentName = pascalCase(path.parse(state.filePath).name)
30-
3130
return { ...state, componentName }
3231
}
3332

src/plugins/h2x.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { transform } from 'h2x-core'
22

3-
export default (code, opts) => transform(code, opts)
3+
export default (code, opts, state) => transform(code, { ...opts, state })
Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1-
export default (opts = {}) => (code, state) => `import React from 'react'
2-
import Svg, {
3-
Circle,
4-
Ellipse,
5-
G,
6-
LinearGradient,
7-
RadialGradient,
8-
Line,
9-
Path,
10-
Polygon,
11-
Polyline,
12-
Rect,
13-
Symbol,
14-
Text,
15-
Use,
16-
Defs,
17-
Stop
18-
} from 'react-native-svg';
1+
const componentsToImport = components =>
2+
[...components].filter(component => component !== 'Svg').join(', ')
193

20-
const ${state.componentName} = (${opts.expandProps ? 'props' : ''}) => ${code}
4+
export default (opts = {}) => (code, state) => {
5+
const { reactNativeSvgReplacedComponents = new Set() } = state
216

22-
export default ${state.componentName}`
7+
return `import React from 'react'
8+
import Svg, { ${componentsToImport(
9+
reactNativeSvgReplacedComponents,
10+
)} } from 'react-native-svg';
11+
12+
const ${state.componentName} = (${opts.expandProps ? 'props' : ''}) => ${code}
13+
14+
export default ${state.componentName}`
15+
}

0 commit comments

Comments
 (0)