babel-plugin-react-native-web
Advanced tools
Comparing version 0.3.1 to 0.3.3
{ | ||
"name": "babel-plugin-react-native-web", | ||
"version": "0.3.1", | ||
"version": "0.3.3", | ||
"description": "Babel plugin for React Native for Web", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# babel-plugin-react-native-web | ||
[![npm version][package-badge]][package-url] [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://reactjs.org/docs/how-to-contribute.html#your-first-pull-request) | ||
A Babel plugin that will alias `react-native` to `react-native-web` and exclude | ||
@@ -40,1 +42,4 @@ any modules not required by your app (keeping bundle size down). | ||
``` | ||
[package-badge]: https://img.shields.io/npm/v/babel-plugin-react-native-web.svg?style=flat | ||
[package-url]: https://yarnpkg.com/en/package/babel-plugin-react-native-web |
@@ -34,2 +34,9 @@ const plugin = require('..'); | ||
{ | ||
title: 'require "react-native"', | ||
code: `const ReactNative = require('react-native'); | ||
const { View } = require('react-native'); | ||
const { StyleSheet, TouchableOpacity } = require('react-native');`, | ||
snapshot: true | ||
}, | ||
{ | ||
title: 'require "react-native-web"', | ||
@@ -36,0 +43,0 @@ code: `const ReactNative = require('react-native-web'); |
@@ -11,3 +11,3 @@ const getDistLocation = importName => | ||
return ( | ||
t.isObjectPattern(id) && | ||
(t.isObjectPattern(id) || t.isIdentifier(id)) && | ||
t.isCallExpression(init) && | ||
@@ -88,17 +88,31 @@ t.isIdentifier(init.callee) && | ||
const { id } = path.node.declarations[0]; | ||
const imports = id.properties | ||
.map(identifier => { | ||
const distLocation = getDistLocation(identifier.key.name); | ||
if (distLocation) { | ||
return t.variableDeclaration(path.node.kind, [ | ||
t.variableDeclarator( | ||
t.identifier(identifier.value.name), | ||
t.callExpression(t.identifier('require'), [t.stringLiteral(distLocation)]) | ||
) | ||
]); | ||
} | ||
}) | ||
.filter(Boolean); | ||
if (t.isObjectPattern(id)) { | ||
const imports = id.properties | ||
.map(identifier => { | ||
const distLocation = getDistLocation(identifier.key.name); | ||
if (distLocation) { | ||
return t.variableDeclaration(path.node.kind, [ | ||
t.variableDeclarator( | ||
t.identifier(identifier.value.name), | ||
t.callExpression(t.identifier('require'), [t.stringLiteral(distLocation)]) | ||
) | ||
]); | ||
} | ||
}) | ||
.filter(Boolean); | ||
path.replaceWithMultiple(imports); | ||
path.replaceWithMultiple(imports); | ||
} else if (t.isIdentifier(id)) { | ||
const name = id.name; | ||
const importIndex = t.variableDeclaration(path.node.kind, [ | ||
t.variableDeclarator( | ||
t.identifier(name), | ||
t.callExpression(t.identifier('require'), [ | ||
t.stringLiteral('react-native-web/dist/index') | ||
]) | ||
) | ||
]); | ||
path.replaceWith(importIndex); | ||
} | ||
} | ||
@@ -105,0 +119,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11297
162
45