babel-plugin-react-native-direct-imports 1.2.1-rc.14 → 2.0.0
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/CHANGELOG.md +68 -0
- package/README.md +41 -141
- package/index.js +163 -21
- package/maps.json +1 -1
- package/package.json +4 -3
- 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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "babel-plugin-react-native-direct-imports",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Babel plugin that rewrites `react-native` barrel imports into direct module imports",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
"files": [
|
|
11
11
|
"index.js",
|
|
12
12
|
"resolve-package.js",
|
|
13
|
-
"maps.json"
|
|
13
|
+
"maps.json",
|
|
14
|
+
"setup"
|
|
14
15
|
],
|
|
15
16
|
"scripts": {
|
|
16
17
|
"test": "node scripts/build-maps.js && node --test",
|
|
@@ -53,4 +54,4 @@
|
|
|
53
54
|
"publishConfig": {
|
|
54
55
|
"access": "public"
|
|
55
56
|
}
|
|
56
|
-
}
|
|
57
|
+
}
|
package/resolve-package.js
CHANGED
|
@@ -188,7 +188,10 @@ const isTypeOnly = (node) =>
|
|
|
188
188
|
// one `package.json` points at
|
|
189
189
|
// ignoreExports - ignore the `exports` field: paths are file locations, for
|
|
190
190
|
// packages whose deep paths are meant to be imported anyway
|
|
191
|
-
const createPackageResolver = (
|
|
191
|
+
const createPackageResolver = (
|
|
192
|
+
{ types: t, parseSync, traverse },
|
|
193
|
+
options = {},
|
|
194
|
+
) => {
|
|
192
195
|
const nameOf = (node) => (t.isIdentifier(node) ? node.name : node.value);
|
|
193
196
|
|
|
194
197
|
const analyses = new Map();
|
|
@@ -412,13 +415,25 @@ const createPackageResolver = ({ types: t, parseSync }, options = {}) => {
|
|
|
412
415
|
return [...names];
|
|
413
416
|
};
|
|
414
417
|
|
|
418
|
+
// The import specifier of `file` from outside the package
|
|
419
|
+
// (`'<package>/<path>'`), or `null` when its `exports` field hides it.
|
|
420
|
+
const specifierOf = ({ root, exportsMap }, packageName, file) => {
|
|
421
|
+
const relative = nodePath.relative(root, file).split(nodePath.sep).join('/');
|
|
422
|
+
if (exportsMap) {
|
|
423
|
+
const subpath = findExportedSubpath(exportsMap, `./${relative}`);
|
|
424
|
+
return subpath === null ? null : `${packageName}/${subpath.slice(2)}`;
|
|
425
|
+
}
|
|
426
|
+
return `${packageName}/${relative.replace(/\.(?:[cm]?[jt]sx?)$/, '')}`;
|
|
427
|
+
};
|
|
428
|
+
|
|
415
429
|
const resolved = new Map();
|
|
416
430
|
|
|
417
431
|
// Returns `['<package>/<path>', exportName]` for the module defining
|
|
418
432
|
// `importedName` (`'default'` for the default export), or `null` if the
|
|
419
433
|
// import should stay as it is.
|
|
420
434
|
const resolveImport = (packageName, fromDir, importedName) => {
|
|
421
|
-
const
|
|
435
|
+
const loaded = loadPackage(packageName, fromDir);
|
|
436
|
+
const { entry } = loaded;
|
|
422
437
|
|
|
423
438
|
const cacheKey = `${entry}\0${importedName}`;
|
|
424
439
|
if (resolved.has(cacheKey)) return resolved.get(cacheKey);
|
|
@@ -427,29 +442,175 @@ const createPackageResolver = ({ types: t, parseSync }, options = {}) => {
|
|
|
427
442
|
let result = null;
|
|
428
443
|
// Names declared in the entry file itself have nowhere else to go.
|
|
429
444
|
if (origin && origin.file !== entry) {
|
|
430
|
-
const
|
|
431
|
-
|
|
432
|
-
.split(nodePath.sep)
|
|
433
|
-
.join('/');
|
|
434
|
-
|
|
435
|
-
if (exportsMap) {
|
|
436
|
-
const subpath = findExportedSubpath(exportsMap, `./${relative}`);
|
|
437
|
-
if (subpath !== null) {
|
|
438
|
-
result = [`${packageName}/${subpath.slice(2)}`, origin.name];
|
|
439
|
-
}
|
|
440
|
-
} else {
|
|
441
|
-
result = [
|
|
442
|
-
`${packageName}/${relative.replace(/\.(?:[cm]?[jt]sx?)$/, '')}`,
|
|
443
|
-
origin.name,
|
|
444
|
-
];
|
|
445
|
-
}
|
|
445
|
+
const specifier = specifierOf(loaded, packageName, origin.file);
|
|
446
|
+
if (specifier !== null) result = [specifier, origin.name];
|
|
446
447
|
}
|
|
447
448
|
|
|
448
449
|
resolved.set(cacheKey, result);
|
|
449
450
|
return result;
|
|
450
451
|
};
|
|
451
452
|
|
|
452
|
-
|
|
453
|
+
// The code the package's entry runs when it is loaded, which a direct
|
|
454
|
+
// import skips, as lines of a standalone ES module (used to generate maps):
|
|
455
|
+
//
|
|
456
|
+
// import './publicGlobals';
|
|
457
|
+
// import { initialize } from './init';
|
|
458
|
+
// export { View } from './View';
|
|
459
|
+
// const local = 1;
|
|
460
|
+
// initialize();
|
|
461
|
+
// // ->
|
|
462
|
+
// import 'pkg/src/publicGlobals';
|
|
463
|
+
// import { initialize } from 'pkg/src/init';
|
|
464
|
+
// initialize();
|
|
465
|
+
//
|
|
466
|
+
// Kept: bare imports, top-level expression statements (calls) that only
|
|
467
|
+
// use imports and globals, and the imports those use. Everything else
|
|
468
|
+
// (exports, declarations, statements using entry-local bindings) is dropped;
|
|
469
|
+
// `warn` is called for each dropped statement that runs code.
|
|
470
|
+
const listSetup = (packageName, fromDir, warn = () => {}) => {
|
|
471
|
+
const loaded = loadPackage(packageName, fromDir);
|
|
472
|
+
const { entry } = loaded;
|
|
473
|
+
const code = fs.readFileSync(entry, 'utf8');
|
|
474
|
+
const ast = parseSync(code, {
|
|
475
|
+
filename: entry,
|
|
476
|
+
babelrc: false,
|
|
477
|
+
configFile: false,
|
|
478
|
+
sourceType: 'module',
|
|
479
|
+
parserOpts: { plugins: parserPlugins(entry) },
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
// `./x` -> 'pkg/x'; a file that only exists as `x.native.ts` / `x.ios.ts`
|
|
483
|
+
// keeps the bare path, which Metro resolves per platform.
|
|
484
|
+
const PLATFORMS = ['native', 'ios', 'android'];
|
|
485
|
+
const setupSource = (source) => {
|
|
486
|
+
if (!source.startsWith('.')) return source;
|
|
487
|
+
const base = nodePath.resolve(nodePath.dirname(entry), source);
|
|
488
|
+
const file = resolveFile(base);
|
|
489
|
+
if (file) return specifierOf(loaded, packageName, file);
|
|
490
|
+
const candidates = [base, nodePath.join(base, 'index')];
|
|
491
|
+
for (const candidate of candidates) {
|
|
492
|
+
for (const platform of PLATFORMS) {
|
|
493
|
+
if (EXTENSIONS.some((ext) => isFile(`${candidate}.${platform}${ext}`))) {
|
|
494
|
+
return specifierOf(loaded, packageName, `${candidate}.ts`);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
return null;
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
// Names each top-level statement reads, and whether it reads a binding
|
|
502
|
+
// declared in the entry itself (not an import).
|
|
503
|
+
const usage = new Map();
|
|
504
|
+
traverse(ast, {
|
|
505
|
+
ReferencedIdentifier(path) {
|
|
506
|
+
const statement = path.find((p) => p.parentPath?.isProgram());
|
|
507
|
+
if (!statement) return;
|
|
508
|
+
const binding = path.scope.getBinding(path.node.name);
|
|
509
|
+
if (!binding || binding.scope !== path.scope.getProgramParent()) return;
|
|
510
|
+
const entryUsage = usage.get(statement.node) ?? {
|
|
511
|
+
imports: new Set(),
|
|
512
|
+
usesLocals: false,
|
|
513
|
+
};
|
|
514
|
+
if (binding.kind === 'module') entryUsage.imports.add(path.node.name);
|
|
515
|
+
else entryUsage.usesLocals = true;
|
|
516
|
+
usage.set(statement.node, entryUsage);
|
|
517
|
+
},
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
const imports = new Map(); // local name -> { source, specifier node }
|
|
521
|
+
for (const node of ast.program.body) {
|
|
522
|
+
if (!t.isImportDeclaration(node) || isTypeOnly(node)) continue;
|
|
523
|
+
for (const spec of node.specifiers) {
|
|
524
|
+
if (!isTypeOnly(spec)) {
|
|
525
|
+
imports.set(spec.local.name, { source: node.source.value, spec });
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
const importLine = (source, spec) => {
|
|
531
|
+
const local = spec.local.name;
|
|
532
|
+
if (t.isImportDefaultSpecifier(spec)) {
|
|
533
|
+
return `import ${local} from '${source}';`;
|
|
534
|
+
}
|
|
535
|
+
if (t.isImportNamespaceSpecifier(spec)) {
|
|
536
|
+
return `import * as ${local} from '${source}';`;
|
|
537
|
+
}
|
|
538
|
+
const imported = nameOf(spec.imported);
|
|
539
|
+
if (imported === local) return `import { ${local} } from '${source}';`;
|
|
540
|
+
const name = t.isValidIdentifier(imported, false)
|
|
541
|
+
? imported
|
|
542
|
+
: JSON.stringify(imported);
|
|
543
|
+
return `import { ${name} as ${local} } from '${source}';`;
|
|
544
|
+
};
|
|
545
|
+
|
|
546
|
+
const lines = [];
|
|
547
|
+
const imported = new Set();
|
|
548
|
+
const where = (node) =>
|
|
549
|
+
`${nodePath.relative(loaded.root, entry)}:${node.loc.start.line}`;
|
|
550
|
+
|
|
551
|
+
for (const node of ast.program.body) {
|
|
552
|
+
// import './publicGlobals';
|
|
553
|
+
if (t.isImportDeclaration(node) && node.specifiers.length === 0) {
|
|
554
|
+
if (isTypeOnly(node)) continue;
|
|
555
|
+
const source = setupSource(node.source.value);
|
|
556
|
+
if (source === null) {
|
|
557
|
+
warn(`can't resolve side-effect import '${node.source.value}' (${where(node)})`);
|
|
558
|
+
continue;
|
|
559
|
+
}
|
|
560
|
+
lines.push(`import '${source}';`);
|
|
561
|
+
continue;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
if (!t.isExpressionStatement(node)) continue;
|
|
565
|
+
|
|
566
|
+
const { imports: names = new Set(), usesLocals = false } =
|
|
567
|
+
usage.get(node) ?? {};
|
|
568
|
+
if (usesLocals) {
|
|
569
|
+
warn(`skipped, uses a binding declared in the entry (${where(node)})`);
|
|
570
|
+
continue;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
const needed = [];
|
|
574
|
+
let resolvable = true;
|
|
575
|
+
for (const name of names) {
|
|
576
|
+
if (imported.has(name)) continue;
|
|
577
|
+
const { source, spec } = imports.get(name);
|
|
578
|
+
const resolvedSource = setupSource(source);
|
|
579
|
+
if (resolvedSource === null) {
|
|
580
|
+
resolvable = false;
|
|
581
|
+
break;
|
|
582
|
+
}
|
|
583
|
+
needed.push([name, importLine(resolvedSource, spec)]);
|
|
584
|
+
}
|
|
585
|
+
if (!resolvable) {
|
|
586
|
+
warn(`skipped, can't resolve one of its imports (${where(node)})`);
|
|
587
|
+
continue;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
const statement = code.slice(node.start, node.end);
|
|
591
|
+
try {
|
|
592
|
+
// The setup module is plain JavaScript.
|
|
593
|
+
parseSync(statement, {
|
|
594
|
+
babelrc: false,
|
|
595
|
+
configFile: false,
|
|
596
|
+
sourceType: 'module',
|
|
597
|
+
});
|
|
598
|
+
} catch {
|
|
599
|
+
warn(`skipped, not plain JavaScript (${where(node)})`);
|
|
600
|
+
continue;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
for (const [name, line] of needed) {
|
|
604
|
+
imported.add(name);
|
|
605
|
+
lines.push(line);
|
|
606
|
+
}
|
|
607
|
+
lines.push(statement.endsWith(';') ? statement : `${statement};`);
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
return lines;
|
|
611
|
+
};
|
|
612
|
+
|
|
613
|
+
return { resolveImport, listExports, listSetup };
|
|
453
614
|
};
|
|
454
615
|
|
|
455
616
|
module.exports = { createPackageResolver, findPackageRoot };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// Generated by scripts/build-maps.js: what react-native-gesture-handler's entry runs on load.
|
|
2
|
+
// Imported by files the plugin rewrites, so direct imports don't skip it.
|
|
3
|
+
import 'react-native-gesture-handler/src/globals';
|
|
4
|
+
import { initialize } from 'react-native-gesture-handler/src/init';
|
|
5
|
+
initialize();
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Generated by scripts/build-maps.js: what react-native-reanimated's entry runs on load.
|
|
2
|
+
// Imported by files the plugin rewrites, so direct imports don't skip it.
|
|
3
|
+
import 'react-native-reanimated/src/publicGlobals';
|
|
4
|
+
import { initializeReanimatedModule } from 'react-native-reanimated/src/initializers';
|
|
5
|
+
import { ReanimatedModule } from 'react-native-reanimated/src/ReanimatedModule/index';
|
|
6
|
+
initializeReanimatedModule(ReanimatedModule);
|