babel-plugin-react-native-web-tv 0.21.2-tv.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Nicolas Gallagher.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # babel-plugin-react-native-web
2
+
3
+ [![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)
4
+
5
+ A Babel plugin that will alias `react-native` to `react-native-web` and exclude
6
+ any modules not required by your app (keeping bundle size down).
7
+
8
+ In this TV fork, the plugin behavior is unchanged and works for TV exports as well
9
+ (for example `TVFocusGuideView`, `TVEventHandler`, and `useTVEventHandler`) when
10
+ they are imported from `react-native`.
11
+
12
+ ## Installation
13
+
14
+
15
+ ```
16
+ # For the TV fork:
17
+ npm install --save-dev babel-plugin-react-native-web-tv
18
+ # For upstream:
19
+ # npm install --save-dev babel-plugin-react-native-web
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ **.babelrc**
25
+
26
+ ```
27
+ {
28
+ "plugins": [
29
+ ["react-native-web-tv", { commonjs: true }]
30
+ ]
31
+ }
32
+ ```
33
+
34
+ You should configure the plugin to match the module format used by your
35
+ bundler. Most modern bundlers will use a package's ES modules by default (i.e.,
36
+ if `package.json` has a `module` field). But if you need the plugin to rewrite
37
+ import paths to point to CommonJS modules, you must set the `commonjs` option
38
+ to `true`.
39
+
40
+ ## Example
41
+
42
+ NOTE: `react-native-web` internal paths are _not stable_ and you must not rely
43
+ on them. Always use the Babel plugin to optimize your build. What follows is an
44
+ example of the rewrite performed by the plugin.
45
+
46
+ **Before**
47
+
48
+ ```js
49
+ import { StyleSheet, View } from 'react-native';
50
+ ```
51
+
52
+ **After**
53
+
54
+ ```js
55
+ import StyleSheet from 'react-native-web/dist/exports/StyleSheet';
56
+ import View from 'react-native-web/dist/exports/View';
57
+ ```
58
+
59
+ [package-badge]: https://img.shields.io/npm/v/babel-plugin-react-native-web.svg?style=flat
60
+ [package-url]: https://www.npmjs.com/package/babel-plugin-react-native-web
package/index.js ADDED
@@ -0,0 +1,5 @@
1
+ // Allow dynamic package name for publish-time renaming
2
+ const plugin = require('./src');
3
+ // Always rewrite react-native and react-native-web imports to the correct TV fork paths
4
+ plugin.packageName = require('./package.json').name;
5
+ module.exports = plugin;
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "publishConfig": {
3
+ "registry": "https://registry.npmjs.org/"
4
+ },
5
+ "name": "babel-plugin-react-native-web-tv",
6
+ "version": "0.21.2-tv.5",
7
+ "description": "Babel plugin for React Native Web for TV",
8
+ "main": "index.js",
9
+ "devDependencies": {
10
+ "babel-plugin-tester": "^10.1.0"
11
+ },
12
+ "author": "Nicolas Gallagher",
13
+ "license": "MIT",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git://github.com/hps1978/react-native-web-tv.git"
17
+ }
18
+ }
@@ -0,0 +1,141 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`Rewrite react-native to react-native-web export from "react-native": export from "react-native" 1`] = `
4
+
5
+ export { View } from 'react-native';
6
+ export { StyleSheet, Text, unstable_createElement } from 'react-native';
7
+
8
+ ↓ ↓ ↓ ↓ ↓ ↓
9
+
10
+ export { default as View } from 'react-native-web/dist/exports/View';
11
+ export { default as StyleSheet } from 'react-native-web/dist/exports/StyleSheet';
12
+ export { default as Text } from 'react-native-web/dist/exports/Text';
13
+ export { default as unstable_createElement } from 'react-native-web/dist/exports/createElement';
14
+
15
+
16
+ `;
17
+
18
+ exports[`Rewrite react-native to react-native-web export from "react-native-web": export from "react-native-web" 1`] = `
19
+
20
+ export { View } from 'react-native-web';
21
+ export { StyleSheet, Text, unstable_createElement } from 'react-native-web';
22
+
23
+ ↓ ↓ ↓ ↓ ↓ ↓
24
+
25
+ export { default as View } from 'react-native-web/dist/exports/View';
26
+ export { default as StyleSheet } from 'react-native-web/dist/exports/StyleSheet';
27
+ export { default as Text } from 'react-native-web/dist/exports/Text';
28
+ export { default as unstable_createElement } from 'react-native-web/dist/exports/createElement';
29
+
30
+
31
+ `;
32
+
33
+ exports[`Rewrite react-native to react-native-web import from "react-native": import from "react-native" 1`] = `
34
+
35
+ import ReactNative from 'react-native';
36
+ import { View } from 'react-native';
37
+ import { Invalid, View as MyView } from 'react-native';
38
+ import { useLocaleContext } from 'react-native';
39
+ import * as ReactNativeModules from 'react-native';
40
+
41
+ ↓ ↓ ↓ ↓ ↓ ↓
42
+
43
+ import ReactNative from 'react-native-web/dist/index';
44
+ import View from 'react-native-web/dist/exports/View';
45
+ import { Invalid } from 'react-native-web/dist/index';
46
+ import MyView from 'react-native-web/dist/exports/View';
47
+ import useLocaleContext from 'react-native-web/dist/exports/useLocaleContext';
48
+ import * as ReactNativeModules from 'react-native-web/dist/index';
49
+
50
+
51
+ `;
52
+
53
+ exports[`Rewrite react-native to react-native-web import from "react-native": import from "react-native" 2`] = `
54
+
55
+ import ReactNative from 'react-native';
56
+ import { View } from 'react-native';
57
+ import { Invalid, View as MyView } from 'react-native';
58
+ import * as ReactNativeModules from 'react-native';
59
+
60
+ ↓ ↓ ↓ ↓ ↓ ↓
61
+
62
+ import ReactNative from 'react-native-web/dist/cjs/index';
63
+ import View from 'react-native-web/dist/cjs/exports/View';
64
+ import { Invalid } from 'react-native-web/dist/cjs/index';
65
+ import MyView from 'react-native-web/dist/cjs/exports/View';
66
+ import * as ReactNativeModules from 'react-native-web/dist/cjs/index';
67
+
68
+
69
+ `;
70
+
71
+ exports[`Rewrite react-native to react-native-web import from "react-native-web": import from "react-native-web" 1`] = `
72
+
73
+ import { unstable_createElement } from 'react-native-web';
74
+ import { StyleSheet, View, Pressable, processColor } from 'react-native-web';
75
+ import * as ReactNativeModules from 'react-native-web';
76
+
77
+ ↓ ↓ ↓ ↓ ↓ ↓
78
+
79
+ import unstable_createElement from 'react-native-web/dist/exports/createElement';
80
+ import StyleSheet from 'react-native-web/dist/exports/StyleSheet';
81
+ import View from 'react-native-web/dist/exports/View';
82
+ import Pressable from 'react-native-web/dist/exports/Pressable';
83
+ import processColor from 'react-native-web/dist/exports/processColor';
84
+ import * as ReactNativeModules from 'react-native-web/dist/index';
85
+
86
+
87
+ `;
88
+
89
+ exports[`Rewrite react-native to react-native-web require "react-native": require "react-native" 1`] = `
90
+
91
+ const ReactNative = require('react-native');
92
+ const { View } = require('react-native');
93
+ const { StyleSheet, Pressable } = require('react-native');
94
+
95
+ ↓ ↓ ↓ ↓ ↓ ↓
96
+
97
+ const ReactNative = require('react-native-web/dist/index');
98
+ const View = require('react-native-web/dist/exports/View').default;
99
+ const StyleSheet = require('react-native-web/dist/exports/StyleSheet').default;
100
+ const Pressable = require('react-native-web/dist/exports/Pressable').default;
101
+
102
+
103
+ `;
104
+
105
+ exports[`Rewrite react-native to react-native-web require "react-native": require "react-native" 2`] = `
106
+
107
+ const ReactNative = require('react-native');
108
+ const { View } = require('react-native');
109
+ const { StyleSheet, Pressable } = require('react-native');
110
+
111
+ ↓ ↓ ↓ ↓ ↓ ↓
112
+
113
+ const ReactNative = require('react-native-web/dist/cjs/index');
114
+ const View = require('react-native-web/dist/cjs/exports/View').default;
115
+ const StyleSheet =
116
+ require('react-native-web/dist/cjs/exports/StyleSheet').default;
117
+ const Pressable =
118
+ require('react-native-web/dist/cjs/exports/Pressable').default;
119
+
120
+
121
+ `;
122
+
123
+ exports[`Rewrite react-native to react-native-web require "react-native-web": require "react-native-web" 1`] = `
124
+
125
+ const ReactNative = require('react-native-web');
126
+ const { unstable_createElement } = require('react-native-web');
127
+ const { StyleSheet, View, Pressable, processColor } = require('react-native-web');
128
+
129
+ ↓ ↓ ↓ ↓ ↓ ↓
130
+
131
+ const ReactNative = require('react-native-web/dist/index');
132
+ const unstable_createElement =
133
+ require('react-native-web/dist/exports/createElement').default;
134
+ const StyleSheet = require('react-native-web/dist/exports/StyleSheet').default;
135
+ const View = require('react-native-web/dist/exports/View').default;
136
+ const Pressable = require('react-native-web/dist/exports/Pressable').default;
137
+ const processColor =
138
+ require('react-native-web/dist/exports/processColor').default;
139
+
140
+
141
+ `;
@@ -0,0 +1,79 @@
1
+ const plugin = require('..');
2
+ const pluginTester = require('babel-plugin-tester').default;
3
+
4
+ const tests = [
5
+ // import react-native
6
+ {
7
+ title: 'import from "react-native"',
8
+ code: `import ReactNative from 'react-native';
9
+ import { View } from 'react-native';
10
+ import { Invalid, View as MyView } from 'react-native';
11
+ import { useLocaleContext } from 'react-native';
12
+ import * as ReactNativeModules from 'react-native';`,
13
+ snapshot: true
14
+ },
15
+ {
16
+ title: 'import from "react-native"',
17
+ code: `import ReactNative from 'react-native';
18
+ import { View } from 'react-native';
19
+ import { Invalid, View as MyView } from 'react-native';
20
+ import * as ReactNativeModules from 'react-native';`,
21
+ snapshot: true,
22
+ pluginOptions: { commonjs: true }
23
+ },
24
+ {
25
+ title: 'import from "react-native-web"',
26
+ code: `import { unstable_createElement } from 'react-native-web';
27
+ import { StyleSheet, View, Pressable, processColor } from 'react-native-web';
28
+ import * as ReactNativeModules from 'react-native-web';`,
29
+ snapshot: true
30
+ },
31
+ {
32
+ title: 'export from "react-native"',
33
+ code: `export { View } from 'react-native';
34
+ export { StyleSheet, Text, unstable_createElement } from 'react-native';`,
35
+ snapshot: true
36
+ },
37
+ {
38
+ title: 'export from "react-native-web"',
39
+ code: `export { View } from 'react-native-web';
40
+ export { StyleSheet, Text, unstable_createElement } from 'react-native-web';`,
41
+ snapshot: true
42
+ },
43
+ // require react-native
44
+ {
45
+ title: 'require "react-native"',
46
+ code: `const ReactNative = require('react-native');
47
+ const { View } = require('react-native');
48
+ const { StyleSheet, Pressable } = require('react-native');`,
49
+ snapshot: true
50
+ },
51
+ {
52
+ title: 'require "react-native"',
53
+ code: `const ReactNative = require('react-native');
54
+ const { View } = require('react-native');
55
+ const { StyleSheet, Pressable } = require('react-native');`,
56
+ snapshot: true,
57
+ pluginOptions: { commonjs: true }
58
+ },
59
+ {
60
+ title: 'require "react-native-web"',
61
+ code: `const ReactNative = require('react-native-web');
62
+ const { unstable_createElement } = require('react-native-web');
63
+ const { StyleSheet, View, Pressable, processColor } = require('react-native-web');`,
64
+ snapshot: true
65
+ }
66
+ ];
67
+
68
+ pluginTester({
69
+ babelOptions: {
70
+ generatorOpts: {
71
+ jsescOption: {
72
+ quotes: 'single'
73
+ }
74
+ }
75
+ },
76
+ plugin,
77
+ pluginName: 'Rewrite react-native to react-native-web',
78
+ tests
79
+ });
package/src/index.js ADDED
@@ -0,0 +1,150 @@
1
+ const moduleMap = require('./moduleMap');
2
+
3
+ const isCommonJS = (opts) => opts.commonjs === true;
4
+
5
+ const getDistLocation = (importName, opts) => {
6
+ const format = isCommonJS(opts) ? 'cjs/' : '';
7
+ const internalName =
8
+ importName === 'unstable_createElement' ? 'createElement' : importName;
9
+ if (internalName === 'index') {
10
+ return `react-native-web/dist/${format}index`;
11
+ } else if (internalName && moduleMap[internalName]) {
12
+ return `react-native-web/dist/${format}exports/${internalName}`;
13
+ }
14
+ };
15
+
16
+ const isReactNativeRequire = (t, node) => {
17
+ const { declarations } = node;
18
+ if (declarations.length > 1) {
19
+ return false;
20
+ }
21
+ const { id, init } = declarations[0];
22
+ return (
23
+ (t.isObjectPattern(id) || t.isIdentifier(id)) &&
24
+ t.isCallExpression(init) &&
25
+ t.isIdentifier(init.callee) &&
26
+ init.callee.name === 'require' &&
27
+ init.arguments.length === 1 &&
28
+ (init.arguments[0].value === 'react-native' ||
29
+ init.arguments[0].value === 'react-native-web')
30
+ );
31
+ };
32
+
33
+ const isReactNativeModule = ({ source, specifiers }) =>
34
+ source &&
35
+ (source.value === 'react-native' || source.value === 'react-native-web') &&
36
+ specifiers.length;
37
+
38
+ module.exports = function ({ types: t }) {
39
+ return {
40
+ name: 'Rewrite react-native to react-native-web',
41
+ visitor: {
42
+ ImportDeclaration(path, state) {
43
+ const { specifiers } = path.node;
44
+ if (isReactNativeModule(path.node)) {
45
+ const imports = specifiers
46
+ .map((specifier) => {
47
+ if (t.isImportSpecifier(specifier)) {
48
+ const importName = specifier.imported.name;
49
+ const distLocation = getDistLocation(importName, state.opts);
50
+
51
+ if (distLocation) {
52
+ return t.importDeclaration(
53
+ [
54
+ t.importDefaultSpecifier(
55
+ t.identifier(specifier.local.name)
56
+ )
57
+ ],
58
+ t.stringLiteral(distLocation)
59
+ );
60
+ }
61
+ }
62
+ return t.importDeclaration(
63
+ [specifier],
64
+ t.stringLiteral(getDistLocation('index', state.opts))
65
+ );
66
+ })
67
+ .filter(Boolean);
68
+
69
+ path.replaceWithMultiple(imports);
70
+ }
71
+ },
72
+ ExportNamedDeclaration(path, state) {
73
+ const { specifiers } = path.node;
74
+ if (isReactNativeModule(path.node)) {
75
+ const exports = specifiers
76
+ .map((specifier) => {
77
+ if (t.isExportSpecifier(specifier)) {
78
+ const exportName = specifier.exported.name;
79
+ const localName = specifier.local.name;
80
+ const distLocation = getDistLocation(localName, state.opts);
81
+
82
+ if (distLocation) {
83
+ return t.exportNamedDeclaration(
84
+ null,
85
+ [
86
+ t.exportSpecifier(
87
+ t.identifier('default'),
88
+ t.identifier(exportName)
89
+ )
90
+ ],
91
+ t.stringLiteral(distLocation)
92
+ );
93
+ }
94
+ }
95
+ return t.exportNamedDeclaration(
96
+ null,
97
+ [specifier],
98
+ t.stringLiteral(getDistLocation('index', state.opts))
99
+ );
100
+ })
101
+ .filter(Boolean);
102
+
103
+ path.replaceWithMultiple(exports);
104
+ }
105
+ },
106
+ VariableDeclaration(path, state) {
107
+ if (isReactNativeRequire(t, path.node)) {
108
+ const { id } = path.node.declarations[0];
109
+ if (t.isObjectPattern(id)) {
110
+ const imports = id.properties
111
+ .map((identifier) => {
112
+ const distLocation = getDistLocation(
113
+ identifier.key.name,
114
+ state.opts
115
+ );
116
+ if (distLocation) {
117
+ return t.variableDeclaration(path.node.kind, [
118
+ t.variableDeclarator(
119
+ t.identifier(identifier.value.name),
120
+ t.memberExpression(
121
+ t.callExpression(t.identifier('require'), [
122
+ t.stringLiteral(distLocation)
123
+ ]),
124
+ t.identifier('default')
125
+ )
126
+ )
127
+ ]);
128
+ }
129
+ })
130
+ .filter(Boolean);
131
+
132
+ path.replaceWithMultiple(imports);
133
+ } else if (t.isIdentifier(id)) {
134
+ const name = id.name;
135
+ const importIndex = t.variableDeclaration(path.node.kind, [
136
+ t.variableDeclarator(
137
+ t.identifier(name),
138
+ t.callExpression(t.identifier('require'), [
139
+ t.stringLiteral(getDistLocation('index', state.opts))
140
+ ])
141
+ )
142
+ ]);
143
+
144
+ path.replaceWith(importIndex);
145
+ }
146
+ }
147
+ }
148
+ }
149
+ };
150
+ };
@@ -0,0 +1,66 @@
1
+ // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
2
+ module.exports = {
3
+ AccessibilityInfo: true,
4
+ ActivityIndicator: true,
5
+ Alert: true,
6
+ Animated: true,
7
+ AppRegistry: true,
8
+ AppState: true,
9
+ Appearance: true,
10
+ BackHandler: true,
11
+ Button: true,
12
+ CheckBox: true,
13
+ Clipboard: true,
14
+ DeviceEventEmitter: true,
15
+ Dimensions: true,
16
+ Easing: true,
17
+ FlatList: true,
18
+ I18nManager: true,
19
+ Image: true,
20
+ ImageBackground: true,
21
+ InputAccessoryView: true,
22
+ InteractionManager: true,
23
+ Keyboard: true,
24
+ KeyboardAvoidingView: true,
25
+ LayoutAnimation: true,
26
+ Linking: true,
27
+ LogBox: true,
28
+ Modal: true,
29
+ NativeEventEmitter: true,
30
+ NativeModules: true,
31
+ PanResponder: true,
32
+ Picker: true,
33
+ PixelRatio: true,
34
+ Platform: true,
35
+ Pressable: true,
36
+ ProgressBar: true,
37
+ RefreshControl: true,
38
+ SafeAreaView: true,
39
+ ScrollView: true,
40
+ SectionList: true,
41
+ Share: true,
42
+ StatusBar: true,
43
+ StyleSheet: true,
44
+ Switch: true,
45
+ TV: true,
46
+ Text: true,
47
+ TextInput: true,
48
+ Touchable: true,
49
+ TouchableHighlight: true,
50
+ TouchableNativeFeedback: true,
51
+ TouchableOpacity: true,
52
+ TouchableWithoutFeedback: true,
53
+ UIManager: true,
54
+ Vibration: true,
55
+ View: true,
56
+ VirtualizedList: true,
57
+ YellowBox: true,
58
+ createElement: true,
59
+ findNodeHandle: true,
60
+ processColor: true,
61
+ render: true,
62
+ unmountComponentAtNode: true,
63
+ useColorScheme: true,
64
+ useLocaleContext: true,
65
+ useWindowDimensions: true
66
+ };