uniViteTemplate/vite/plugins/build.unocss.config.ts
2024-08-28 20:39:20 +08:00

59 lines
1.6 KiB
TypeScript

import * as fs from 'node:fs';
import { buildTheme } from '../../src/theme/theme';
export default function buildThemeConfig() {
const fileName = './tailwind.config.ts';
const _data = buildTheme();
// 写入时会先清空文件
fs.writeFile(fileName, `${createFileByTemplate(_data)}`, (err: any) => {
if (err) {
console.error(err);
} else {
console.log('构建 css 提示文件完成');
console.table(flatten(_data));
}
});
// 写完文件后使用编辑器格式化一下
}
function flatten(obj: any, parentKey = ''): any {
let result: any = {};
for (const [key, value] of Object.entries(obj)) {
const newKey = parentKey ? `${parentKey}.${key}` : key;
if (typeof value === 'object' && value !== null) {
const flattened = flatten(value, newKey);
result = { ...result, ...flattened };
} else {
result[newKey] = value;
}
}
return result;
}
// 通过模版生成文件
function createFileByTemplate(theme: any) {
const fileString = `
import { getIconCollections, iconsPlugin } from '@egoist/tailwindcss-icons';
import { isMp } from './build/platform';
/** @type {import('tailwindcss').Config} */
export default {
content: ['./public/index.html', './src/**/**.{html,js,ts,jsx,tsx,vue}'],
corePlugins: {
// 小程序去使用 h5 的 preflight 和响应式 container 没有意义
preflight: !isMp,
container: !isMp,
},
plugins: [iconsPlugin({
collections: getIconCollections(['mdi', 'svg-spinners']),
})],
theme: ${JSON.stringify(theme, null, 4)}
};
`;
return fileString;
}