babel-plugin-react-native-direct-imports 1.1.2 → 1.2.1-rc.14
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 +102 -4
- package/index.js +398 -49
- package/maps.json +1 -0
- package/package.json +8 -5
- package/resolve-package.js +455 -0
- package/CHANGELOG.md +0 -13
- package/maps/0.70.0.json +0 -330
- package/maps/0.70.1.json +0 -330
- package/maps/0.70.10.json +0 -330
- package/maps/0.70.11.json +0 -330
- package/maps/0.70.12.json +0 -330
- package/maps/0.70.13.json +0 -330
- package/maps/0.70.14.json +0 -330
- package/maps/0.70.15.json +0 -330
- package/maps/0.70.2.json +0 -330
- package/maps/0.70.3.json +0 -330
- package/maps/0.70.4.json +0 -330
- package/maps/0.70.5.json +0 -330
- package/maps/0.70.6.json +0 -330
- package/maps/0.70.7.json +0 -330
- package/maps/0.70.8.json +0 -330
- package/maps/0.70.9.json +0 -330
- package/maps/0.71.0.json +0 -338
- package/maps/0.71.1.json +0 -338
- package/maps/0.71.10.json +0 -338
- package/maps/0.71.11.json +0 -338
- package/maps/0.71.12.json +0 -338
- package/maps/0.71.13.json +0 -338
- package/maps/0.71.14.json +0 -338
- package/maps/0.71.15.json +0 -338
- package/maps/0.71.16.json +0 -338
- package/maps/0.71.17.json +0 -338
- package/maps/0.71.18.json +0 -338
- package/maps/0.71.19.json +0 -338
- package/maps/0.71.2.json +0 -338
- package/maps/0.71.3.json +0 -338
- package/maps/0.71.4.json +0 -338
- package/maps/0.71.5.json +0 -338
- package/maps/0.71.6.json +0 -338
- package/maps/0.71.7.json +0 -338
- package/maps/0.71.8.json +0 -338
- package/maps/0.71.9.json +0 -338
- package/maps/0.72.0.json +0 -326
- package/maps/0.72.1.json +0 -326
- package/maps/0.72.10.json +0 -326
- package/maps/0.72.11.json +0 -326
- package/maps/0.72.12.json +0 -326
- package/maps/0.72.13.json +0 -326
- package/maps/0.72.14.json +0 -326
- package/maps/0.72.15.json +0 -326
- package/maps/0.72.17.json +0 -326
- package/maps/0.72.2.json +0 -326
- package/maps/0.72.3.json +0 -326
- package/maps/0.72.4.json +0 -326
- package/maps/0.72.5.json +0 -326
- package/maps/0.72.6.json +0 -326
- package/maps/0.72.7.json +0 -326
- package/maps/0.72.8.json +0 -326
- package/maps/0.72.9.json +0 -326
- package/maps/0.73.0.json +0 -326
- package/maps/0.73.1.json +0 -326
- package/maps/0.73.10.json +0 -326
- package/maps/0.73.11.json +0 -326
- package/maps/0.73.2.json +0 -326
- package/maps/0.73.3.json +0 -326
- package/maps/0.73.4.json +0 -326
- package/maps/0.73.5.json +0 -326
- package/maps/0.73.6.json +0 -326
- package/maps/0.73.7.json +0 -326
- package/maps/0.73.8.json +0 -326
- package/maps/0.73.9.json +0 -326
- package/maps/0.74.0.json +0 -314
- package/maps/0.74.1.json +0 -314
- package/maps/0.74.2.json +0 -314
- package/maps/0.74.3.json +0 -314
- package/maps/0.74.4.json +0 -314
- package/maps/0.74.5.json +0 -314
- package/maps/0.74.6.json +0 -314
- package/maps/0.74.7.json +0 -314
- package/maps/0.75.0.json +0 -314
- package/maps/0.75.1.json +0 -314
- package/maps/0.75.2.json +0 -314
- package/maps/0.75.3.json +0 -314
- package/maps/0.75.4.json +0 -314
- package/maps/0.75.5.json +0 -314
- package/maps/0.76.0.json +0 -314
- package/maps/0.76.1.json +0 -314
- package/maps/0.76.2.json +0 -314
- package/maps/0.76.3.json +0 -314
- package/maps/0.76.4.json +0 -314
- package/maps/0.76.5.json +0 -314
- package/maps/0.76.6.json +0 -314
- package/maps/0.76.7.json +0 -314
- package/maps/0.76.8.json +0 -314
- package/maps/0.76.9.json +0 -314
- package/maps/0.77.0.json +0 -318
- package/maps/0.77.1.json +0 -318
- package/maps/0.77.2.json +0 -318
- package/maps/0.77.3.json +0 -318
- package/maps/0.78.0.json +0 -322
- package/maps/0.78.1.json +0 -322
- package/maps/0.78.2.json +0 -322
- package/maps/0.78.3.json +0 -322
- package/maps/0.79.0.json +0 -318
- package/maps/0.79.1.json +0 -318
- package/maps/0.79.2.json +0 -318
- package/maps/0.79.3.json +0 -318
- package/maps/0.79.4.json +0 -318
- package/maps/0.79.5.json +0 -318
- package/maps/0.79.6.json +0 -318
- package/maps/0.79.7.json +0 -318
- package/maps/0.80.0.json +0 -326
- package/maps/0.80.1.json +0 -326
- package/maps/0.80.2.json +0 -326
- package/maps/0.80.3.json +0 -326
- package/maps/0.81.0.json +0 -338
- package/maps/0.81.1.json +0 -338
- package/maps/0.81.2.json +0 -338
- package/maps/0.81.3.json +0 -338
- package/maps/0.81.4.json +0 -338
- package/maps/0.81.5.json +0 -338
- package/maps/0.81.6.json +0 -338
- package/maps/0.82.0.json +0 -346
- package/maps/0.82.1.json +0 -346
- package/maps/0.83.0.json +0 -354
- package/maps/0.83.1.json +0 -354
- package/maps/0.83.10.json +0 -354
- package/maps/0.83.2.json +0 -354
- package/maps/0.83.3.json +0 -354
- package/maps/0.83.4.json +0 -354
- package/maps/0.83.5.json +0 -354
- package/maps/0.83.6.json +0 -354
- package/maps/0.83.7.json +0 -354
- package/maps/0.83.8.json +0 -354
- package/maps/0.83.9.json +0 -354
- package/maps/0.84.0.json +0 -358
- package/maps/0.84.1.json +0 -358
- package/maps/0.85.0.json +0 -370
- package/maps/0.85.1.json +0 -370
- package/maps/0.85.2.json +0 -370
- package/maps/0.85.3.json +0 -370
- package/maps/0.86.0.json +0 -394
- package/maps/0.86.2.json +0 -394
- package/maps/0.86.3.json +0 -394
- package/maps/0.87.0.json +0 -390
- package/maps/0.87.1.json +0 -390
- package/maps/0.88.0.json +0 -390
- package/maps/0.88.1.json +0 -390
package/README.md
CHANGED
|
@@ -61,17 +61,93 @@ module.exports = {
|
|
|
61
61
|
|
|
62
62
|
Then reset the Metro cache once (`npx react-native start --reset-cache` or `npx expo start --clear`).
|
|
63
63
|
|
|
64
|
-
`reactNativeVersion` picks the
|
|
64
|
+
`reactNativeVersion` picks the React Native version to generate imports for (default: the latest known). An unknown version falls back to the closest older map — a `0.88.2` or `0.89.0` app keeps building with the `0.88.1` map — so upgrading React Native no longer breaks the build, though updating the plugin still gets you the exact map. Only a version older than everything the plugin knows (`< 0.70.0`) fails.
|
|
65
|
+
|
|
66
|
+
`packages` (default: `[]`) lists other packages to rewrite imports of, see [Other packages](#other-packages).
|
|
67
|
+
|
|
68
|
+
`onModuleReexport` (default: `'error'`) is what happens on `module.exports = require('react-native')` (or of a listed package), which re-exports the whole package and keeps all of it in the bundle: `'error'` fails the build, `'warn'` logs a warning and leaves the code as it is, `'silent'` just leaves it. For listed packages it also covers `const x = require('pkg'); module.exports = x`.
|
|
65
69
|
|
|
66
70
|
`silenceBatchedUpdatesWarning` (default: `false`) hides the warning logged once when `unstable_batchedUpdates` is imported on React Native 0.86+, where it [does nothing](https://github.com/react/react-native/commit/1a623a826654db4238cddc22fb958ccb76207a74) and just calls the callback.
|
|
67
71
|
|
|
72
|
+
## Other packages
|
|
73
|
+
|
|
74
|
+
The same idea works for other packages. List them in `packages`:
|
|
75
|
+
|
|
76
|
+
```js
|
|
77
|
+
['react-native-direct-imports', { packages: ['react-native-svg'] }];
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
import Svg, { Defs, LinearGradient, Path, Rect, Stop } from 'react-native-svg';
|
|
82
|
+
|
|
83
|
+
// ↓ becomes
|
|
84
|
+
|
|
85
|
+
import Svg from 'react-native-svg/src/elements/Svg';
|
|
86
|
+
import Defs from 'react-native-svg/src/elements/Defs';
|
|
87
|
+
import LinearGradient from 'react-native-svg/src/elements/LinearGradient';
|
|
88
|
+
import Path from 'react-native-svg/src/elements/Path';
|
|
89
|
+
import Rect from 'react-native-svg/src/elements/Rect';
|
|
90
|
+
import Stop from 'react-native-svg/src/elements/Stop';
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Packages with a map
|
|
94
|
+
|
|
95
|
+
| Package | Versions | Rewritten paths |
|
|
96
|
+
| --- | --- | --- |
|
|
97
|
+
| [`react-native-svg`](https://github.com/software-mansion/react-native-svg) | `15.0.0`+ | `src/...` |
|
|
98
|
+
| [`react-native-reanimated`](https://github.com/software-mansion/react-native-reanimated) | `4.0.0`+ | `src/...` |
|
|
99
|
+
| [`react-native-gesture-handler`](https://github.com/software-mansion/react-native-gesture-handler) | `2.0.0`+ | `src/...` |
|
|
100
|
+
| [`react-native-screens`](https://github.com/software-mansion/react-native-screens) | `4.0.0`+ | `src/...` |
|
|
101
|
+
| [`react-native-skia`](https://github.com/wcandillon/react-native-skia) | `3.0.0`+ | `src/...` |
|
|
102
|
+
| [`react-native-keyboard-controller`](https://github.com/kirillzyusko/react-native-keyboard-controller) | `1.20.0`+ | `src/...` |
|
|
103
|
+
| [`react-native-notify-kit`](https://www.npmjs.com/package/react-native-notify-kit) | `10.0.0`+ | `src/...` |
|
|
104
|
+
| [`@gorhom/bottom-sheet`](https://github.com/gorhom/react-native-bottom-sheet) | `5.0.0`+ | `src/...` |
|
|
105
|
+
| [`@sentry/core`](https://github.com/getsentry/sentry-javascript) | `10.0.0`+ | `build/esm/...` |
|
|
106
|
+
| [`@sentry/react-native`](https://github.com/getsentry/sentry-react-native) | `7.0.0`+ | `dist/js/...` |
|
|
107
|
+
|
|
108
|
+
Add the ones you use to `packages`. The deep paths of the `@sentry/*` packages aren't listed in their [`exports`](https://nodejs.org/api/packages.html#exports) field, so Metro needs package exports off (or a `resolveRequest` that allows them) to resolve them.
|
|
109
|
+
|
|
110
|
+
> [!WARNING]
|
|
111
|
+
> A direct import skips everything the package's entry file does when it is loaded. For some of these packages that is **setup code**: `react-native-reanimated` (`initializeReanimatedModule`), `react-native-gesture-handler` (`initialize()` starts the native event receiver), `react-native-skia` (`NativeSetup`) and `react-native-screens` (`NativeScreensModule`). If nothing else in your app loads the package root, that setup never runs. Keep one plain `import 'react-native-reanimated'`-style import (or an import the plugin can't rewrite) in your app's entry file, and test on a device after enabling these.
|
|
112
|
+
|
|
113
|
+
Like `react-native`, these ship a map per version. The version is read from the **installed** package, so there is nothing to configure: an unknown version falls back to the closest older map (a `15.99.0` install uses the `15.16.2` map). A version older than the first map (`< 15.0.0` for `react-native-svg`) throws a build error.
|
|
114
|
+
|
|
115
|
+
### Any other package
|
|
116
|
+
|
|
117
|
+
A package without a shipped map is resolved from the **installed** package instead: the plugin picks its entry file from the package's `package.json` (`exports["."]`, then `react-native`, `module` and `main`; unlike Metro it prefers `module`, so it can read an ES build when `main` is CommonJS) and follows the re-exports (`export * from`, `export { default as X } from`, `import X from '...'; export { X }`, ...) to the file that defines each name. Only the files on the way to the names you import are parsed, once per process. A package that isn't installed, or whose entry has no ES module exports (a CommonJS-only build), throws a build error instead of silently doing nothing.
|
|
118
|
+
|
|
119
|
+
### What gets rewritten
|
|
120
|
+
|
|
121
|
+
- Default and named imports and `export { ... } from 'pkg'` re-exports are rewritten; each one becomes its own `import` / `export ... from`. Namespace imports, type imports and names that aren't in the map (or can't be traced to a file inside the package) stay as they are.
|
|
122
|
+
- `require('pkg')` is rewritten like it is for `react-native`, but all or nothing. These are rewritten:
|
|
123
|
+
|
|
124
|
+
```js
|
|
125
|
+
const { Svg, Path: P } = require('react-native-svg');
|
|
126
|
+
const RNSvg = require('react-native-svg');
|
|
127
|
+
RNSvg.G; RNSvg['Rect'];
|
|
128
|
+
require('react-native-svg').Circle;
|
|
129
|
+
|
|
130
|
+
// ↓ becomes
|
|
131
|
+
|
|
132
|
+
const Svg = require('react-native-svg/src/elements/Svg').default,
|
|
133
|
+
P = require('react-native-svg/src/elements/Path').default;
|
|
134
|
+
require('react-native-svg/src/elements/G').default; // each usage is a lazy inline require
|
|
135
|
+
require('react-native-svg/src/elements/Rect').default;
|
|
136
|
+
require('react-native-svg/src/elements/Circle').default;
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
If a binding is used any other way, the whole `require` stays as written, because the whole package is loaded through it anyway: `fn(RNSvg)`, `const x = RNSvg`, `RNSvg.Unknown`, `RNSvg.G = ...`, `delete RNSvg.G`, `{ ...RNSvg }`, `Object.keys(RNSvg)`, `RNSvg[name]`, `export const RNSvg = require(...)`, `{ Svg, ...rest } = require(...)`, `{ Svg = fallback } = require(...)`. `module.exports = require('pkg')` and `module.exports = RNSvg` follow [`onModuleReexport`](#usage).
|
|
140
|
+
- `export * from 'pkg'` is not rewritten.
|
|
141
|
+
- Direct imports skip everything else the barrel does, including its side effects.
|
|
142
|
+
- Packages with an [`exports`](https://nodejs.org/api/packages.html#exports) field are resolved through it: the entry is `exports["."]` (conditions `react-native`, `import`, `module`, `default`), and each import gets the public subpath its file is exposed as (`"./shapes/*": "./src/elements/*.ts"` turns `src/elements/Circle.ts` into `pkg/shapes/Circle`). A file that `exports` doesn't expose can't be imported from outside, so that import stays as it is.
|
|
143
|
+
|
|
68
144
|
## Notes
|
|
69
145
|
|
|
70
146
|
- Unknown names and type-only imports are left untouched.
|
|
71
147
|
- Re-exports (`export { X } from 'react-native'`) and `require('react-native')` in compiled CommonJS code (e.g. in `node_modules`) are rewritten too.
|
|
72
148
|
- `Platform` stays a local `Platform` variable, so Metro's `Platform.OS` / `Platform.select` inlining keeps working (not for aliases like `Platform as P`).
|
|
73
149
|
- `import * as RN from 'react-native'` and `module.exports = require('react-native')` throw a build error.
|
|
74
|
-
- Generated paths point to React Native internals, which is why
|
|
150
|
+
- Generated paths point to React Native internals, which is why the map is per version.
|
|
75
151
|
|
|
76
152
|
## Compatibility
|
|
77
153
|
|
|
@@ -80,9 +156,31 @@ Then reset the Metro cache once (`npx react-native start --reset-cache` or `npx
|
|
|
80
156
|
|
|
81
157
|
## Generating maps (development)
|
|
82
158
|
|
|
159
|
+
Each map lives in two shapes:
|
|
160
|
+
|
|
161
|
+
- [`maps/<package>/<version>.json`](maps) — one pretty-printed file per package version (`maps/@scope/<name>/` for scoped packages). This is the committed source of truth, so adding a version stays a small reviewable diff.
|
|
162
|
+
- `maps.json` — all packages and versions folded into one file with version ranges and interned values (≈140 KB for everything). Generated automatically before tests and before packing, gitignored, and the only map that ships.
|
|
163
|
+
|
|
83
164
|
```bash
|
|
84
|
-
yarn generate-maps
|
|
85
|
-
yarn generate-maps
|
|
165
|
+
yarn generate-maps # all packages, all versions -> maps/<package>/*.json
|
|
166
|
+
yarn generate-maps react-native-svg # one package
|
|
167
|
+
yarn generate-maps react-native-svg 15.16.2 # specific versions
|
|
168
|
+
yarn generate-maps 0.88.1 # bare versions are react-native versions
|
|
169
|
+
yarn build-maps # fold maps/**/*.json -> maps.json (also runs on test/prepack)
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
`react-native` maps are read from the lazy getters of `react-native/index.js`. Every other package's maps are generated by downloading each published tarball and running the same resolver the plugin uses for unmapped packages over every name the entry exports (needs `tar`).
|
|
173
|
+
|
|
174
|
+
To add a package, create `scripts/packages/<name>.js` (`/` in a scoped name becomes `__`) and run `yarn generate-maps <name>`:
|
|
175
|
+
|
|
176
|
+
```js
|
|
177
|
+
module.exports = {
|
|
178
|
+
name: '@scope/pkg',
|
|
179
|
+
minVersion: '1.0.0', // first stable release to support
|
|
180
|
+
extract: ['src'], // tarball folders to extract (default: src)
|
|
181
|
+
entry: 'src/index.ts', // entry file, when package.json points at a build
|
|
182
|
+
ignoreExports: true, // paths are file locations, ignoring `exports`
|
|
183
|
+
};
|
|
86
184
|
```
|
|
87
185
|
|
|
88
186
|
## License
|