83 lines
2.2 KiB
TypeScript
83 lines
2.2 KiB
TypeScript
import * as fs from 'node:fs';
|
|
import { buildTheme } from '../../src/theme/theme';
|
|
|
|
export default function buildThemeConfig() {
|
|
const _data = buildTheme();
|
|
// 写入时会先清空文件
|
|
fs.writeFile('./unocss.config.ts', `${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 = `
|
|
/**
|
|
* unocss defineConfig
|
|
* @link unocss: https://github.com/unocss/unocss
|
|
* @link unocss-preset-weapp: https://github.com/MellowCo/unocss-preset-weapp
|
|
* */
|
|
import presetWeapp from 'unocss-preset-weapp'
|
|
import { extractorAttributify, transformerClass } from 'unocss-preset-weapp/transformer'
|
|
import { presetIcons } from 'unocss'
|
|
|
|
import transformerDirectives from '@unocss/transformer-directives'
|
|
|
|
const { presetWeappAttributify, transformerAttributify } = extractorAttributify()
|
|
|
|
export default {
|
|
presets: [
|
|
// https://github.com/MellowCo/unocss-preset-weapp
|
|
presetWeapp(),
|
|
// attributify autocomplete
|
|
presetWeappAttributify(),
|
|
presetIcons(),
|
|
],
|
|
shortcuts: [
|
|
{
|
|
'border-base': 'border border-gray-500_10',
|
|
'center': 'flex justify-center items-center',
|
|
},
|
|
],
|
|
transformers: [
|
|
transformerDirectives({
|
|
enforce: 'pre',
|
|
}),
|
|
|
|
// https://github.com/MellowCo/unocss-preset-weapp/tree/main/src/transformer/transformerAttributify
|
|
transformerAttributify(),
|
|
|
|
// https://github.com/MellowCo/unocss-preset-weapp/tree/main/src/transformer/transformerClass
|
|
transformerClass(),
|
|
],
|
|
theme: ${JSON.stringify(theme, null, 2)}
|
|
}
|
|
`;
|
|
|
|
return fileString;
|
|
}
|