babel-plugin-react-native-direct-imports 1.0.0 → 1.0.2

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.
Files changed (100) hide show
  1. package/README.md +4 -10
  2. package/index.js +41 -25
  3. package/maps/0.70.0.json +65 -65
  4. package/maps/0.70.1.json +65 -65
  5. package/maps/0.70.10.json +65 -65
  6. package/maps/0.70.11.json +65 -65
  7. package/maps/0.70.12.json +65 -65
  8. package/maps/0.70.13.json +65 -65
  9. package/maps/0.70.14.json +65 -65
  10. package/maps/0.70.15.json +65 -65
  11. package/maps/0.70.2.json +65 -65
  12. package/maps/0.70.3.json +65 -65
  13. package/maps/0.70.4.json +65 -65
  14. package/maps/0.70.5.json +65 -65
  15. package/maps/0.70.6.json +65 -65
  16. package/maps/0.70.7.json +65 -65
  17. package/maps/0.70.8.json +65 -65
  18. package/maps/0.70.9.json +65 -65
  19. package/maps/0.71.0.json +58 -58
  20. package/maps/0.71.1.json +58 -58
  21. package/maps/0.71.10.json +58 -58
  22. package/maps/0.71.11.json +58 -58
  23. package/maps/0.71.12.json +58 -58
  24. package/maps/0.71.13.json +58 -58
  25. package/maps/0.71.14.json +58 -58
  26. package/maps/0.71.15.json +58 -58
  27. package/maps/0.71.16.json +58 -58
  28. package/maps/0.71.17.json +58 -58
  29. package/maps/0.71.18.json +58 -58
  30. package/maps/0.71.19.json +58 -58
  31. package/maps/0.71.2.json +58 -58
  32. package/maps/0.71.3.json +58 -58
  33. package/maps/0.71.4.json +58 -58
  34. package/maps/0.71.5.json +58 -58
  35. package/maps/0.71.6.json +58 -58
  36. package/maps/0.71.7.json +58 -58
  37. package/maps/0.71.8.json +58 -58
  38. package/maps/0.71.9.json +58 -58
  39. package/maps/0.72.0.json +47 -47
  40. package/maps/0.72.1.json +47 -47
  41. package/maps/0.72.10.json +47 -47
  42. package/maps/0.72.11.json +47 -47
  43. package/maps/0.72.12.json +47 -47
  44. package/maps/0.72.13.json +47 -47
  45. package/maps/0.72.14.json +47 -47
  46. package/maps/0.72.15.json +47 -47
  47. package/maps/0.72.17.json +47 -47
  48. package/maps/0.72.2.json +47 -47
  49. package/maps/0.72.3.json +47 -47
  50. package/maps/0.72.4.json +47 -47
  51. package/maps/0.72.5.json +47 -47
  52. package/maps/0.72.6.json +47 -47
  53. package/maps/0.72.7.json +47 -47
  54. package/maps/0.72.8.json +47 -47
  55. package/maps/0.72.9.json +47 -47
  56. package/maps/0.73.0.json +47 -47
  57. package/maps/0.73.1.json +47 -47
  58. package/maps/0.73.10.json +47 -47
  59. package/maps/0.73.11.json +47 -47
  60. package/maps/0.73.2.json +47 -47
  61. package/maps/0.73.3.json +47 -47
  62. package/maps/0.73.4.json +47 -47
  63. package/maps/0.73.5.json +47 -47
  64. package/maps/0.73.6.json +47 -47
  65. package/maps/0.73.7.json +47 -47
  66. package/maps/0.73.8.json +47 -47
  67. package/maps/0.73.9.json +47 -47
  68. package/maps/0.74.0.json +44 -44
  69. package/maps/0.74.1.json +44 -44
  70. package/maps/0.74.2.json +44 -44
  71. package/maps/0.74.3.json +44 -44
  72. package/maps/0.74.4.json +44 -44
  73. package/maps/0.74.5.json +44 -44
  74. package/maps/0.74.6.json +44 -44
  75. package/maps/0.74.7.json +44 -44
  76. package/maps/0.75.0.json +44 -44
  77. package/maps/0.75.1.json +44 -44
  78. package/maps/0.75.2.json +44 -44
  79. package/maps/0.75.3.json +44 -44
  80. package/maps/0.75.4.json +44 -44
  81. package/maps/0.75.5.json +44 -44
  82. package/maps/0.76.0.json +43 -43
  83. package/maps/0.76.1.json +43 -43
  84. package/maps/0.76.2.json +43 -43
  85. package/maps/0.76.3.json +43 -43
  86. package/maps/0.76.4.json +43 -43
  87. package/maps/0.76.5.json +43 -43
  88. package/maps/0.76.6.json +43 -43
  89. package/maps/0.76.7.json +43 -43
  90. package/maps/0.76.8.json +43 -43
  91. package/maps/0.76.9.json +43 -43
  92. package/maps/0.77.0.json +44 -44
  93. package/maps/0.77.1.json +44 -44
  94. package/maps/0.77.2.json +44 -44
  95. package/maps/0.77.3.json +44 -44
  96. package/maps/0.78.0.json +44 -44
  97. package/maps/0.78.1.json +44 -44
  98. package/maps/0.78.2.json +44 -44
  99. package/maps/0.78.3.json +44 -44
  100. package/package.json +2 -1
package/README.md CHANGED
@@ -48,13 +48,12 @@ module.exports = {
48
48
  [
49
49
  'module:@react-native/babel-preset',
50
50
  {
51
- // the plugin generates deep imports on purpose
52
- disableDeepImportWarnings: true,
51
+ disableDeepImportWarnings: true, // <-- add this, the plugin generates deep imports on purpose
53
52
  },
54
53
  ],
55
54
  ],
56
55
  plugins: [
57
- // Jest mocks usually target `react-native` as a whole, so skip the plugin in tests
56
+ // Skip the plugin in test environments
58
57
  !isTest && [
59
58
  'react-native-direct-imports',
60
59
  { reactNativeVersion: require('react-native/package.json').version },
@@ -83,6 +82,7 @@ Maps are available for every stable React Native release from `0.70.0` on. The v
83
82
 
84
83
  - Only names listed in the map are rewritten. Unknown names stay on the original `react-native` import.
85
84
  - Type-only imports (`import type { ... }`, `import { type ... }`) are left untouched.
85
+ - `Platform` always stays on the `react-native` import. Metro's `Platform.OS` / `Platform.select` dead-code elimination (`inlinePlatform`) only recognizes `Platform` imported from `'react-native'`.
86
86
  - Aliases are preserved: `import { Text as RNText }` → `import RNText from 'react-native/Libraries/Text/Text'`.
87
87
  - `import * as RN from 'react-native'` throws a build error. Use named imports instead.
88
88
  - The generated paths point to React Native internals (`Libraries/...`, `src/private/...`). They are not public API, which is why every React Native version has its own map.
@@ -93,16 +93,10 @@ Maps are generated from each version's `react-native/index.js`:
93
93
 
94
94
  ```bash
95
95
  yarn generate-maps
96
- ```
97
-
98
- or for specific versions:
99
-
100
- ```bash
96
+ # or for specific versions:
101
97
  yarn generate-maps 0.87.1 0.86.3
102
98
  ```
103
99
 
104
- The script lists all stable `react-native` releases from npm (`>=0.70.0`, no nightlies or prereleases), downloads each `index.js` from jsDelivr, parses its lazy getters and writes `maps/<version>.json`. When a getter returns a whole module (`require('./X')`), the script checks the target file: CommonJS (`module.exports = ...`) maps to a default import, and ESM maps to a namespace import.
105
-
106
100
  ## License
107
101
 
108
102
  MIT
package/index.js CHANGED
@@ -1,17 +1,22 @@
1
1
  /**
2
- * Rewrites named imports from `react-native` to direct imports of the
3
- * underlying modules, so Metro doesn't need to go through the barrel file.
2
+ * Rewrites named imports from `react-native` to direct `require()` calls of
3
+ * the underlying modules, so Metro doesn't need to go through the barrel file.
4
+ * Plain `require()` is used instead of `import` so Babel doesn't add
5
+ * interop helpers (`_interopRequireDefault`, ...) for every module.
4
6
  *
5
7
  * import { View, AppRegistry, Systrace } from 'react-native';
6
8
  * // ->
7
- * import View from 'react-native/Libraries/Components/View/View';
8
- * import { AppRegistry } from 'react-native/Libraries/ReactNative/AppRegistry';
9
- * import * as Systrace from 'react-native/Libraries/Performance/Systrace';
9
+ * const View = require('react-native/Libraries/Components/View/View').default;
10
+ * const AppRegistry = require('react-native/Libraries/ReactNative/AppRegistry').AppRegistry;
11
+ * const Systrace = require('react-native/Libraries/Performance/Systrace');
10
12
  *
11
- * Map value format: [path, exportName]
12
- * 'default' -> default import
13
- * '*' -> namespace import (module is used as a whole, no `.default`)
14
- * other -> named import
13
+ * Map value format: [path, exportName], mirroring the getter in
14
+ * `react-native/index.js`:
15
+ * '*' -> `require(path)` (the getter returns the whole module)
16
+ * other -> `require(path).<exportName>`
17
+ *
18
+ * `Platform` is always required as `require('Platform')`, so Metro's
19
+ * inline-platform optimization (Platform.OS / Platform.select) keeps working.
15
20
  *
16
21
  * Unknown names and type-only imports are left untouched.
17
22
  *
@@ -24,6 +29,8 @@ const nodePath = require('path');
24
29
 
25
30
  const RN = 'react-native';
26
31
 
32
+ const PLATFORM = 'Platform';
33
+
27
34
  const MAPS_DIR = nodePath.join(__dirname, 'maps');
28
35
 
29
36
  const compareVersions = (a, b) => {
@@ -62,10 +69,18 @@ module.exports = function reactNativeDirectImports(
62
69
  ) {
63
70
  const MAP = loadMap(reactNativeVersion);
64
71
 
65
- const buildSpecifier = (exportName, local) => {
66
- if (exportName === 'default') return t.importDefaultSpecifier(local);
67
- if (exportName === '*') return t.importNamespaceSpecifier(local);
68
- return t.importSpecifier(local, t.identifier(exportName));
72
+ const buildRequire = (source, exportName, local) => {
73
+ const call = t.callExpression(t.identifier('require'), [
74
+ t.stringLiteral(source),
75
+ ]);
76
+ return t.variableDeclaration('const', [
77
+ t.variableDeclarator(
78
+ local,
79
+ exportName === '*'
80
+ ? call
81
+ : t.memberExpression(call, t.identifier(exportName)),
82
+ ),
83
+ ]);
69
84
  };
70
85
 
71
86
  return {
@@ -87,14 +102,20 @@ module.exports = function reactNativeDirectImports(
87
102
  const direct = [];
88
103
 
89
104
  for (const spec of node.specifiers) {
90
- const entry =
105
+ const importedName =
91
106
  t.isImportSpecifier(spec) &&
92
107
  spec.importKind !== 'type' &&
93
- MAP[
94
- t.isIdentifier(spec.imported)
95
- ? spec.imported.name
96
- : spec.imported.value
97
- ];
108
+ (t.isIdentifier(spec.imported)
109
+ ? spec.imported.name
110
+ : spec.imported.value);
111
+ const local = t.identifier(spec.local.name);
112
+
113
+ if (importedName === PLATFORM) {
114
+ direct.push(buildRequire(PLATFORM, '*', local));
115
+ continue;
116
+ }
117
+
118
+ const entry = importedName && MAP[importedName];
98
119
 
99
120
  if (!entry) {
100
121
  kept.push(spec);
@@ -102,12 +123,7 @@ module.exports = function reactNativeDirectImports(
102
123
  }
103
124
 
104
125
  const [modulePath, exportName] = entry;
105
- direct.push(
106
- t.importDeclaration(
107
- [buildSpecifier(exportName, t.identifier(spec.local.name))],
108
- t.stringLiteral(`${RN}/${modulePath}`),
109
- ),
110
- );
126
+ direct.push(buildRequire(`${RN}/${modulePath}`, exportName, local));
111
127
  }
112
128
 
113
129
  if (direct.length === 0) return;
package/maps/0.70.0.json CHANGED
@@ -5,35 +5,35 @@
5
5
  ],
6
6
  "ActivityIndicator": [
7
7
  "Libraries/Components/ActivityIndicator/ActivityIndicator",
8
- "default"
8
+ "*"
9
9
  ],
10
10
  "Button": [
11
11
  "Libraries/Components/Button",
12
- "default"
12
+ "*"
13
13
  ],
14
14
  "DatePickerIOS": [
15
15
  "Libraries/Components/DatePicker/DatePickerIOS",
16
- "default"
16
+ "*"
17
17
  ],
18
18
  "DrawerLayoutAndroid": [
19
19
  "Libraries/Components/DrawerAndroid/DrawerLayoutAndroid",
20
- "default"
20
+ "*"
21
21
  ],
22
22
  "FlatList": [
23
23
  "Libraries/Lists/FlatList",
24
- "default"
24
+ "*"
25
25
  ],
26
26
  "Image": [
27
27
  "Libraries/Image/Image",
28
- "default"
28
+ "*"
29
29
  ],
30
30
  "ImageBackground": [
31
31
  "Libraries/Image/ImageBackground",
32
- "default"
32
+ "*"
33
33
  ],
34
34
  "InputAccessoryView": [
35
35
  "Libraries/Components/TextInput/InputAccessoryView",
36
- "default"
36
+ "*"
37
37
  ],
38
38
  "KeyboardAvoidingView": [
39
39
  "Libraries/Components/Keyboard/KeyboardAvoidingView",
@@ -41,11 +41,11 @@
41
41
  ],
42
42
  "MaskedViewIOS": [
43
43
  "Libraries/Components/MaskedView/MaskedViewIOS",
44
- "default"
44
+ "*"
45
45
  ],
46
46
  "Modal": [
47
47
  "Libraries/Modal/Modal",
48
- "default"
48
+ "*"
49
49
  ],
50
50
  "Pressable": [
51
51
  "Libraries/Components/Pressable/Pressable",
@@ -53,15 +53,15 @@
53
53
  ],
54
54
  "ProgressBarAndroid": [
55
55
  "Libraries/Components/ProgressBarAndroid/ProgressBarAndroid",
56
- "default"
56
+ "*"
57
57
  ],
58
58
  "ProgressViewIOS": [
59
59
  "Libraries/Components/ProgressViewIOS/ProgressViewIOS",
60
- "default"
60
+ "*"
61
61
  ],
62
62
  "RefreshControl": [
63
63
  "Libraries/Components/RefreshControl/RefreshControl",
64
- "default"
64
+ "*"
65
65
  ],
66
66
  "SafeAreaView": [
67
67
  "Libraries/Components/SafeAreaView/SafeAreaView",
@@ -69,7 +69,7 @@
69
69
  ],
70
70
  "ScrollView": [
71
71
  "Libraries/Components/ScrollView/ScrollView",
72
- "default"
72
+ "*"
73
73
  ],
74
74
  "SectionList": [
75
75
  "Libraries/Lists/SectionList",
@@ -77,11 +77,11 @@
77
77
  ],
78
78
  "Slider": [
79
79
  "Libraries/Components/Slider/Slider",
80
- "default"
80
+ "*"
81
81
  ],
82
82
  "StatusBar": [
83
83
  "Libraries/Components/StatusBar/StatusBar",
84
- "default"
84
+ "*"
85
85
  ],
86
86
  "Switch": [
87
87
  "Libraries/Components/Switch/Switch",
@@ -89,95 +89,95 @@
89
89
  ],
90
90
  "Text": [
91
91
  "Libraries/Text/Text",
92
- "default"
92
+ "*"
93
93
  ],
94
94
  "TextInput": [
95
95
  "Libraries/Components/TextInput/TextInput",
96
- "default"
96
+ "*"
97
97
  ],
98
98
  "Touchable": [
99
99
  "Libraries/Components/Touchable/Touchable",
100
- "default"
100
+ "*"
101
101
  ],
102
102
  "TouchableHighlight": [
103
103
  "Libraries/Components/Touchable/TouchableHighlight",
104
- "default"
104
+ "*"
105
105
  ],
106
106
  "TouchableNativeFeedback": [
107
107
  "Libraries/Components/Touchable/TouchableNativeFeedback",
108
- "default"
108
+ "*"
109
109
  ],
110
110
  "TouchableOpacity": [
111
111
  "Libraries/Components/Touchable/TouchableOpacity",
112
- "default"
112
+ "*"
113
113
  ],
114
114
  "TouchableWithoutFeedback": [
115
115
  "Libraries/Components/Touchable/TouchableWithoutFeedback",
116
- "default"
116
+ "*"
117
117
  ],
118
118
  "View": [
119
119
  "Libraries/Components/View/View",
120
- "default"
120
+ "*"
121
121
  ],
122
122
  "VirtualizedList": [
123
123
  "Libraries/Lists/VirtualizedList",
124
- "default"
124
+ "*"
125
125
  ],
126
126
  "VirtualizedSectionList": [
127
127
  "Libraries/Lists/VirtualizedSectionList",
128
- "default"
128
+ "*"
129
129
  ],
130
130
  "ActionSheetIOS": [
131
131
  "Libraries/ActionSheetIOS/ActionSheetIOS",
132
- "default"
132
+ "*"
133
133
  ],
134
134
  "Alert": [
135
135
  "Libraries/Alert/Alert",
136
- "default"
136
+ "*"
137
137
  ],
138
138
  "Animated": [
139
139
  "Libraries/Animated/Animated",
140
- "default"
140
+ "*"
141
141
  ],
142
142
  "Appearance": [
143
143
  "Libraries/Utilities/Appearance",
144
- "default"
144
+ "*"
145
145
  ],
146
146
  "AppRegistry": [
147
147
  "Libraries/ReactNative/AppRegistry",
148
- "default"
148
+ "*"
149
149
  ],
150
150
  "AppState": [
151
151
  "Libraries/AppState/AppState",
152
- "default"
152
+ "*"
153
153
  ],
154
154
  "AsyncStorage": [
155
155
  "Libraries/Storage/AsyncStorage",
156
- "default"
156
+ "*"
157
157
  ],
158
158
  "BackHandler": [
159
159
  "Libraries/Utilities/BackHandler",
160
- "default"
160
+ "*"
161
161
  ],
162
162
  "Clipboard": [
163
163
  "Libraries/Components/Clipboard/Clipboard",
164
- "default"
164
+ "*"
165
165
  ],
166
166
  "DeviceInfo": [
167
167
  "Libraries/Utilities/DeviceInfo",
168
- "default"
168
+ "*"
169
169
  ],
170
170
  "DevSettings": [
171
171
  "Libraries/Utilities/DevSettings",
172
- "default"
172
+ "*"
173
173
  ],
174
174
  "Dimensions": [
175
175
  "Libraries/Utilities/Dimensions",
176
- "default"
176
+ "*"
177
177
  ],
178
178
  "Easing": [
179
179
  "Libraries/Animated/Easing",
180
- "default"
180
+ "*"
181
181
  ],
182
182
  "findNodeHandle": [
183
183
  "Libraries/Renderer/shims/ReactNative",
@@ -185,31 +185,31 @@
185
185
  ],
186
186
  "I18nManager": [
187
187
  "Libraries/ReactNative/I18nManager",
188
- "default"
188
+ "*"
189
189
  ],
190
190
  "ImagePickerIOS": [
191
191
  "Libraries/Image/ImagePickerIOS",
192
- "default"
192
+ "*"
193
193
  ],
194
194
  "InteractionManager": [
195
195
  "Libraries/Interaction/InteractionManager",
196
- "default"
196
+ "*"
197
197
  ],
198
198
  "Keyboard": [
199
199
  "Libraries/Components/Keyboard/Keyboard",
200
- "default"
200
+ "*"
201
201
  ],
202
202
  "LayoutAnimation": [
203
203
  "Libraries/LayoutAnimation/LayoutAnimation",
204
- "default"
204
+ "*"
205
205
  ],
206
206
  "Linking": [
207
207
  "Libraries/Linking/Linking",
208
- "default"
208
+ "*"
209
209
  ],
210
210
  "LogBox": [
211
211
  "Libraries/LogBox/LogBox",
212
- "default"
212
+ "*"
213
213
  ],
214
214
  "NativeDialogManagerAndroid": [
215
215
  "Libraries/NativeModules/specs/NativeDialogManagerAndroid",
@@ -221,43 +221,43 @@
221
221
  ],
222
222
  "Networking": [
223
223
  "Libraries/Network/RCTNetworking",
224
- "default"
224
+ "*"
225
225
  ],
226
226
  "PanResponder": [
227
227
  "Libraries/Interaction/PanResponder",
228
- "default"
228
+ "*"
229
229
  ],
230
230
  "PermissionsAndroid": [
231
231
  "Libraries/PermissionsAndroid/PermissionsAndroid",
232
- "default"
232
+ "*"
233
233
  ],
234
234
  "PixelRatio": [
235
235
  "Libraries/Utilities/PixelRatio",
236
- "default"
236
+ "*"
237
237
  ],
238
238
  "PushNotificationIOS": [
239
239
  "Libraries/PushNotificationIOS/PushNotificationIOS",
240
- "default"
240
+ "*"
241
241
  ],
242
242
  "Settings": [
243
243
  "Libraries/Settings/Settings",
244
- "default"
244
+ "*"
245
245
  ],
246
246
  "Share": [
247
247
  "Libraries/Share/Share",
248
- "default"
248
+ "*"
249
249
  ],
250
250
  "StyleSheet": [
251
251
  "Libraries/StyleSheet/StyleSheet",
252
- "default"
252
+ "*"
253
253
  ],
254
254
  "Systrace": [
255
255
  "Libraries/Performance/Systrace",
256
- "default"
256
+ "*"
257
257
  ],
258
258
  "ToastAndroid": [
259
259
  "Libraries/Components/ToastAndroid/ToastAndroid",
260
- "default"
260
+ "*"
261
261
  ],
262
262
  "TurboModuleRegistry": [
263
263
  "Libraries/TurboModule/TurboModuleRegistry",
@@ -265,7 +265,7 @@
265
265
  ],
266
266
  "UIManager": [
267
267
  "Libraries/ReactNative/UIManager",
268
- "default"
268
+ "*"
269
269
  ],
270
270
  "unstable_batchedUpdates": [
271
271
  "Libraries/Renderer/shims/ReactNative",
@@ -281,15 +281,15 @@
281
281
  ],
282
282
  "UTFSequence": [
283
283
  "Libraries/UTFSequence",
284
- "default"
284
+ "*"
285
285
  ],
286
286
  "Vibration": [
287
287
  "Libraries/Vibration/Vibration",
288
- "default"
288
+ "*"
289
289
  ],
290
290
  "YellowBox": [
291
291
  "Libraries/YellowBox/YellowBoxDeprecated",
292
- "default"
292
+ "*"
293
293
  ],
294
294
  "DeviceEventEmitter": [
295
295
  "Libraries/EventEmitter/RCTDeviceEventEmitter",
@@ -301,15 +301,15 @@
301
301
  ],
302
302
  "NativeAppEventEmitter": [
303
303
  "Libraries/EventEmitter/RCTNativeAppEventEmitter",
304
- "default"
304
+ "*"
305
305
  ],
306
306
  "NativeModules": [
307
307
  "Libraries/BatchedBridge/NativeModules",
308
- "default"
308
+ "*"
309
309
  ],
310
310
  "Platform": [
311
311
  "Libraries/Utilities/Platform",
312
- "default"
312
+ "*"
313
313
  ],
314
314
  "PlatformColor": [
315
315
  "Libraries/StyleSheet/PlatformColorValueTypes",
@@ -317,11 +317,11 @@
317
317
  ],
318
318
  "processColor": [
319
319
  "Libraries/StyleSheet/processColor",
320
- "default"
320
+ "*"
321
321
  ],
322
322
  "requireNativeComponent": [
323
323
  "Libraries/ReactNative/requireNativeComponent",
324
- "default"
324
+ "*"
325
325
  ],
326
326
  "RootTagContext": [
327
327
  "Libraries/ReactNative/RootTag",