babel-plugin-react-native-direct-imports 1.0.0 → 1.0.1
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/README.md +4 -10
- package/index.js +15 -7
- package/package.json +1 -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
|
-
//
|
|
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
|
@@ -13,7 +13,10 @@
|
|
|
13
13
|
* '*' -> namespace import (module is used as a whole, no `.default`)
|
|
14
14
|
* other -> named import
|
|
15
15
|
*
|
|
16
|
-
* Unknown names
|
|
16
|
+
* Unknown names, type-only imports and `Platform` are left untouched.
|
|
17
|
+
* `Platform` stays on `react-native` because Metro's inline-platform
|
|
18
|
+
* optimization (Platform.OS / Platform.select dead-code elimination) only
|
|
19
|
+
* recognizes it when imported from 'react-native' directly.
|
|
17
20
|
*
|
|
18
21
|
* Options:
|
|
19
22
|
* reactNativeVersion - picks `maps/<version>.json` (defaults to the latest map)
|
|
@@ -24,6 +27,9 @@ const nodePath = require('path');
|
|
|
24
27
|
|
|
25
28
|
const RN = 'react-native';
|
|
26
29
|
|
|
30
|
+
// Kept on the `react-native` import so Metro can still inline them.
|
|
31
|
+
const KEEP = new Set(['Platform']);
|
|
32
|
+
|
|
27
33
|
const MAPS_DIR = nodePath.join(__dirname, 'maps');
|
|
28
34
|
|
|
29
35
|
const compareVersions = (a, b) => {
|
|
@@ -87,14 +93,16 @@ module.exports = function reactNativeDirectImports(
|
|
|
87
93
|
const direct = [];
|
|
88
94
|
|
|
89
95
|
for (const spec of node.specifiers) {
|
|
90
|
-
const
|
|
96
|
+
const importedName =
|
|
91
97
|
t.isImportSpecifier(spec) &&
|
|
98
|
+
(t.isIdentifier(spec.imported)
|
|
99
|
+
? spec.imported.name
|
|
100
|
+
: spec.imported.value);
|
|
101
|
+
const entry =
|
|
102
|
+
importedName &&
|
|
92
103
|
spec.importKind !== 'type' &&
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
? spec.imported.name
|
|
96
|
-
: spec.imported.value
|
|
97
|
-
];
|
|
104
|
+
!KEEP.has(importedName) &&
|
|
105
|
+
MAP[importedName];
|
|
98
106
|
|
|
99
107
|
if (!entry) {
|
|
100
108
|
kept.push(spec);
|