Skip to content
Merged
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
12 changes: 8 additions & 4 deletions packages/uniwind/src/bundler/adapters/metro/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ export const nativeResolver = ({
const resolution = resolver(context, moduleName, platform)

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}`)
const nodeModulesPath = context.originModulePath.split(`${sep}node_modules${sep}`).at(-1) ?? ''
const isFromNodeModules = nodeModulesPath !== context.originModulePath
const isFromReactNative = nodeModulesPath.startsWith(`react-native${sep}`)
|| nodeModulesPath.startsWith(`@react-native${sep}`)
Comment thread
Brentlok marked this conversation as resolved.
const isReactNativeAnimated = context.originModulePath.includes(`${sep}Animated${sep}components${sep}`)

if (
Expand Down Expand Up @@ -113,11 +114,14 @@ export const webResolver = ({
resolver,
}: ResolverConfig) => {
const resolution = resolver(context, moduleName, platform)
const resolvedNodeModulesPath = resolution.type === 'sourceFile'
? resolution.filePath.split(`${sep}node_modules${sep}`).at(-1) ?? ''
: ''

if (
isInternalOrigin(context.originModulePath)
|| resolution.type !== 'sourceFile'
|| !resolution.filePath.includes(`${sep}react-native-web${sep}`)
|| !resolvedNodeModulesPath.startsWith(`react-native-web${sep}`)
Comment thread
Brentlok marked this conversation as resolved.
) {
return resolution
}
Expand Down
84 changes: 83 additions & 1 deletion packages/uniwind/tests/native/bundler/resolvers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,89 @@ 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'
import { nativeResolver, webResolver } from '../../../src/bundler/adapters/metro/resolvers'

test('rewrites dependency imports when the project path contains a react-native directory', () => {
const root = join(tmpdir(), 'react-native', 'my-app')
const originModulePath = join(root, 'node_modules', 'heroui-native', 'lib', 'surface.js')
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', 'uniwind/components'])
expect(resolution).toMatchObject({
type: 'sourceFile',
filePath: join(root, 'node_modules', 'uniwind/components', 'index.js'),
})
})

test('does not rewrite unrelated web modules when the project path contains react-native-web', () => {
const root = join(tmpdir(), 'react-native-web', 'my-app')
const calls: Array<string> = []
const resolver: CustomResolver = (_context, moduleName) => {
calls.push(moduleName)

return {
type: 'sourceFile',
filePath: join(root, 'node_modules', 'other-package', 'View', 'index.js'),
}
}
const context = {
originModulePath: join(root, 'src', 'App.tsx'),
resolveRequest: resolver,
} as CustomResolutionContext

const resolution = webResolver({
context,
moduleName: 'other-package/View',
platform: 'web',
resolver,
})

expect(calls).toEqual(['other-package/View'])
expect(resolution).toMatchObject({
type: 'sourceFile',
filePath: join(root, 'node_modules', 'other-package', 'View', 'index.js'),
})
})

test('rewrites React Native Web component files', () => {
const root = join(tmpdir(), 'my-app')
const calls: Array<string> = []
const resolver: CustomResolver = (_context, moduleName) => {
calls.push(moduleName)

return {
type: 'sourceFile',
filePath: join(root, 'node_modules', 'react-native-web', 'dist', 'exports', 'View', 'index.js'),
}
}
const context = {
originModulePath: join(root, 'src', 'App.tsx'),
resolveRequest: resolver,
} as CustomResolutionContext

webResolver({ context, moduleName: 'react-native-web', platform: 'web', resolver })

expect(calls).toEqual(['react-native-web', 'uniwind/components/View'])
})

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