Skip to content

Commit

Permalink
chore(react-query): skip get pkg path when plugin is disabled (#12315)
Browse files Browse the repository at this point in the history
* chore(react-query): skip get pkg path when plugin is disabled

* chore: remove log

* chore: type
  • Loading branch information
fz6m authored May 15, 2024
1 parent d0ca58d commit f64d639
Showing 1 changed file with 107 additions and 43 deletions.
150 changes: 107 additions & 43 deletions packages/plugins/src/react-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,57 @@ import { isFlattedNodeModulesDir } from './utils/npmClient';
import { resolveProjectDep } from './utils/resolveProjectDep';
import { withTmpPath } from './utils/withTmpPath';

export default (api: IApi) => {
api.describe({
key: 'reactQuery',
config: {
schema({ zod }) {
return zod
.object({
devtool: zod.union([zod.record(zod.any()), zod.boolean()]),
queryClient: zod.union([zod.record(zod.any()), zod.boolean()]),
})
.deepPartial();
},
},
enableBy: api.EnableBy.config,
});
interface IReactQueryInfo {
/**
* react query package dir path
*/
pkgPath: string;
/**
* react query devtools package dir path
*/
devtoolsPkgPath: string;
/**
* (internal) react query package dir path
*/
defaultPkgPath: string;
/**
* (internal) react query devtools package dir path
*/
defaultDevtoolPkgPath: string;
/**
* current using react query version
*/
pkgVersion: string;
/**
* can use devtools when devtools package is installed
*/
canUseDevtools: boolean;
/**
* use v4 react query
*/
useV4: boolean;
/**
* use v5 react query
*/
useV5: boolean;
/**
* react query core package dir path
*/
corePath?: string;
}

let reactQueryInfo: IReactQueryInfo;

const REACT_QUERY_DEP_NAME = '@tanstack/react-query';
const REACT_QUERY_DEVTOOLS_DEP_NAME = '@tanstack/react-query-devtools';
const REACT_QUERY_CORE_DEP_NAME = '@tanstack/query-core';
const getReactQueryPkgInfo = (api: IApi) => {
if (reactQueryInfo) {
return reactQueryInfo;
}
let pkgPath: string;
let devtoolsPkgPath: string;
const REACT_QUERY_DEP_NAME = '@tanstack/react-query';
const REACT_QUERY_DEVTOOLS_DEP_NAME = '@tanstack/react-query-devtools';
let corePath: string | undefined;
const defaultPkgPath = winPath(
dirname(require.resolve(`${REACT_QUERY_DEP_NAME}/package.json`)),
);
Expand All @@ -44,6 +75,16 @@ export default (api: IApi) => {
`[reactQuery] package '${REACT_QUERY_DEP_NAME}' resolve failed, ${e.message}`,
);
}
// resolve RQ core
try {
corePath = winPath(
dirname(
require.resolve(`${REACT_QUERY_CORE_DEP_NAME}/package.json`, {
paths: [pkgPath],
}),
),
);
} catch {}
// resolve RQ devtools
try {
const localDevtoolsPkgPath = resolveProjectDep({
Expand Down Expand Up @@ -72,7 +113,41 @@ export default (api: IApi) => {
const useV5Devtools = devtoolsVersion.startsWith('5');
const canUseDevtools = (useV4 && useV4Devtools) || (useV5 && useV5Devtools);

// set cache
reactQueryInfo = {
pkgPath,
devtoolsPkgPath,
defaultPkgPath,
defaultDevtoolPkgPath,
pkgVersion,
canUseDevtools,
useV4,
useV5,
corePath,
};
return reactQueryInfo;
};

export default (api: IApi) => {
api.describe({
key: 'reactQuery',
config: {
schema({ zod }) {
return zod
.object({
devtool: zod.union([zod.record(zod.any()), zod.boolean()]),
queryClient: zod.union([zod.record(zod.any()), zod.boolean()]),
})
.deepPartial();
},
},
enableBy: api.EnableBy.config,
});

api.onStart(() => {
getReactQueryPkgInfo(api);

const { pkgPath, defaultPkgPath, pkgVersion } = reactQueryInfo;
if (pkgPath !== defaultPkgPath && !process.env.IS_UMI_BUILD_WORKER) {
api.logger.info(`[reactQuery] use local package, version: ${pkgVersion}`);
}
Expand All @@ -88,6 +163,8 @@ export default (api: IApi) => {

// alias
api.modifyConfig((memo) => {
const { pkgPath, devtoolsPkgPath, canUseDevtools } =
getReactQueryPkgInfo(api);
memo.alias[REACT_QUERY_DEP_NAME] = pkgPath;
if (canUseDevtools) {
memo.alias[REACT_QUERY_DEVTOOLS_DEP_NAME] = devtoolsPkgPath;
Expand All @@ -96,6 +173,8 @@ export default (api: IApi) => {
});

api.onGenerateFiles(() => {
const { pkgPath, devtoolsPkgPath, canUseDevtools, useV4, useV5 } =
getReactQueryPkgInfo(api);
const enableDevTools =
api.config.reactQuery.devtool !== false && canUseDevtools;
const enableQueryClient = api.config.reactQuery.queryClient !== false;
Expand Down Expand Up @@ -187,7 +266,7 @@ export {
`,
});

const exportTypes: string[] = [
const exportTypes = [
// from @tanstack/query-core
'Query',
'QueryState',
Expand All @@ -207,7 +286,7 @@ export {
'UseMutateFunction',
'UseMutateAsyncFunction',
'UseBaseMutationResult',
].filter(Boolean);
].filter(Boolean) as string[];

api.writeTmpFile({
path: 'types.d.ts',
Expand Down Expand Up @@ -248,32 +327,17 @@ export type RuntimeReactQueryType = {

// v5
const isFlattedDepsDir = isFlattedNodeModulesDir(api);
if (useV5 && !isFlattedDepsDir) {
let corePath: string;
const REACT_QUERY_CORE_DEP_NAME = '@tanstack/query-core';

// resolve RQ core
try {
corePath = winPath(
dirname(
require.resolve(`${REACT_QUERY_CORE_DEP_NAME}/package.json`, {
paths: [pkgPath],
}),
),
);
} catch (e: any) {
throw new Error(
`[reactQuery] package '${REACT_QUERY_CORE_DEP_NAME}' resolve failed, ${e.message}`,
);
}

if (!isFlattedDepsDir) {
api.modifyTSConfig((config) => {
const { corePath, useV5 } = getReactQueryPkgInfo(api);
// if without the source of `@tanstack/query-core`, the IDE can't find the types
lodash.set(
config,
`compilerOptions.paths["${REACT_QUERY_CORE_DEP_NAME}"]`,
[corePath],
);
if (useV5 && corePath?.length) {
lodash.set(
config,
`compilerOptions.paths["${REACT_QUERY_CORE_DEP_NAME}"]`,
[corePath],
);
}
return config;
});
}
Expand Down

0 comments on commit f64d639

Please sign in to comment.