From 583d6c726956d49d7ce8f368b1d0abfa2608db4c Mon Sep 17 00:00:00 2001 From: lutinglt Date: Fri, 5 Jun 2026 12:10:34 +0800 Subject: [PATCH] organize code --- bunfig.toml | 20 +++++++++++++++- package.json | 19 ++++++++------- src/vite-plugin/index.ts | 50 +++++++++++++++++++--------------------- themes/index.ts | 2 +- vitest.config.ts | 19 +++++++++++++++ 5 files changed, 74 insertions(+), 36 deletions(-) diff --git a/bunfig.toml b/bunfig.toml index fd430c5..7fee034 100644 --- a/bunfig.toml +++ b/bunfig.toml @@ -1,3 +1,21 @@ -# bunfig.toml +# +# Copyright (c) https://github.com/lutinglt +# +# See the NOTICE file distributed with this work for additional +# information regarding copyright ownership. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + [install] registry = "https://registry.npmjs.org" \ No newline at end of file diff --git a/package.json b/package.json index a98e5a0..6c9bb9e 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,15 @@ { "name": "@lutinglt/gitea-github-theme", + "version": "1.26.2", + "license": "Apache-2.0", + "author": { + "name": "lutinglt", + "url": "https://github.com/lutinglt" + }, + "type": "module", + "engines": { + "bun": ">=1.3.14" + }, "exports": { "./theme.config.ts": "./theme.config.ts", "./core": "./src/index.ts", @@ -9,17 +19,10 @@ "./themes": "./themes/index.ts", "./primer": "./primer/index.ts" }, - "version": "1.26.2", - "license": "Apache-2.0", - "author": { - "name": "lutinglt", - "url": "https://github.com/lutinglt" - }, - "type": "module", "scripts": { "lint": "bun eslint .", "format": "bun prettier --write .", - "bundle": "tsgo -b -v && bunx --bun vite build", + "bundle": "tsgo -b && bunx --bun vite build", "commit": "bun format && bun lint && bun test && bun bundle", "version": "bun scripts/version.ts", "release": "bun bundle && bun update:locale", diff --git a/src/vite-plugin/index.ts b/src/vite-plugin/index.ts index 30ac179..fbbae07 100644 --- a/src/vite-plugin/index.ts +++ b/src/vite-plugin/index.ts @@ -22,17 +22,19 @@ import fs from "node:fs"; import path from "node:path"; import type { PluginOption } from "vite"; import type { GiteaThemeMeta, Theme } from "../core"; -import { createTheme, createThemeMetaInfo } from "../core"; -import { flushCSS } from "../core/css"; +import { createTheme, createThemeMetaInfo, flushCSS } from "../core"; import { compileCSS } from "../vanilla-extract"; import { buildFullDisplayName, buildFullThemeName } from "./utils"; -type ThemeEntry = { themeName: string; meta: GiteaThemeMeta; theme: Theme }; -type AutoThemeEntry = { darkThemeName: string; lightThemeName: string }; +type ThemeEntry = { + themeName: string; + meta: GiteaThemeMeta; + theme?: Theme; + autoTheme?: { darkThemeName: string; lightThemeName: string }; +}; export function giteaGitHubTheme(): PluginOption { - const themeEntries = new Array(); - const autoThemeEntries = new Map(); + const themeEntries: ThemeEntry[] = []; return { name: "lutinglt-gitea-github-theme", @@ -41,7 +43,7 @@ export function giteaGitHubTheme(): PluginOption { // 1. 动态导入 theme.config.ts(Bun 原生 TS + exports 自引用) const themeConfig = (await import("@lutinglt/gitea-github-theme/theme.config.ts")).default; - // 2. 解析主题条目 + // 2. 解析主题条目 + 自动主题条目 for (const themeSeries of themeConfig) { for (const [themeKeyName, entry] of Object.entries(themeSeries.themes)) { const name = entry.themeName ?? themeKeyName; @@ -57,25 +59,23 @@ export function giteaGitHubTheme(): PluginOption { theme: entry.theme, }); } - } - // 3. 解析自动主题条目 - for (const themeSeries of themeConfig) { if (themeSeries.themes.dark && themeSeries.themes.light) { const themeName = buildFullThemeName(themeSeries, "auto"); - const meta: GiteaThemeMeta = { - colorScheme: "auto", - displayName: themeSeries.autoTheme?.displayName - ? `GitHub ${themeSeries.autoTheme.displayName}` - : buildFullDisplayName(themeSeries, "auto"), - colorblindType: themeSeries.autoTheme?.colorblindType, - }; - autoThemeEntries.set(themeName, { - darkThemeName: themeSeries.themes.dark.themeName!, - lightThemeName: themeSeries.themes.light.themeName!, + themeEntries.push({ + themeName, + meta: { + colorScheme: "auto", + displayName: themeSeries.autoTheme?.displayName + ? `GitHub ${themeSeries.autoTheme.displayName}` + : buildFullDisplayName(themeSeries, "auto"), + colorblindType: themeSeries.autoTheme?.colorblindType, + }, + autoTheme: { + darkThemeName: themeSeries.themes.dark.themeName!, + lightThemeName: themeSeries.themes.light.themeName!, + }, }); - // 将自动主题也加入 themeEntries 以便编译 CSS - themeEntries.push({ themeName, meta, theme: null as unknown as Theme }); } } @@ -101,14 +101,12 @@ export function giteaGitHubTheme(): PluginOption { // 编译每个主题的 CSS 并输出 for (const entry of themeEntries) { - const { themeName, meta } = entry; - const autoTheme = autoThemeEntries.get(themeName); + const { themeName, meta, autoTheme } = entry; const rawThemeCSS = Buffer.from( compileCSS(() => { createThemeMetaInfo(meta); - // 自动主题不需要颜色变量, 只有 metaInfo - if (!autoTheme) createTheme(entry.theme); + if (entry.theme) createTheme(entry.theme); }) ); const themeCSS = transform({ filename: themeName + ".css", code: rawThemeCSS, minify: true }).code.toString(); diff --git a/themes/index.ts b/themes/index.ts index 2685e86..27e4742 100644 --- a/themes/index.ts +++ b/themes/index.ts @@ -1,5 +1,5 @@ /*! - * Copyright (c) https://com/lutinglt + * Copyright (c) https://github.com/lutinglt * * See the NOTICE file distributed with this work for additional * information regarding copyright ownership. diff --git a/vitest.config.ts b/vitest.config.ts index 1ff6c9d..0b9d35c 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,3 +1,22 @@ +/*! + * Copyright (c) https://github.com/lutinglt + * + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + import { defineConfig } from "vitest/config"; export default defineConfig({