UNPKG

@umijs/plugins

Version:
237 lines (232 loc) 8.04 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/antd.ts var antd_exports = {}; __export(antd_exports, { default: () => antd_default }); module.exports = __toCommonJS(antd_exports); var import_assert = __toESM(require("assert")); var import_path = require("path"); var import_umi = require("umi"); var import_plugin_utils = require("umi/plugin-utils"); var import_resolveProjectDep = require("./utils/resolveProjectDep"); var import_withTmpPath = require("./utils/withTmpPath"); var antd_default = (api) => { let pkgPath; let antdVersion = "4.0.0"; try { pkgPath = (0, import_resolveProjectDep.resolveProjectDep)({ pkg: api.pkg, cwd: api.cwd, dep: "antd" }) || (0, import_path.dirname)(require.resolve("antd/package.json")); antdVersion = require(`${pkgPath}/package.json`).version; } catch (e) { } const appComponentAvailable = import_plugin_utils.semver.gte(antdVersion, "5.1.0"); const appConfigAvailable = import_plugin_utils.semver.gte(antdVersion, "5.3.0"); api.describe({ config: { schema({ zod }) { return zod.object({ configProvider: zod.record(zod.any()), // themes dark: zod.boolean(), compact: zod.boolean(), // babel-plugin-import import: zod.boolean(), // less or css, default less style: zod.enum(["less", "css"]).describe("less or css, default less"), theme: zod.record(zod.any()), // Only antd@5.1.0 is supported appConfig: zod.record(zod.any()).describe("Only antd@5.1.0 is supported") }).deepPartial(); } }, enableBy({ userConfig }) { return process.env.UMI_PLUGIN_ANTD_ENABLE || userConfig.antd; } }); api.addRuntimePluginKey(() => ["antd"]); function checkPkgPath() { if (!pkgPath) { throw new Error(`Can't find antd package. Please install antd first.`); } } api.modifyAppData((memo) => { checkPkgPath(); const version = require(`${pkgPath}/package.json`).version; memo.antd = { pkgPath, version }; return memo; }); api.modifyConfig((memo) => { var _a; checkPkgPath(); let antd = memo.antd || {}; if (process.env.UMI_PLUGIN_ANTD_ENABLE) { const { defaultConfig } = JSON.parse(process.env.UMI_PLUGIN_ANTD_ENABLE); memo.antd = antd = Object.assign(defaultConfig, antd); } memo.alias.antd = pkgPath; if (antd.dayjs) { memo.alias.moment = (0, import_path.dirname)(require.resolve("dayjs/package.json")); } if (antdVersion.startsWith("5")) { const theme = require("@ant-design/antd-theme-variable"); memo.theme = { ...theme, ...memo.theme }; if ((_a = memo.antd) == null ? void 0 : _a.import) { const errorMessage = `Can't set antd.import while using antd5 (${antdVersion})`; api.logger.fatal( "please change config antd.import to false, then start server again" ); throw Error(errorMessage); } } if (antd.dark || antd.compact) { const { getThemeVariables } = require("antd/dist/theme"); memo.theme = { ...getThemeVariables(antd), ...memo.theme }; } memo.theme = { "root-entry-name": "default", ...memo.theme }; if (antd.theme) { (0, import_assert.default)( antdVersion.startsWith("5"), `antd.theme is only valid when antd is 5` ); antd.configProvider ?? (antd.configProvider = {}); antd.configProvider.theme = (0, import_plugin_utils.deepmerge)( antd.configProvider.theme || {}, antd.theme ); } if (antd.appConfig) { if (!appComponentAvailable) { delete antd.appConfig; api.logger.warn( `antd.appConfig is only available in version 5.1.0 and above, but you are using version ${antdVersion}` ); } else if (!appConfigAvailable && Object.keys(antd.appConfig).length > 0) { api.logger.warn( `versions [5.1.0 ~ 5.3.0) only allows antd.appConfig to be set to \`{}\`` ); } } return memo; }); api.addExtraBabelPlugins(() => { const style = antdVersion.startsWith("5") ? false : api.config.antd.style || "less"; return api.config.antd.import && !api.appData.vite ? [ [ require.resolve("babel-plugin-import"), { libraryName: "antd", libraryDirectory: "es", ...style ? { style: style === "less" || "css" } : {} }, "antd" ] ] : []; }); api.onGenerateFiles(() => { const withConfigProvider = !!api.config.antd.configProvider; const withAppConfig = appConfigAvailable && !!api.config.antd.appConfig; api.writeTmpFile({ path: `runtime.tsx`, context: { configProvider: withConfigProvider && JSON.stringify(api.config.antd.configProvider), appConfig: appComponentAvailable && JSON.stringify(api.config.antd.appConfig) }, tplPath: (0, import_path.join)(__dirname, "../tpls/antd-runtime.ts.tpl") }); api.writeTmpFile({ path: "types.d.ts", content: import_plugin_utils.Mustache.render( ` {{#withConfigProvider}} import type { ConfigProviderProps } from 'antd/es/config-provider'; {{/withConfigProvider}} {{#withAppConfig}} import type { AppConfig } from 'antd/es/app/context'; {{/withAppConfig}} type Prettify<T> = { [K in keyof T]: T[K]; } & {}; type AntdConfig = Prettify<{} {{#withConfigProvider}} & ConfigProviderProps{{/withConfigProvider}} {{#withAppConfig}} & { appConfig: AppConfig }{{/withAppConfig}} >; export type RuntimeAntdConfig = (memo: AntdConfig) => AntdConfig; `.trim(), { withConfigProvider, withAppConfig } ) }); api.writeTmpFile({ path: import_umi.RUNTIME_TYPE_FILE_NAME, content: ` import type { RuntimeAntdConfig } from './types.d'; export type IRuntimeConfig = { antd?: RuntimeAntdConfig }; ` }); }); api.addRuntimePlugin(() => { if (api.config.antd.configProvider || appComponentAvailable && api.config.antd.appConfig) { return [(0, import_withTmpPath.withTmpPath)({ api, path: "runtime.tsx" })]; } return []; }); api.addEntryImportsAhead(() => { const style = api.config.antd.style || "less"; const doNotImportLess = api.config.antd.import && !api.appData.vite || antdVersion.startsWith("5"); return doNotImportLess ? [] : [ { source: style === "less" ? "antd/dist/antd.less" : "antd/dist/antd.css" } ]; }); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = {});