Skip to content

Commit

Permalink
feat(native): import only relevant components
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Dec 7, 2017
1 parent b53b6cf commit fcd4229
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 46 deletions.
18 changes: 1 addition & 17 deletions src/cli/__snapshots__/index.test.js.snap
Expand Up @@ -43,23 +43,7 @@ export default One;

exports[`cli --native 1`] = `
"import React from \\"react\\";
import Svg, {
Circle,
Ellipse,
G,
LinearGradient,
RadialGradient,
Line,
Path,
Polygon,
Polyline,
Rect,
Symbol,
Text,
Use,
Defs,
Stop
} from \\"react-native-svg\\";
import Svg, { Path } from \\"react-native-svg\\";
const One = props => (
<Svg width={48} height={1} viewBox=\\"0 0 48 1\\" {...props}>
Expand Down
24 changes: 17 additions & 7 deletions src/h2x/toReactNative.js
@@ -1,4 +1,4 @@
const supportedElements = {
const elementToComponent = {
svg: 'Svg',
circle: 'Circle',
ellipse: 'Ellipse',
Expand All @@ -17,18 +17,28 @@ const supportedElements = {
stop: 'Stop',
}

const reverse = Object.keys(supportedElements).reduce(
(map, key) => ({ ...map, [supportedElements[key]]: key }),
const componentToElement = Object.keys(elementToComponent).reduce(
(map, key) => ({ ...map, [elementToComponent[key]]: key }),
{},
)

const toReactNative = () => ({
visitor: {
JSXElement: {
enter(path) {
if (supportedElements[path.node.name]) {
path.node.name = supportedElements[path.node.name]
} else if (!reverse[path.node.name]) {
enter(path, state) {
// Replace element by react-native-svg components
const component = elementToComponent[path.node.name]
if (component) {
path.node.name = component
state.reactNativeSvgReplacedComponents = state.reactNativeSvgReplacedComponents || new Set()
state.reactNativeSvgReplacedComponents.add(component)
return
}

// Remove element if not supported
if (!componentToElement[path.node.name]) {
state.unsupportedComponents = state.unsupportedComponents || new Set()
state.unsupportedComponents.add(component)
path.remove()
}
},
Expand Down
1 change: 0 additions & 1 deletion src/index.js
Expand Up @@ -27,7 +27,6 @@ export {

function expandState(state) {
const componentName = pascalCase(path.parse(state.filePath).name)

return { ...state, componentName }
}

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/h2x.js
@@ -1,3 +1,3 @@
import { transform } from 'h2x-core'

export default (code, opts) => transform(code, opts)
export default (code, opts, state) => transform(code, { ...opts, state })
33 changes: 13 additions & 20 deletions src/transforms/wrapIntoNativeComponent.js
@@ -1,22 +1,15 @@
export default (opts = {}) => (code, state) => `import React from 'react'
import Svg, {
Circle,
Ellipse,
G,
LinearGradient,
RadialGradient,
Line,
Path,
Polygon,
Polyline,
Rect,
Symbol,
Text,
Use,
Defs,
Stop
} from 'react-native-svg';
const componentsToImport = components =>
[...components].filter(component => component !== 'Svg').join(', ')

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

export default ${state.componentName}`
return `import React from 'react'
import Svg, { ${componentsToImport(
reactNativeSvgReplacedComponents,
)} } from 'react-native-svg';
const ${state.componentName} = (${opts.expandProps ? 'props' : ''}) => ${code}
export default ${state.componentName}`
}

0 comments on commit fcd4229

Please sign in to comment.