UNPKG

@umijs/plugins

Version:
160 lines (158 loc) 5.35 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/react-query.ts var react_query_exports = {}; __export(react_query_exports, { default: () => react_query_default }); module.exports = __toCommonJS(react_query_exports); var import_utils = require("@umijs/utils"); var import_path = require("path"); var import_resolveProjectDep = require("./utils/resolveProjectDep"); var import_withTmpPath = require("./utils/withTmpPath"); var react_query_default = (api) => { 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 }); let pkgPath; const defaultPkgPath = (0, import_utils.winPath)( (0, import_path.dirname)(require.resolve("@tanstack/react-query/package.json")) ); const devtoolPkgPath = (0, import_utils.winPath)( (0, import_path.dirname)(require.resolve("@tanstack/react-query-devtools/package.json")) ); try { const localQueryPath = (0, import_resolveProjectDep.resolveProjectDep)({ pkg: api.pkg, cwd: api.cwd, dep: "@tanstack/react-query" }); pkgPath = localQueryPath ? (0, import_utils.winPath)(localQueryPath) : defaultPkgPath; } catch (e) { throw new Error(`[reactQuery] package resolve failed, ${e.message}`); } api.onStart(() => { if (pkgPath !== defaultPkgPath) { api.logger.info(`[reactQuery] use local package ${pkgPath}`); } }); api.addRuntimePlugin(() => { return [(0, import_withTmpPath.withTmpPath)({ api, path: "runtime.tsx" })]; }); api.addRuntimePluginKey(() => { return ["reactQuery"]; }); api.onGenerateFiles(() => { var _a; const enableDevTools = api.config.reactQuery.devtool !== false; const enableQueryClient = api.config.reactQuery.queryClient !== false; const reactQueryRuntimeCode = ((_a = api.appData.appJS) == null ? void 0 : _a.exports.includes( "reactQuery" )) ? `import { reactQuery as reactQueryConfig } from '@/app';` : `const reactQueryConfig = {};`; api.writeTmpFile({ path: "runtime.tsx", content: enableQueryClient ? ` import { defaultContext, QueryClient, QueryClientProvider } from '${pkgPath}'; import { ReactQueryDevtools } from '${devtoolPkgPath}'; ${reactQueryRuntimeCode} const client = new QueryClient(reactQueryConfig.queryClient || {}); export function rootContainer(container) { return ( <QueryClientProvider client={client} context={defaultContext}> {container} ${enableDevTools ? "<ReactQueryDevtools context={defaultContext} initialIsOpen={false} {...(reactQueryConfig.devtool || {})} />" : ""} </QueryClientProvider> ); } ` : "" }); api.writeTmpFile({ path: "index.tsx", content: ` export { // from @tanstack/query-core QueryClient, MutationObserver, MutationCache, InfiniteQueryObserver, QueriesObserver, QueryObserver, QueryCache, // from @tanstack/react-query useIsRestoring, IsRestoringProvider, useInfiniteQuery, useIsMutating, useIsFetching, useMutation, useQueries, useQuery, QueryClientProvider, useQueryClient, QueryErrorResetBoundary, useQueryErrorResetBoundary, } from '${pkgPath}'; ` }); api.writeTmpFile({ path: "types.d.ts", content: ` export type { // from @tanstack/query-core Query, QueryState, Mutation, // from @tanstack/react-query QueriesResults, QueriesOptions, QueryErrorResetBoundaryProps, QueryClientProviderProps, ContextOptions as QueryContextOptions, UseQueryOptions, UseBaseQueryOptions, UseQueryResult, UseBaseQueryResult, UseInfiniteQueryOptions, UseMutationResult, UseMutateFunction, UseMutateAsyncFunction, UseBaseMutationResult, } from '${pkgPath}'; ` }); }); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = {});