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 +118 -6
- package/index.js +406 -42
- package/maps.json +1 -1
- package/package.json +5 -3
- package/resolve-package.js +616 -0
- 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/CHANGELOG.md +0 -21
package/index.js
CHANGED
|
@@ -37,15 +37,38 @@
|
|
|
37
37
|
*
|
|
38
38
|
* Unknown names and type-only imports/exports are left untouched.
|
|
39
39
|
*
|
|
40
|
+
* Other packages can be opted in with the `packages` option. Packages with a
|
|
41
|
+
* shipped map (`react-native-svg`) use the map of the installed version; any
|
|
42
|
+
* other package is resolved by following the ES re-exports of its installed
|
|
43
|
+
* entry file (see `resolve-package.js`). They stay `import` declarations:
|
|
44
|
+
*
|
|
45
|
+
* import Svg, { Defs, Path } from 'react-native-svg';
|
|
46
|
+
* // ->
|
|
47
|
+
* import Svg from 'react-native-svg/src/elements/Svg';
|
|
48
|
+
* import Defs from 'react-native-svg/src/elements/Defs';
|
|
49
|
+
* import Path from 'react-native-svg/src/elements/Path';
|
|
50
|
+
*
|
|
51
|
+
* `export { Defs } from 'pkg'` re-exports are rewritten the same way, and so is
|
|
52
|
+
* `require('pkg')`, all or nothing: when every usage of
|
|
53
|
+
* `const RNSvg = require('pkg')` is a known `RNSvg.Name` they all become inline
|
|
54
|
+
* `require()`s, but `fn(RNSvg)`, `RNSvg.Unknown`, `RNSvg.Name = ...`, ... keep
|
|
55
|
+
* the whole `require('pkg')` as it is, since it would be loaded anyway.
|
|
56
|
+
*
|
|
40
57
|
* Options:
|
|
41
58
|
* reactNativeVersion - which react-native version to generate imports for
|
|
42
59
|
* (defaults to the latest known one). An unknown version falls back to the
|
|
43
60
|
* closest older map, so `0.88.2` and `0.89.0` both use the `0.88.1` map.
|
|
61
|
+
* packages - names of other packages (besides `react-native`) to rewrite
|
|
62
|
+
* imports of, e.g. `['react-native-svg']`
|
|
63
|
+
* onModuleReexport - what to do on `module.exports = require('pkg')`:
|
|
64
|
+
* 'error' (default) throws, 'warn' logs and keeps the code, 'silent' keeps it
|
|
44
65
|
* silenceBatchedUpdatesWarning - hides the `unstable_batchedUpdates` no-op
|
|
45
66
|
* warning on 0.86+
|
|
46
67
|
*/
|
|
47
68
|
|
|
69
|
+
const fs = require('fs');
|
|
48
70
|
const nodePath = require('path');
|
|
71
|
+
const { createPackageResolver, findPackageRoot } = require('./resolve-package');
|
|
49
72
|
|
|
50
73
|
const RN = 'react-native';
|
|
51
74
|
|
|
@@ -61,9 +84,10 @@ const compareVersions = (a, b) => {
|
|
|
61
84
|
return 0;
|
|
62
85
|
};
|
|
63
86
|
|
|
64
|
-
// `maps.json` is generated by `scripts/build-maps.js` from
|
|
65
|
-
// (Format B: one entry per
|
|
66
|
-
// values). See that script for
|
|
87
|
+
// `maps.json` is generated by `scripts/build-maps.js` from
|
|
88
|
+
// `maps/<package>/*.json`: one block per package (Format B: one entry per
|
|
89
|
+
// export name, with version ranges and interned values). See that script for
|
|
90
|
+
// the exact format.
|
|
67
91
|
const loadData = () => require(nodePath.join(__dirname, 'maps.json'));
|
|
68
92
|
|
|
69
93
|
// 'Libraries/Image/Image|default' -> ['Libraries/Image/Image', 'default']
|
|
@@ -95,18 +119,25 @@ const findMapVersion = (versions, requested) => {
|
|
|
95
119
|
return found;
|
|
96
120
|
};
|
|
97
121
|
|
|
98
|
-
|
|
99
|
-
|
|
122
|
+
// Packages with a shipped map (besides `react-native`).
|
|
123
|
+
const hasMap = (packageName) =>
|
|
124
|
+
Object.prototype.hasOwnProperty.call(loadData(), packageName);
|
|
125
|
+
|
|
126
|
+
// The map of `packageName` for `version` (default: the latest known one):
|
|
127
|
+
// `{ [exportName]: [pathInPackage, exportNameInModule] }`.
|
|
128
|
+
const loadPackageMap = (packageName, version) => {
|
|
129
|
+
const data = loadData()[packageName];
|
|
100
130
|
const selected =
|
|
101
131
|
version == null ? data.latest : findMapVersion(data.versions, version);
|
|
102
132
|
|
|
103
133
|
if (selected === null) {
|
|
104
134
|
throw new Error(
|
|
105
|
-
`[react-native-direct-imports] No map for
|
|
135
|
+
`[react-native-direct-imports] No map for ${packageName} ${version}. Available versions: ${data.versions.join(', ')}`,
|
|
106
136
|
);
|
|
107
137
|
}
|
|
108
138
|
|
|
109
|
-
const
|
|
139
|
+
const cacheKey = `${packageName}@${selected}`;
|
|
140
|
+
const cached = resolvedMaps.get(cacheKey);
|
|
110
141
|
if (cached) return cached;
|
|
111
142
|
|
|
112
143
|
const map = {};
|
|
@@ -118,15 +149,43 @@ const loadMap = (version) => {
|
|
|
118
149
|
map[name] = splitValue(data.values[valueIndex]);
|
|
119
150
|
}
|
|
120
151
|
|
|
121
|
-
resolvedMaps.set(
|
|
152
|
+
resolvedMaps.set(cacheKey, map);
|
|
122
153
|
return map;
|
|
123
154
|
};
|
|
124
155
|
|
|
156
|
+
const loadMap = (version) => loadPackageMap(RN, version);
|
|
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
|
+
|
|
125
171
|
module.exports = function reactNativeDirectImports(
|
|
126
|
-
|
|
127
|
-
{
|
|
172
|
+
api,
|
|
173
|
+
{
|
|
174
|
+
reactNativeVersion,
|
|
175
|
+
packages = [],
|
|
176
|
+
onModuleReexport = 'error',
|
|
177
|
+
silenceBatchedUpdatesWarning = false,
|
|
178
|
+
} = {},
|
|
128
179
|
) {
|
|
180
|
+
const { types: t } = api;
|
|
181
|
+
if (!['error', 'warn', 'silent'].includes(onModuleReexport)) {
|
|
182
|
+
throw new Error(
|
|
183
|
+
`[react-native-direct-imports] \`onModuleReexport\` must be 'error', 'warn' or 'silent', got ${JSON.stringify(onModuleReexport)}`,
|
|
184
|
+
);
|
|
185
|
+
}
|
|
129
186
|
const MAP = loadMap(reactNativeVersion);
|
|
187
|
+
const PACKAGES = new Set(packages);
|
|
188
|
+
const { resolveImport: resolveByAnalysis } = createPackageResolver(api);
|
|
130
189
|
|
|
131
190
|
const buildRequire = (source, exportName) => {
|
|
132
191
|
const call = t.callExpression(t.identifier('require'), [
|
|
@@ -191,8 +250,9 @@ module.exports = function reactNativeDirectImports(
|
|
|
191
250
|
return null;
|
|
192
251
|
};
|
|
193
252
|
|
|
194
|
-
// `require('react-native')
|
|
195
|
-
|
|
253
|
+
// `require('react-native')` or `require('<listed package>')`, also wrapped in
|
|
254
|
+
// `(... as T)`, `(...)!`, `(...)`. Returns the package name, or `null`.
|
|
255
|
+
const requiredPackage = (node, scope) => {
|
|
196
256
|
while (
|
|
197
257
|
t.isTSAsExpression(node) ||
|
|
198
258
|
t.isTSSatisfiesExpression(node) ||
|
|
@@ -203,13 +263,17 @@ module.exports = function reactNativeDirectImports(
|
|
|
203
263
|
) {
|
|
204
264
|
node = node.expression;
|
|
205
265
|
}
|
|
206
|
-
|
|
266
|
+
if (
|
|
207
267
|
t.isCallExpression(node) &&
|
|
208
268
|
t.isIdentifier(node.callee, { name: 'require' }) &&
|
|
209
269
|
!scope.hasBinding('require') &&
|
|
210
270
|
node.arguments.length === 1 &&
|
|
211
|
-
t.isStringLiteral(node.arguments[0]
|
|
212
|
-
|
|
271
|
+
t.isStringLiteral(node.arguments[0]) &&
|
|
272
|
+
(node.arguments[0].value === RN || PACKAGES.has(node.arguments[0].value))
|
|
273
|
+
) {
|
|
274
|
+
return node.arguments[0].value;
|
|
275
|
+
}
|
|
276
|
+
return null;
|
|
213
277
|
};
|
|
214
278
|
|
|
215
279
|
// Metro's `inlinePlatform` only inlines `Platform.OS` / `Platform.select`
|
|
@@ -242,28 +306,35 @@ module.exports = function reactNativeDirectImports(
|
|
|
242
306
|
return t.identifier('Platform');
|
|
243
307
|
};
|
|
244
308
|
|
|
245
|
-
//
|
|
246
|
-
|
|
247
|
-
const replaceMember = (memberPath, state) => {
|
|
309
|
+
// `x.View = ...`, `x.View++`, `delete x.View`
|
|
310
|
+
const isMutation = (memberPath) => {
|
|
248
311
|
const { parentPath } = memberPath;
|
|
249
|
-
|
|
312
|
+
return (
|
|
250
313
|
(parentPath.isAssignmentExpression() &&
|
|
251
314
|
parentPath.node.left === memberPath.node) ||
|
|
252
315
|
parentPath.isUpdateExpression() ||
|
|
253
316
|
parentPath.isUnaryExpression({ operator: 'delete' })
|
|
254
|
-
)
|
|
255
|
-
|
|
256
|
-
|
|
317
|
+
);
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
// Replaces `x.View` with the direct `require()` expression. Returns `false`
|
|
321
|
+
// if the member can't be rewritten. `pkg` is the package `x` was required
|
|
322
|
+
// from.
|
|
323
|
+
const replaceMember = (memberPath, state, pkg = RN) => {
|
|
324
|
+
if (isMutation(memberPath)) return false;
|
|
257
325
|
|
|
258
326
|
const name = memberName(memberPath.node);
|
|
259
327
|
const expression =
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
328
|
+
pkg === RN
|
|
329
|
+
? (name === 'Platform' &&
|
|
330
|
+
MAP.Platform &&
|
|
331
|
+
platformIdentifier(memberPath, state)) ||
|
|
332
|
+
buildDirectExpression(name)
|
|
333
|
+
: buildPackageExpression(pkg, name, state);
|
|
264
334
|
if (!expression) return false;
|
|
265
335
|
|
|
266
336
|
memberPath.replaceWith(expression);
|
|
337
|
+
if (pkg !== RN) addSetup(memberPath, state, pkg);
|
|
267
338
|
return true;
|
|
268
339
|
};
|
|
269
340
|
|
|
@@ -320,6 +391,291 @@ module.exports = function reactNativeDirectImports(
|
|
|
320
391
|
}
|
|
321
392
|
};
|
|
322
393
|
|
|
394
|
+
// Whole-module re-export (`module.exports = require('pkg')`): the whole
|
|
395
|
+
// package ends up in the bundle, so `onModuleReexport` decides what to do.
|
|
396
|
+
const reportModuleReexport = (path, pkg) => {
|
|
397
|
+
if (onModuleReexport === 'silent') return;
|
|
398
|
+
|
|
399
|
+
const example = pkg === RN ? 'View' : 'Name';
|
|
400
|
+
const reason =
|
|
401
|
+
onModuleReexport === 'error'
|
|
402
|
+
? 'is not allowed'
|
|
403
|
+
: 'is not rewritten and keeps the whole package in the bundle';
|
|
404
|
+
const error = path.buildCodeFrameError(
|
|
405
|
+
`\`module.exports = require('${pkg}')\` ${reason}, re-export named values instead: \`exports.${example} = require('${pkg}').${example}\``,
|
|
406
|
+
);
|
|
407
|
+
if (onModuleReexport === 'error') throw error;
|
|
408
|
+
console.warn(`[react-native-direct-imports] ${error.message}`);
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
// `module.exports = <ref>`
|
|
412
|
+
const isModuleExportsValue = (ref) => {
|
|
413
|
+
const { parentPath } = ref;
|
|
414
|
+
return (
|
|
415
|
+
parentPath.isAssignmentExpression() &&
|
|
416
|
+
parentPath.node.right === ref.node &&
|
|
417
|
+
parentPath.get('left').matchesPattern('module.exports')
|
|
418
|
+
);
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
// Packages are looked up in `node_modules` from the file being compiled.
|
|
422
|
+
const dirOf = (state) =>
|
|
423
|
+
nodePath.dirname(state.filename || nodePath.join(process.cwd(), '_'));
|
|
424
|
+
|
|
425
|
+
const installedVersions = new Map();
|
|
426
|
+
|
|
427
|
+
// Version of the package installed next to the file being compiled
|
|
428
|
+
// (`15.12.0-rc.1` -> `15.12.0`).
|
|
429
|
+
const installedVersion = (packageName, fromDir) => {
|
|
430
|
+
const cacheKey = `${packageName}\0${fromDir}`;
|
|
431
|
+
const cached = installedVersions.get(cacheKey);
|
|
432
|
+
if (cached) return cached;
|
|
433
|
+
|
|
434
|
+
const root = findPackageRoot(packageName, fromDir);
|
|
435
|
+
if (!root) {
|
|
436
|
+
throw new Error(
|
|
437
|
+
`[react-native-direct-imports] Can't find the package "${packageName}" listed in \`packages\`, looked in node_modules from ${fromDir}`,
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
const { version } = JSON.parse(
|
|
441
|
+
fs.readFileSync(nodePath.join(root, 'package.json'), 'utf8'),
|
|
442
|
+
);
|
|
443
|
+
const release = String(version).replace(/[-+].*$/, '');
|
|
444
|
+
installedVersions.set(cacheKey, release);
|
|
445
|
+
return release;
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
// `['<package>/<path>', exportName]` for `importedName` of another
|
|
449
|
+
// package, or `null` to keep the import. Packages with a shipped map use the
|
|
450
|
+
// map of the installed version; any other package is resolved by following
|
|
451
|
+
// the re-exports of its installed entry file.
|
|
452
|
+
const resolveImport = (packageName, fromDir, importedName) => {
|
|
453
|
+
if (!hasMap(packageName)) {
|
|
454
|
+
return resolveByAnalysis(packageName, fromDir, importedName);
|
|
455
|
+
}
|
|
456
|
+
const map = loadPackageMap(
|
|
457
|
+
packageName,
|
|
458
|
+
installedVersion(packageName, fromDir),
|
|
459
|
+
);
|
|
460
|
+
const entry = map[importedName];
|
|
461
|
+
return entry ? [`${packageName}/${entry[0]}`, entry[1]] : null;
|
|
462
|
+
};
|
|
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
|
+
|
|
500
|
+
// `require('<path>').<exportName>` for `importedName` of another package,
|
|
501
|
+
// or `null` to keep the original.
|
|
502
|
+
const buildPackageExpression = (pkg, importedName, state) => {
|
|
503
|
+
const entry =
|
|
504
|
+
importedName != null && resolveImport(pkg, dirOf(state), importedName);
|
|
505
|
+
if (!entry) return null;
|
|
506
|
+
|
|
507
|
+
const [source, exportName] = entry;
|
|
508
|
+
const call = buildRequire(source, '*');
|
|
509
|
+
if (exportName === '*') return call;
|
|
510
|
+
return t.isValidIdentifier(exportName, false)
|
|
511
|
+
? t.memberExpression(call, t.identifier(exportName))
|
|
512
|
+
: t.memberExpression(call, t.stringLiteral(exportName), true);
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
// var x = require('react-native-svg');
|
|
516
|
+
// x.Path; x.default;
|
|
517
|
+
//
|
|
518
|
+
// Unlike `react-native`, all or nothing: if a usage can't be rewritten
|
|
519
|
+
// (`fn(x)`, `x.Unknown`, `x.Path = ...`, `{ ...x }`), the whole package
|
|
520
|
+
// stays loaded through `x` anyway, so nothing is gained by rewriting the rest.
|
|
521
|
+
const rewritePackageBinding = (declaratorPath, state, pkg) => {
|
|
522
|
+
const { name } = declaratorPath.node.id;
|
|
523
|
+
const binding = declaratorPath.scope.getBinding(name);
|
|
524
|
+
if (
|
|
525
|
+
!binding ||
|
|
526
|
+
binding.path !== declaratorPath ||
|
|
527
|
+
!binding.constant ||
|
|
528
|
+
binding.referencePaths.length === 0 ||
|
|
529
|
+
declaratorPath.parentPath.parentPath.isExportNamedDeclaration()
|
|
530
|
+
) {
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
const reexport = binding.referencePaths.find(isModuleExportsValue);
|
|
535
|
+
if (reexport) {
|
|
536
|
+
reportModuleReexport(reexport.parentPath, pkg);
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
const members = [];
|
|
541
|
+
for (const ref of binding.referencePaths) {
|
|
542
|
+
const memberPath = ref.parentPath;
|
|
543
|
+
if (
|
|
544
|
+
!memberPath.isMemberExpression() ||
|
|
545
|
+
memberPath.node.object !== ref.node ||
|
|
546
|
+
isMutation(memberPath) ||
|
|
547
|
+
!buildPackageExpression(pkg, memberName(memberPath.node), state)
|
|
548
|
+
) {
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
members.push(memberPath);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
for (const memberPath of members) replaceMember(memberPath, state, pkg);
|
|
555
|
+
declaratorPath.remove();
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
// const { Svg, Path: P } = require('react-native-svg');
|
|
559
|
+
// All or nothing too: `{ Svg, ...rest }`, `{ Unknown }`, `{ [key]: x }` and
|
|
560
|
+
// `{ Svg = fallback }` keep the original.
|
|
561
|
+
const rewritePackagePattern = (declaratorPath, state, pkg) => {
|
|
562
|
+
const declarators = [];
|
|
563
|
+
|
|
564
|
+
for (const prop of declaratorPath.node.id.properties) {
|
|
565
|
+
if (
|
|
566
|
+
!t.isObjectProperty(prop) ||
|
|
567
|
+
prop.computed ||
|
|
568
|
+
!t.isIdentifier(prop.value) ||
|
|
569
|
+
!(t.isIdentifier(prop.key) || t.isStringLiteral(prop.key))
|
|
570
|
+
) {
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
const expression = buildPackageExpression(pkg, nameOf(prop.key), state);
|
|
574
|
+
if (!expression) return;
|
|
575
|
+
declarators.push(t.variableDeclarator(prop.value, expression));
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
if (declarators.length === 0) return;
|
|
579
|
+
addSetup(declaratorPath, state, pkg);
|
|
580
|
+
declaratorPath.replaceWithMultiple(declarators);
|
|
581
|
+
};
|
|
582
|
+
|
|
583
|
+
// An export name as a Babel node (names like `'a-b'` are string literals).
|
|
584
|
+
const exportNameNode = (name) =>
|
|
585
|
+
t.isValidIdentifier(name, false) ? t.identifier(name) : t.stringLiteral(name);
|
|
586
|
+
|
|
587
|
+
// export { Defs, default as Svg } from 'react-native-svg';
|
|
588
|
+
const rewritePackageReexport = (path, state) => {
|
|
589
|
+
const { node } = path;
|
|
590
|
+
if (node.exportKind === 'type') return;
|
|
591
|
+
|
|
592
|
+
const fromDir = dirOf(state);
|
|
593
|
+
const kept = [];
|
|
594
|
+
const direct = [];
|
|
595
|
+
|
|
596
|
+
for (const spec of node.specifiers) {
|
|
597
|
+
const entry =
|
|
598
|
+
t.isExportSpecifier(spec) &&
|
|
599
|
+
spec.exportKind !== 'type' &&
|
|
600
|
+
resolveImport(node.source.value, fromDir, nameOf(spec.local));
|
|
601
|
+
if (!entry) {
|
|
602
|
+
kept.push(spec);
|
|
603
|
+
continue;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
const [source, exportName] = entry;
|
|
607
|
+
const specifier =
|
|
608
|
+
exportName === '*'
|
|
609
|
+
? t.exportNamespaceSpecifier(t.cloneNode(spec.exported))
|
|
610
|
+
: t.exportSpecifier(exportNameNode(exportName), t.cloneNode(spec.exported));
|
|
611
|
+
direct.push(
|
|
612
|
+
t.exportNamedDeclaration(null, [specifier], t.stringLiteral(source)),
|
|
613
|
+
);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
if (direct.length === 0) return;
|
|
617
|
+
addSetup(path, state, node.source.value);
|
|
618
|
+
|
|
619
|
+
if (kept.length > 0) {
|
|
620
|
+
node.specifiers = kept;
|
|
621
|
+
path.insertAfter(direct);
|
|
622
|
+
} else {
|
|
623
|
+
path.replaceWithMultiple(direct);
|
|
624
|
+
}
|
|
625
|
+
};
|
|
626
|
+
|
|
627
|
+
// import Svg, { Defs } from 'react-native-svg';
|
|
628
|
+
const rewritePackageImport = (path, state) => {
|
|
629
|
+
const { node } = path;
|
|
630
|
+
if (node.importKind != null && node.importKind !== 'value') return;
|
|
631
|
+
|
|
632
|
+
const fromDir = dirOf(state);
|
|
633
|
+
const kept = [];
|
|
634
|
+
const direct = [];
|
|
635
|
+
|
|
636
|
+
for (const spec of node.specifiers) {
|
|
637
|
+
let imported = null;
|
|
638
|
+
if (t.isImportDefaultSpecifier(spec)) {
|
|
639
|
+
imported = 'default';
|
|
640
|
+
} else if (
|
|
641
|
+
t.isImportSpecifier(spec) &&
|
|
642
|
+
spec.importKind !== 'type' &&
|
|
643
|
+
spec.importKind !== 'typeof'
|
|
644
|
+
) {
|
|
645
|
+
imported = nameOf(spec.imported);
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
const entry =
|
|
649
|
+
imported && resolveImport(node.source.value, fromDir, imported);
|
|
650
|
+
if (!entry) {
|
|
651
|
+
kept.push(spec);
|
|
652
|
+
continue;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
const [source, exportName] = entry;
|
|
656
|
+
const local = t.identifier(spec.local.name);
|
|
657
|
+
let specifier;
|
|
658
|
+
if (exportName === 'default') {
|
|
659
|
+
specifier = t.importDefaultSpecifier(local);
|
|
660
|
+
} else if (exportName === '*') {
|
|
661
|
+
specifier = t.importNamespaceSpecifier(local);
|
|
662
|
+
} else {
|
|
663
|
+
specifier = t.importSpecifier(local, exportNameNode(exportName));
|
|
664
|
+
}
|
|
665
|
+
direct.push(t.importDeclaration([specifier], t.stringLiteral(source)));
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
if (direct.length === 0) return;
|
|
669
|
+
addSetup(path, state, node.source.value);
|
|
670
|
+
|
|
671
|
+
if (kept.length > 0) {
|
|
672
|
+
node.specifiers = kept;
|
|
673
|
+
path.insertAfter(direct);
|
|
674
|
+
} else {
|
|
675
|
+
path.replaceWithMultiple(direct);
|
|
676
|
+
}
|
|
677
|
+
};
|
|
678
|
+
|
|
323
679
|
return {
|
|
324
680
|
name: 'react-native-direct-imports',
|
|
325
681
|
visitor: {
|
|
@@ -328,36 +684,38 @@ module.exports = function reactNativeDirectImports(
|
|
|
328
684
|
// see detached paths.
|
|
329
685
|
AssignmentExpression(path) {
|
|
330
686
|
const { node } = path;
|
|
331
|
-
if (
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
)
|
|
335
|
-
throw path.buildCodeFrameError(
|
|
336
|
-
"`module.exports = require('react-native')` is not allowed, re-export named values instead: `exports.View = require('react-native').View`",
|
|
337
|
-
);
|
|
338
|
-
}
|
|
687
|
+
if (!path.get('left').matchesPattern('module.exports')) return;
|
|
688
|
+
|
|
689
|
+
const pkg = requiredPackage(node.right, path.scope);
|
|
690
|
+
if (pkg) reportModuleReexport(path, pkg);
|
|
339
691
|
},
|
|
340
692
|
|
|
341
693
|
// require('react-native').Linking
|
|
342
694
|
MemberExpression(path, state) {
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
}
|
|
695
|
+
const pkg = requiredPackage(path.node.object, path.scope);
|
|
696
|
+
if (pkg) replaceMember(path, state, pkg);
|
|
346
697
|
},
|
|
347
698
|
|
|
348
699
|
VariableDeclarator(path, state) {
|
|
349
700
|
const { node } = path;
|
|
350
|
-
|
|
701
|
+
const pkg = requiredPackage(node.init, path.scope);
|
|
702
|
+
if (!pkg) return;
|
|
351
703
|
|
|
352
704
|
if (t.isIdentifier(node.id)) {
|
|
353
|
-
rewriteBinding(path, state);
|
|
705
|
+
if (pkg === RN) rewriteBinding(path, state);
|
|
706
|
+
else rewritePackageBinding(path, state, pkg);
|
|
354
707
|
} else if (t.isObjectPattern(node.id)) {
|
|
355
|
-
rewritePattern(path);
|
|
708
|
+
if (pkg === RN) rewritePattern(path);
|
|
709
|
+
else rewritePackagePattern(path, state, pkg);
|
|
356
710
|
}
|
|
357
711
|
},
|
|
358
712
|
|
|
359
|
-
ImportDeclaration(path) {
|
|
713
|
+
ImportDeclaration(path, state) {
|
|
360
714
|
const { node } = path;
|
|
715
|
+
if (node.source.value !== RN && PACKAGES.has(node.source.value)) {
|
|
716
|
+
rewritePackageImport(path, state);
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
361
719
|
if (node.source.value !== RN || node.importKind === 'type') return;
|
|
362
720
|
|
|
363
721
|
if (
|
|
@@ -395,8 +753,12 @@ module.exports = function reactNativeDirectImports(
|
|
|
395
753
|
}
|
|
396
754
|
},
|
|
397
755
|
|
|
398
|
-
ExportNamedDeclaration(path) {
|
|
756
|
+
ExportNamedDeclaration(path, state) {
|
|
399
757
|
const { node } = path;
|
|
758
|
+
if (node.source && node.source.value !== RN && PACKAGES.has(node.source.value)) {
|
|
759
|
+
rewritePackageReexport(path, state);
|
|
760
|
+
return;
|
|
761
|
+
}
|
|
400
762
|
if (node.source?.value !== RN || node.exportKind === 'type') return;
|
|
401
763
|
|
|
402
764
|
const kept = [];
|
|
@@ -443,3 +805,5 @@ module.exports = function reactNativeDirectImports(
|
|
|
443
805
|
};
|
|
444
806
|
|
|
445
807
|
module.exports.loadMap = loadMap;
|
|
808
|
+
module.exports.loadPackageMap = loadPackageMap;
|
|
809
|
+
module.exports.loadPackageSetup = loadPackageSetup;
|