fix: enable `resolveSymlinks` and remove dupe code (#1608)

This commit is contained in:
Tommy Nguyen 2022-06-07 10:58:43 +02:00 коммит произвёл GitHub
Родитель e0c542b964
Коммит 4674e76147
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 29 добавлений и 21 удалений

Просмотреть файл

@ -0,0 +1,6 @@
---
"@rnx-kit/metro-config": patch
"@rnx-kit/metro-resolver-symlinks": patch
---
Enable `resolveSymlinks` and remove dupe code

Просмотреть файл

@ -1,7 +1,6 @@
/* jshint esversion: 8, node: true */
// @ts-check
const fs = require("fs");
const path = require("path");
/**
@ -63,17 +62,10 @@ function defaultWatchFolders(projectRoot) {
*/
function resolveModule(name, startDir) {
const { findPackageDependencyDir } = require("@rnx-kit/tools-node/package");
const result = findPackageDependencyDir(name, {
return findPackageDependencyDir(name, {
startDir,
allowSymlinks: true,
resolveSymlinks: true,
});
if (!result) {
return undefined;
}
return fs.lstatSync(result).isSymbolicLink()
? path.resolve(path.dirname(result), fs.readlinkSync(result))
: result;
}
/**

Просмотреть файл

@ -5,19 +5,22 @@ import {
readPackage,
} from "@rnx-kit/tools-node";
import { getAvailablePlatforms } from "@rnx-kit/tools-react-native";
import * as fs from "fs";
import * as path from "path";
import type { MetroResolver, ModuleResolver } from "./types";
function resolveFrom(moduleName: string, startDir: string): string {
const p = findPackageDependencyDir(moduleName, { startDir });
function resolveFrom(moduleName: string, startDir: string): string | undefined {
return findPackageDependencyDir(moduleName, {
startDir,
resolveSymlinks: true,
});
}
function ensureResolveFrom(moduleName: string, startDir: string): string {
const p = resolveFrom(moduleName, startDir);
if (!p) {
throw new Error(`Cannot find module '${moduleName}'`);
}
return fs.lstatSync(p).isSymbolicLink()
? path.resolve(path.dirname(p), fs.readlinkSync(p))
: p;
return p;
}
/**
@ -26,16 +29,19 @@ function resolveFrom(moduleName: string, startDir: string): string {
*/
export function getMetroResolver(fromDir = process.cwd()): MetroResolver {
try {
const rnPath = resolveFrom("react-native", fromDir);
const rncliPath = resolveFrom("@react-native-community/cli", rnPath);
const rnPath = ensureResolveFrom("react-native", fromDir);
const rncliPath = ensureResolveFrom("@react-native-community/cli", rnPath);
const { dependencies = {} } = readPackage(rncliPath);
const metroResolverSearchPath =
"@react-native-community/cli-plugin-metro" in dependencies
? resolveFrom("@react-native-community/cli-plugin-metro", rncliPath)
? ensureResolveFrom(
"@react-native-community/cli-plugin-metro",
rncliPath
)
: rncliPath;
const metroResolverPath = resolveFrom(
const metroResolverPath = ensureResolveFrom(
"metro-resolver",
metroResolverSearchPath
);
@ -76,6 +82,10 @@ export const resolveModulePath: ModuleResolver = (
const pkgName = ref.scope ? `${ref.scope}/${ref.name}` : ref.name;
const pkgRoot = resolveFrom(pkgName, originModulePath);
if (!pkgRoot) {
return moduleName;
}
const replaced = moduleName.replace(pkgName, pkgRoot);
const relativePath = path.relative(path.dirname(originModulePath), replaced);
return relativePath.startsWith(".")