From 29460642fab1c2a48b97360735d17f2d3c078cc1 Mon Sep 17 00:00:00 2001 From: tony chen Date: Wed, 6 Mar 2024 14:34:33 +0800 Subject: [PATCH] feat: support custom resolveRequest --- packages/taro-rn-supporter/src/Support.ts | 29 +++++++++++++++---- .../taro-rn-supporter/src/taroResolver.ts | 8 ++--- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/packages/taro-rn-supporter/src/Support.ts b/packages/taro-rn-supporter/src/Support.ts index e00583f34b30..c1b2b5b31bdb 100644 --- a/packages/taro-rn-supporter/src/Support.ts +++ b/packages/taro-rn-supporter/src/Support.ts @@ -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' @@ -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'], } @@ -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 { const config = await getProjectConfig() const rnConfig = config.rn || {} const entry = rnConfig?.entry || 'app' @@ -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 @@ -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 } diff --git a/packages/taro-rn-supporter/src/taroResolver.ts b/packages/taro-rn-supporter/src/taroResolver.ts index 4e66bc94523b..7915712e8f15 100644 --- a/packages/taro-rn-supporter/src/taroResolver.ts +++ b/packages/taro-rn-supporter/src/taroResolver.ts @@ -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 @@ -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 {