Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions packages/uniwind/src/bundler/adapters/metro/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { CustomResolutionContext, CustomResolver } from 'metro-resolver'
import { realpathSync } from 'node:fs'
import { basename, dirname, sep } from 'node:path'

type ResolverConfig = {
Expand All @@ -10,6 +11,35 @@ type ResolverConfig = {

let cachedInternalBasePath: string | null = null

const isInternalOrigin = (originModulePath: string) => {
if (cachedInternalBasePath === null) {
try {
cachedInternalBasePath = dirname(realpathSync(require.resolve('uniwind/package.json')))
} catch {
cachedInternalBasePath = ''
}
}

if (cachedInternalBasePath === '') {
return false
}

const internalPathPrefix = `${cachedInternalBasePath}${sep}`
if (originModulePath.startsWith(internalPathPrefix)) {
return true
}

if (!originModulePath.includes(`${sep}node_modules${sep}uniwind${sep}`)) {
return false
}

try {
return realpathSync(originModulePath).startsWith(internalPathPrefix)
} catch {
return false
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.
}

const SUPPORTED_COMPONENTS = [
'ActivityIndicator',
'Button',
Expand Down Expand Up @@ -44,15 +74,7 @@ export const nativeResolver = ({
}: ResolverConfig) => {
const resolution = resolver(context, moduleName, platform)

if (cachedInternalBasePath === null) {
try {
cachedInternalBasePath = dirname(require.resolve('uniwind/package.json'))
} catch {
cachedInternalBasePath = ''
}
}

const isInternal = cachedInternalBasePath !== '' && context.originModulePath.startsWith(cachedInternalBasePath)
const isInternal = isInternalOrigin(context.originModulePath)
const isFromNodeModules = context.originModulePath.includes(`${sep}node_modules${sep}`)
const isFromReactNative = context.originModulePath.includes(`${sep}react-native${sep}`)
|| context.originModulePath.includes(`${sep}@react-native${sep}`)
Expand Down Expand Up @@ -92,16 +114,8 @@ export const webResolver = ({
}: ResolverConfig) => {
const resolution = resolver(context, moduleName, platform)

if (cachedInternalBasePath === null) {
try {
cachedInternalBasePath = dirname(require.resolve('uniwind/package.json'))
} catch {
cachedInternalBasePath = ''
}
}

if (
(cachedInternalBasePath !== '' && context.originModulePath.startsWith(cachedInternalBasePath))
isInternalOrigin(context.originModulePath)
|| resolution.type !== 'sourceFile'
|| !resolution.filePath.includes(`${sep}react-native-web${sep}`)
) {
Expand Down
45 changes: 45 additions & 0 deletions packages/uniwind/tests/native/bundler/resolvers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { CustomResolutionContext, CustomResolver } from 'metro-resolver'
import { mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { dirname, join } from 'node:path'
import { nativeResolver } from '../../../src/bundler/adapters/metro/resolvers'

test('keeps internal imports internal when Metro reports a symlinked origin', () => {
const root = mkdtempSync(join(tmpdir(), 'uniwind-resolver-'))

try {
const internalRoot = dirname(realpathSync(require.resolve('uniwind/package.json')))
const linkedRoot = join(root, 'node_modules', 'uniwind')
mkdirSync(dirname(linkedRoot), { recursive: true })
symlinkSync(internalRoot, linkedRoot, 'dir')

const originModulePath = join(linkedRoot, 'src', 'components', 'index.ts')
expect(realpathSync(originModulePath)).toBe(join(internalRoot, 'src', 'components', 'index.ts'))

const calls: Array<string> = []
const resolver: CustomResolver = (_context, moduleName) => {
calls.push(moduleName)

return {
type: 'sourceFile',
filePath: join(root, 'node_modules', moduleName, 'index.js'),
}
}
const context = {
originModulePath,
resolveRequest: resolver,
} as CustomResolutionContext

const resolution = nativeResolver({
context,
moduleName: 'react-native',
platform: 'ios',
resolver,
})

expect(calls).toEqual(['react-native'])
expect(resolution.filePath).toBe(join(root, 'node_modules', 'react-native', 'index.js'))
} finally {
rmSync(root, { force: true, recursive: true })
}
})
Comment thread
greptile-apps[bot] marked this conversation as resolved.