Skip to content

Commit

Permalink
remove option and add default themes
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshuai2144 committed Mar 10, 2020
1 parent 9993bfc commit 1360489
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 44 deletions.
61 changes: 28 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
<!-- @format -->

# umi-plugin-antd-theme

[![NPM version](https://img.shields.io/npm/v/umi-plugin-antd-theme.svg?style=flat)](https://npmjs.org/package/umi-plugin-antd-theme) [![NPM downloads](http://img.shields.io/npm/dm/umi-plugin-antd-theme.svg?style=flat)](https://npmjs.org/package/umi-plugin-antd-theme)

## Usage

Configure in `.umirc.js`,

```js
export default {
plugins: [
[
'umi-plugin-antd-theme',
{
theme: [
{
theme: 'dark',
fileName: 'dark.css',
},
{
fileName: 'mingQing.css',
modifyVars: {
'@primary-color': '#13C2C2',
},
},
],
// 是否压缩css
min: true,
// css module
isModule: true,
// 忽略 antd 的依赖
ignoreAntd: false,
// 忽略 pro-layout
ignoreProLayout: false,
// 不使用缓存
cache: true,
},
],
Configure in `config/theme.config.json`,

```json
{
"theme": [
{
"theme": "dark",
"fileName": "dark.css"
},
{
"fileName": "mingQing.css",
"modifyVars": {
"@primary-color": "#13C2C2"
}
}
],
};
// 是否压缩css
"min": true,
// css module
"isModule": true,
// 忽略 antd 的依赖
"ignoreAntd": false,
// 忽略 pro-layout
"ignoreProLayout": false,
// 不使用缓存
"cache": true
}
```

you can get config in `window.umi_plugin_ant_themeVar`
Expand Down
117 changes: 117 additions & 0 deletions src/defaultTheme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/** @format */

export default {
theme: [
{
key: 'dark',
fileName: 'dark.css',
theme: 'dark',
},
{
key: 'dust',
fileName: 'dust.css',
modifyVars: {
'@primary-color': '#F5222D',
},
},
{
key: 'volcano',
fileName: 'volcano.css',
modifyVars: {
'@primary-color': '#FA541C',
},
},
{
key: 'sunset',
fileName: 'sunset.css',
modifyVars: {
'@primary-color': '#FAAD14',
},
},
{
key: 'cyan',
fileName: 'cyan.css',
modifyVars: {
'@primary-color': '#13C2C2',
},
},
{
key: 'green',
fileName: 'green.css',
modifyVars: {
'@primary-color': '#52C41A',
},
},
{
key: 'geekblue',
fileName: 'geekblue.css',
modifyVars: {
'@primary-color': '#2F54EB',
},
},
{
key: 'purple',
fileName: 'purple.css',
modifyVars: {
'@primary-color': '#722ED1',
},
},

{
key: 'dust',
theme: 'dark',
fileName: 'dark-dust.css',
modifyVars: {
'@primary-color': '#F5222D',
},
},
{
key: 'volcano',
theme: 'dark',
fileName: 'dark-volcano.css',
modifyVars: {
'@primary-color': '#FA541C',
},
},
{
key: 'sunset',
theme: 'dark',
fileName: 'dark-sunset.css',
modifyVars: {
'@primary-color': '#FAAD14',
},
},
{
key: 'cyan',
theme: 'dark',
fileName: 'dark-cyan.css',
modifyVars: {
'@primary-color': '#13C2C2',
},
},
{
key: 'green',
theme: 'dark',
fileName: 'dark-green.css',
modifyVars: {
'@primary-color': '#52C41A',
},
},
{
key: 'geekblue',
theme: 'dark',
fileName: 'dark-geekblue.css',
modifyVars: {
'@primary-color': '#2F54EB',
},
},
{
key: 'purple',
theme: 'dark',
fileName: 'dark-purple.css',
modifyVars: {
'@primary-color': '#722ED1',
},
},
],
};
31 changes: 20 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,40 @@ import { join } from 'path';
import serveStatic from 'serve-static';
import rimraf from 'rimraf';
import { existsSync, mkdirSync } from 'fs';
import defaultTheme from './defaultTheme';

const buildCss = require('antd-pro-merge-less');
const winPath = require('slash2');

interface themeConfig {
theme: 'dark' | 'light';
theme?: string;
fileName: string;
modifyVars: { [key: string]: string };
key: string;
modifyVars?: { [key: string]: string };
}

export default function(
api: IApi,
options: {
export default function(api: IApi) {
// 给一个默认的配置
let options: {
theme: themeConfig[];
min: boolean;
},
) {
min?: boolean;
} = defaultTheme;

// 从固定的路径去读取配置,而不是从 config 中读取
const themeConfigPath = winPath(join(api.paths.cwd, 'config/theme.config.json'));
if (existsSync(themeConfigPath)) {
options = require(themeConfigPath);
}
const { cwd, absOutputPath, absNodeModulesPath } = api.paths;
const outputPath = winPath(join(cwd, absOutputPath));
const themeTemp = winPath(join(absNodeModulesPath, '.plugin-theme'));

// 增加中间件
api.addMiddewares(() => {
return serveStatic(themeTemp);
});

// 增加一个对象,用于 layout 的配合
api.addHTMLHeadScripts(() => [
`window.umi_plugin_ant_themeVar = ${JSON.stringify(options.theme)}`,
]);
Expand Down Expand Up @@ -61,8 +70,8 @@ export default function(
{
min: true,
...options,
},
),
}
)
)
.then(() => {
api.logger.log('🎊 build theme success');
Expand Down Expand Up @@ -100,7 +109,7 @@ export default function(
})),
{
...options,
},
}
)
.then(() => {
api.logger.log('🎊 build theme success');
Expand Down

1 comment on commit 1360489

@chenshuai2144
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.