Skip to content

Commit

Permalink
feat: support custom resolveRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiqingchen committed Mar 6, 2024
1 parent 045a92f commit 2946064
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
29 changes: 23 additions & 6 deletions packages/taro-rn-supporter/src/Support.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import resolveReactNativePath from '@react-native-community/cli-config/build/resolveReactNativePath'
import { findProjectRoot } from '@react-native-community/cli-tools'
import { mergeConfig, MetroConfig } from 'metro'
import * as os from 'os'
import * as path from 'path'

Expand Down Expand Up @@ -42,13 +43,13 @@ export function getTransformer (opt: Options = {}) {
return transform
}

export function getResolver (opt: Options = {}, config: IProjectConfig) {
export function getResolver (opt: Options = {}, config: IProjectConfig, resolveRequest?: any) {
const blockList = getBlockList(config)
const handleEntryFile = (opt.fromRunner ?? true) ? handleTaroFile : handleFile
const resolver: any = {
sourceExts: ['ts', 'tsx', 'js', 'jsx', 'scss', 'sass', 'less', 'css', 'pcss', 'json', 'styl', 'cjs', 'svgx'],
resolveRequest: (context, moduleName, platform) => {
return handleEntryFile(context, moduleName, platform, config)
return handleEntryFile(context, moduleName, platform, config, resolveRequest)
},
resolverMainFields: ['react-native', 'browser', 'main'],
}
Expand Down Expand Up @@ -97,7 +98,7 @@ export const shareObject:ShareObject = {
metroServerInstance: null
}

export async function getMetroConfig (opt: Options = {}) {
export async function getMetroConfig (opt: Options = {}, toMergeConfig?: MetroConfig): Promise<MetroConfig> {
const config = await getProjectConfig()
const rnConfig = config.rn || {}
const entry = rnConfig?.entry || 'app'
Expand All @@ -108,9 +109,9 @@ export async function getMetroConfig (opt: Options = {}) {
shareObject.qr = opt.qr ?? false
shareObject.entry = entry
shareObject.cacheStore = cacheStore
return {
const taroMetroConfig = {
transformer: getTransformer(opt),
resolver: getResolver(opt, config),
resolver: getResolver(opt, config, toMergeConfig?.resolver?.resolveRequest),
serializer: {
// We can include multiple copies of InitializeCore here because metro will
// only add ones that are already part of the bundle
Expand All @@ -126,9 +127,25 @@ export async function getMetroConfig (opt: Options = {}) {
server: {
enhanceMiddleware: (Middleware, Server) => {
shareObject.metroServerInstance = Server
// @ts-ignore
shareObject.port = Server._config.server.port
return Middleware
}
}
},
}
if (!toMergeConfig) return taroMetroConfig

const blockListTaro = taroMetroConfig.resolver?.blockList
const blockListMerge = toMergeConfig.resolver?.blockList

const finalConfig = mergeConfig(taroMetroConfig, toMergeConfig, {
resolver: {
blockList: [
...(blockListTaro instanceof RegExp ? [blockListTaro] : blockListTaro || []),
...(blockListMerge instanceof RegExp ? [blockListMerge] : blockListMerge || []),
],
resolveRequest: taroMetroConfig.resolver?.resolveRequest,
}
})
return finalConfig
}
8 changes: 4 additions & 4 deletions packages/taro-rn-supporter/src/taroResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ function searchReactNativeModule (moduleName: string, platform: string): string
* resolveRequest 文件处理,alias,文件后缀加载等
* metro 0.70 type ResolveRequestFunc = (context, moduleName, platform) => any
*/
function handleFile (context: CustomResolutionContext, moduleName, platform, config) {
function handleFile (context: CustomResolutionContext, moduleName, platform, config, resolveRequest?) {
// 处理 alias
moduleName = resolvePathFromAlias(moduleName, config)

// 处理后缀 .rn.ts
moduleName = resolveExtFile(context, moduleName, platform, config)
return context.resolveRequest(context, moduleName, platform)
return (resolveRequest || context.resolveRequest)(context, moduleName, platform)
}

// rn runner调用
function handleTaroFile (context: CustomResolutionContext, moduleName, platform, config) {
function handleTaroFile (context: CustomResolutionContext, moduleName, platform, config, resolveRequest?) {
const newContext = { ...context }
if (context.originModulePath === require.resolve(entryFilePath)) {
// node_modules/@tarojs/rn-supporter/entry-file.js
Expand All @@ -87,7 +87,7 @@ function handleTaroFile (context: CustomResolutionContext, moduleName, platform,
'./index.js'
))
}
return handleFile(newContext, moduleName, platform, config)
return handleFile(newContext, moduleName, platform, config, resolveRequest)
}

export {
Expand Down

0 comments on commit 2946064

Please sign in to comment.