babel-plugin-react-native-direct-imports 1.2.1-rc.14 → 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 +23 -3
- package/index.js +56 -1
- package/maps.json +1 -1
- package/package.json +3 -2
- package/resolve-package.js +180 -19
- package/setup/@sentry/core/0.js +4 -0
- package/setup/react-native-gesture-handler/0.js +4 -0
- package/setup/react-native-gesture-handler/1.js +5 -0
- package/setup/react-native-reanimated/0.js +6 -0
- package/setup/react-native-screens/0.js +3 -0
- package/setup/react-native-skia/0.js +3 -0
package/README.md
CHANGED
|
@@ -107,8 +107,28 @@ import Stop from 'react-native-svg/src/elements/Stop';
|
|
|
107
107
|
|
|
108
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
109
|
|
|
110
|
-
|
|
111
|
-
|
|
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.
|
|
112
132
|
|
|
113
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.
|
|
114
134
|
|
|
@@ -138,7 +158,7 @@ A package without a shipped map is resolved from the **installed** package inste
|
|
|
138
158
|
|
|
139
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).
|
|
140
160
|
- `export * from 'pkg'` is not rewritten.
|
|
141
|
-
- Direct imports skip everything else the barrel does, including its side effects.
|
|
161
|
+
- Direct imports skip everything else the barrel does, including its side effects, except the setup of the [packages with a map](#setup-modules).
|
|
142
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.
|
|
143
163
|
|
|
144
164
|
## Notes
|
package/index.js
CHANGED
|
@@ -155,6 +155,19 @@ const loadPackageMap = (packageName, version) => {
|
|
|
155
155
|
|
|
156
156
|
const loadMap = (version) => loadPackageMap(RN, version);
|
|
157
157
|
|
|
158
|
+
// Index of the setup module (`setup/<package>/<index>.js`, written by
|
|
159
|
+
// `scripts/build-maps.js`) of `packageName`'s map for `version`, or `null`
|
|
160
|
+
// when its entry runs nothing on load.
|
|
161
|
+
const loadPackageSetup = (packageName, version) => {
|
|
162
|
+
const data = loadData()[packageName];
|
|
163
|
+
const selected =
|
|
164
|
+
version == null ? data.latest : findMapVersion(data.versions, version);
|
|
165
|
+
if (selected === null || !data.setup) return null;
|
|
166
|
+
return pickWindow(data.setup, selected);
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const SELF = require('./package.json').name;
|
|
170
|
+
|
|
158
171
|
module.exports = function reactNativeDirectImports(
|
|
159
172
|
api,
|
|
160
173
|
{
|
|
@@ -321,6 +334,7 @@ module.exports = function reactNativeDirectImports(
|
|
|
321
334
|
if (!expression) return false;
|
|
322
335
|
|
|
323
336
|
memberPath.replaceWith(expression);
|
|
337
|
+
if (pkg !== RN) addSetup(memberPath, state, pkg);
|
|
324
338
|
return true;
|
|
325
339
|
};
|
|
326
340
|
|
|
@@ -447,6 +461,42 @@ module.exports = function reactNativeDirectImports(
|
|
|
447
461
|
return entry ? [`${packageName}/${entry[0]}`, entry[1]] : null;
|
|
448
462
|
};
|
|
449
463
|
|
|
464
|
+
// A direct import skips what the package's entry runs on load
|
|
465
|
+
// (`initializeReanimatedModule(...)`, `initialize()`, ...), so the first
|
|
466
|
+
// rewrite of a package with setup in a file adds its setup module at the
|
|
467
|
+
// top, once per file. Modules are cached, so the setup runs once per app:
|
|
468
|
+
//
|
|
469
|
+
// import 'babel-plugin-react-native-direct-imports/setup/react-native-reanimated/0';
|
|
470
|
+
//
|
|
471
|
+
// ES module files get an `import`, others a `require()`.
|
|
472
|
+
const addSetup = (path, state, pkg) => {
|
|
473
|
+
if (!hasMap(pkg)) return;
|
|
474
|
+
state.setupAdded ??= new Set();
|
|
475
|
+
if (state.setupAdded.has(pkg)) return;
|
|
476
|
+
state.setupAdded.add(pkg);
|
|
477
|
+
|
|
478
|
+
const index = loadPackageSetup(pkg, installedVersion(pkg, dirOf(state)));
|
|
479
|
+
if (index === null) return;
|
|
480
|
+
|
|
481
|
+
const source = t.stringLiteral(`${SELF}/setup/${pkg}/${index}`);
|
|
482
|
+
const program = path.scope.getProgramParent().path;
|
|
483
|
+
const isModule = program.node.body.some(
|
|
484
|
+
(node) =>
|
|
485
|
+
t.isImportDeclaration(node) ||
|
|
486
|
+
t.isExportNamedDeclaration(node) ||
|
|
487
|
+
t.isExportDefaultDeclaration(node) ||
|
|
488
|
+
t.isExportAllDeclaration(node),
|
|
489
|
+
);
|
|
490
|
+
program.unshiftContainer(
|
|
491
|
+
'body',
|
|
492
|
+
isModule
|
|
493
|
+
? t.importDeclaration([], source)
|
|
494
|
+
: t.expressionStatement(
|
|
495
|
+
t.callExpression(t.identifier('require'), [source]),
|
|
496
|
+
),
|
|
497
|
+
);
|
|
498
|
+
};
|
|
499
|
+
|
|
450
500
|
// `require('<path>').<exportName>` for `importedName` of another package,
|
|
451
501
|
// or `null` to keep the original.
|
|
452
502
|
const buildPackageExpression = (pkg, importedName, state) => {
|
|
@@ -525,7 +575,9 @@ module.exports = function reactNativeDirectImports(
|
|
|
525
575
|
declarators.push(t.variableDeclarator(prop.value, expression));
|
|
526
576
|
}
|
|
527
577
|
|
|
528
|
-
if (declarators.length
|
|
578
|
+
if (declarators.length === 0) return;
|
|
579
|
+
addSetup(declaratorPath, state, pkg);
|
|
580
|
+
declaratorPath.replaceWithMultiple(declarators);
|
|
529
581
|
};
|
|
530
582
|
|
|
531
583
|
// An export name as a Babel node (names like `'a-b'` are string literals).
|
|
@@ -562,6 +614,7 @@ module.exports = function reactNativeDirectImports(
|
|
|
562
614
|
}
|
|
563
615
|
|
|
564
616
|
if (direct.length === 0) return;
|
|
617
|
+
addSetup(path, state, node.source.value);
|
|
565
618
|
|
|
566
619
|
if (kept.length > 0) {
|
|
567
620
|
node.specifiers = kept;
|
|
@@ -613,6 +666,7 @@ module.exports = function reactNativeDirectImports(
|
|
|
613
666
|
}
|
|
614
667
|
|
|
615
668
|
if (direct.length === 0) return;
|
|
669
|
+
addSetup(path, state, node.source.value);
|
|
616
670
|
|
|
617
671
|
if (kept.length > 0) {
|
|
618
672
|
node.specifiers = kept;
|
|
@@ -752,3 +806,4 @@ module.exports = function reactNativeDirectImports(
|
|
|
752
806
|
|
|
753
807
|
module.exports.loadMap = loadMap;
|
|
754
808
|
module.exports.loadPackageMap = loadPackageMap;
|
|
809
|
+
module.exports.loadPackageSetup = loadPackageSetup;
|