Skip to content

Commit

Permalink
chore: add antd plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohuoni committed Mar 2, 2022
1 parent 96c6086 commit edd4cba
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-falcons-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@alita/plugins': patch
---

chore: add antd plugin
142 changes: 138 additions & 4 deletions packages/plugins/src/antd.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,145 @@
import modelPlugin from '@umijs/plugins/dist/antd';
import { logger } from '@umijs/utils';
//TODO: 从 umi-next 复制过来,因为修复的bug umi-next 为发包
// import modelPlugin from '@umijs/plugins/dist/antd';
// import { logger } from '@umijs/utils';
// import { AlitaApi } from 'alita';

// export default (api: AlitaApi) => {
// api.onStart(() => {
// logger.info('Using Antd Plugin');
// });
// // @ts-ignore
// modelPlugin(api);
// };

import { logger, Mustache } from '@umijs/utils';
import { AlitaApi } from 'alita';
import { dirname } from 'path';
import { resolveProjectDep } from './utils/resolveProjectDep';
import { withTmpPath } from './utils/withTmpPath';

export default (api: AlitaApi) => {
api.onStart(() => {
logger.info('Using Antd Plugin');
});
// @ts-ignore
modelPlugin(api);
const pkgPath =
resolveProjectDep({
pkg: api.pkg,
cwd: api.cwd,
dep: 'antd',
}) || dirname(require.resolve('antd/package.json'));

api.describe({
config: {
schema(Joi) {
return Joi.object({
configProvider: Joi.object(),
// themes
dark: Joi.boolean(),
compact: Joi.boolean(),
// babel-plugin-import
import: Joi.boolean(),
// less or css, default less
style: Joi.string().allow('less', 'css'),
});
},
},
enableBy: api.EnableBy.config,
});

api.modifyAppData((memo) => {
const version = require(`${pkgPath}/package.json`).version;
memo.antd = {
pkgPath,
version,
};
return memo;
});

api.modifyConfig((memo) => {
// antd import
memo.alias.antd = pkgPath;

// moment > dayjs
if (memo.antd.dayjs) {
memo.alias.moment = dirname(require.resolve('dayjs/package.json'));
}

// dark mode & compact mode
if (memo.antd.dark || memo.antd.compact) {
const { getThemeVariables } = require('antd/dist/theme');
memo.theme = {
...getThemeVariables(memo.antd),
...memo.theme,
};
}

return memo;
});

// babel-plugin-import
api.addExtraBabelPlugins(() => {
const style = api.config.antd.style || 'less';
return api.config.antd.import && !api.appData.vite
? [
[
require.resolve('babel-plugin-import'),
{
libraryName: 'antd',
libraryDirectory: 'es',
style: style === 'less' ? true : 'css',
},
],
]
: [];
});

// antd config provider
api.onGenerateFiles(() => {
if (!api.config.antd.configProvider) return;
api.writeTmpFile({
path: `runtime.tsx`,
content: Mustache.render(
`
import { ConfigProvider, Modal, message, notification } from 'antd';
export function rootContainer(container) {
const finalConfig = {...{{{ config }}}}
if (finalConfig.prefixCls) {
Modal.config({
rootPrefixCls: finalConfig.prefixCls
});
message.config({
prefixCls: \`\${finalConfig.prefixCls}-message\`
});
notification.config({
prefixCls: \`\${finalConfig.prefixCls}-notification\`
});
}
return <ConfigProvider {...finalConfig}>{container}</ConfigProvider>;
}
`.trim(),
{
config: JSON.stringify(api.config.antd.configProvider),
},
),
});
});
api.addRuntimePlugin(() => {
return api.config.antd.configProvider
? [withTmpPath({ api, path: 'runtime.tsx' })]
: [];
});

// import antd style if antd.import is not configured
api.addEntryImportsAhead(() => {
const style = api.config.antd.style || 'less';
return api.config.antd.import && !api.appData.vite
? []
: [
{
source:
style === 'less' ? 'antd/dist/antd.less' : 'antd/dist/antd.css',
},
];
});
};

0 comments on commit edd4cba

Please sign in to comment.