babel-plugin-react-native-direct-imports 1.2.0 → 1.2.1-rc.15

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 CHANGED
@@ -63,8 +63,104 @@ Then reset the Metro cache once (`npx react-native start --reset-cache` or `npx
63
63
 
64
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
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`.
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
+ #### Setup modules
111
+
112
+ Some entry files run **setup code** when they are loaded, which a direct import alone would skip: `react-native-reanimated` (`initializeReanimatedModule(...)`), `react-native-gesture-handler` (`initialize()` starts the native event receiver), `react-native-skia` (`NativeSetup`), `react-native-screens` (`NativeScreensModule`) and `@sentry/core` 11.x. For these, the plugin ships that code (and only that code, not the re-exports) as a small setup module per version, and imports it first in every file where it rewrites the package:
113
+
114
+ ```js
115
+ import { useSharedValue } from 'react-native-reanimated';
116
+
117
+ // ↓ becomes
118
+
119
+ import 'babel-plugin-react-native-direct-imports/setup/react-native-reanimated/0';
120
+ import { useSharedValue } from 'react-native-reanimated/src/hook/useSharedValue';
121
+ ```
122
+
123
+ ```js
124
+ // babel-plugin-react-native-direct-imports/setup/react-native-reanimated/0.js
125
+ import 'react-native-reanimated/src/publicGlobals';
126
+ import { initializeReanimatedModule } from 'react-native-reanimated/src/initializers';
127
+ import { ReanimatedModule } from 'react-native-reanimated/src/ReanimatedModule/index';
128
+ initializeReanimatedModule(ReanimatedModule);
129
+ ```
130
+
131
+ Modules are cached, so the setup runs once per app. The setup modules are generated from each version's entry file: side-effect imports, top-level calls and the imports they use are kept; exports and declarations (like `react-native-notify-kit`'s `new NotifeeApiModule()`) are not. Packages without a shipped map get no setup module.
132
+
133
+ 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.
134
+
135
+ ### Any other package
136
+
137
+ 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.
138
+
139
+ ### What gets rewritten
140
+
141
+ - 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.
142
+ - `require('pkg')` is rewritten like it is for `react-native`, but all or nothing. These are rewritten:
143
+
144
+ ```js
145
+ const { Svg, Path: P } = require('react-native-svg');
146
+ const RNSvg = require('react-native-svg');
147
+ RNSvg.G; RNSvg['Rect'];
148
+ require('react-native-svg').Circle;
149
+
150
+ // ↓ becomes
151
+
152
+ const Svg = require('react-native-svg/src/elements/Svg').default,
153
+ P = require('react-native-svg/src/elements/Path').default;
154
+ require('react-native-svg/src/elements/G').default; // each usage is a lazy inline require
155
+ require('react-native-svg/src/elements/Rect').default;
156
+ require('react-native-svg/src/elements/Circle').default;
157
+ ```
158
+
159
+ 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).
160
+ - `export * from 'pkg'` is not rewritten.
161
+ - Direct imports skip everything else the barrel does, including its side effects, except the setup of the [packages with a map](#setup-modules).
162
+ - 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.
163
+
68
164
  ## Notes
69
165
 
70
166
  - Unknown names and type-only imports are left untouched.
@@ -80,15 +176,31 @@ Then reset the Metro cache once (`npx react-native start --reset-cache` or `npx
80
176
 
81
177
  ## Generating maps (development)
82
178
 
83
- The map lives in two shapes:
179
+ Each map lives in two shapes:
84
180
 
85
- - [`maps/<version>.json`](maps) — one pretty-printed file per React Native version. This is the committed source of truth, so adding a version stays a small reviewable diff.
86
- - `maps.json` — all versions folded into one file with version ranges and interned values (≈15 KB vs ≈980 KB, and a ≈9 KB npm tarball instead of ≈19 KB). Generated automatically before tests and before packing, gitignored, and the only map that ships.
181
+ - [`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.
182
+ - `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.
87
183
 
88
184
  ```bash
89
- yarn generate-maps # fetch react-native/index.js for all versions -> maps/*.json
90
- yarn generate-maps 0.88.1 # specific versions
91
- yarn build-maps # fold maps/*.json -> maps.json (also runs on test/prepack)
185
+ yarn generate-maps # all packages, all versions -> maps/<package>/*.json
186
+ yarn generate-maps react-native-svg # one package
187
+ yarn generate-maps react-native-svg 15.16.2 # specific versions
188
+ yarn generate-maps 0.88.1 # bare versions are react-native versions
189
+ yarn build-maps # fold maps/**/*.json -> maps.json (also runs on test/prepack)
190
+ ```
191
+
192
+ `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`).
193
+
194
+ To add a package, create `scripts/packages/<name>.js` (`/` in a scoped name becomes `__`) and run `yarn generate-maps <name>`:
195
+
196
+ ```js
197
+ module.exports = {
198
+ name: '@scope/pkg',
199
+ minVersion: '1.0.0', // first stable release to support
200
+ extract: ['src'], // tarball folders to extract (default: src)
201
+ entry: 'src/index.ts', // entry file, when package.json points at a build
202
+ ignoreExports: true, // paths are file locations, ignoring `exports`
203
+ };
92
204
  ```
93
205
 
94
206
  ## License