From 16ed5a05bae33b08b2ed395d5a7a0f9e3edc7a4a Mon Sep 17 00:00:00 2001 From: vben Date: Sun, 23 Jun 2024 19:45:40 +0800 Subject: [PATCH] feat: add license plugin --- .github/semantic.yml | 13 ++++ internal/node-utils/package.json | 1 + internal/node-utils/src/date.ts | 10 +++ internal/node-utils/src/index.ts | 10 +-- .../vite-config/src/config/application.ts | 1 + .../src/plugins/extra-app-config.ts | 4 +- internal/vite-config/src/plugins/index.ts | 7 +- internal/vite-config/src/plugins/license.ts | 76 +++++++++++++++++++ internal/vite-config/src/typing.ts | 2 + package.json | 5 +- .../components/normal-menu/normal-menu.vue | 3 +- .../components/chrome-tabs/chrome-tabs.scss | 39 ---------- .../components/chrome-tabs/tab-background.vue | 23 ++++-- .../src/components/chrome-tabs/tab.vue | 5 +- .../src/components/chrome-tabs/tabs.vue | 8 +- pnpm-lock.yaml | 23 +++--- 16 files changed, 156 insertions(+), 74 deletions(-) create mode 100644 .github/semantic.yml create mode 100644 internal/node-utils/src/date.ts create mode 100644 internal/vite-config/src/plugins/license.ts diff --git a/.github/semantic.yml b/.github/semantic.yml new file mode 100644 index 00000000..8d8ffd95 --- /dev/null +++ b/.github/semantic.yml @@ -0,0 +1,13 @@ +titleAndCommits: true +types: + - feat + - fix + - docs + - chore + - style + - refactor + - perf + - test + - build + - ci + - revert diff --git a/internal/node-utils/package.json b/internal/node-utils/package.json index cbfa37af..9daa2bcc 100644 --- a/internal/node-utils/package.json +++ b/internal/node-utils/package.json @@ -31,6 +31,7 @@ "@changesets/git": "^3.0.0", "@manypkg/get-packages": "^2.2.1", "consola": "^3.2.3", + "dayjs": "^1.11.11", "find-up": "^7.0.0", "nanoid": "^5.0.7", "pkg-types": "^1.1.1", diff --git a/internal/node-utils/src/date.ts b/internal/node-utils/src/date.ts new file mode 100644 index 00000000..6cc02745 --- /dev/null +++ b/internal/node-utils/src/date.ts @@ -0,0 +1,10 @@ +import dayjs from 'dayjs'; +import timezone from 'dayjs/plugin/timezone'; +import utc from 'dayjs/plugin/utc'; + +dayjs.extend(utc); +dayjs.extend(timezone); + +const dateUtil = dayjs().tz('Asia/Shanghai'); + +export { dateUtil }; diff --git a/internal/node-utils/src/index.ts b/internal/node-utils/src/index.ts index 9decb5a2..73f603d8 100644 --- a/internal/node-utils/src/index.ts +++ b/internal/node-utils/src/index.ts @@ -1,13 +1,9 @@ -export { UNICODE } from './constants'; +export * from './constants'; +export * from './date'; export * from './git'; export { add as gitAdd, getStagedFiles } from './git'; export { generatorContentHash } from './hash'; -export { - findMonorepoRoot, - getPackage, - getPackages, - getPackagesSync, -} from './monorepo'; +export * from './monorepo'; export { toPosixPath } from './path'; export { prettierFormat } from './prettier'; export type { Package } from '@manypkg/get-packages'; diff --git a/internal/vite-config/src/config/application.ts b/internal/vite-config/src/config/application.ts index 4f0b516e..88ccfa2b 100644 --- a/internal/vite-config/src/config/application.ts +++ b/internal/vite-config/src/config/application.ts @@ -25,6 +25,7 @@ function defineApplicationConfig(options: DefineApplicationOptions = {}) { i18n: true, injectAppLoading: true, isBuild, + license: true, mock: true, mode, pwa: true, diff --git a/internal/vite-config/src/plugins/extra-app-config.ts b/internal/vite-config/src/plugins/extra-app-config.ts index 13d98ec9..cbb70d42 100644 --- a/internal/vite-config/src/plugins/extra-app-config.ts +++ b/internal/vite-config/src/plugins/extra-app-config.ts @@ -1,11 +1,11 @@ +import type { PluginOption } from 'vite'; + import { colors, generatorContentHash, readPackageJSON, } from '@vben/node-utils'; -import { type PluginOption } from 'vite'; - import { getEnvConfig } from '../utils/env'; interface PluginOptions { diff --git a/internal/vite-config/src/plugins/index.ts b/internal/vite-config/src/plugins/index.ts index 62e3c45d..50338035 100644 --- a/internal/vite-config/src/plugins/index.ts +++ b/internal/vite-config/src/plugins/index.ts @@ -27,6 +27,7 @@ import viteVueDevTools from 'vite-plugin-vue-devtools'; import { viteExtraAppConfigPlugin } from './extra-app-config'; import { viteImportMapPlugin } from './importmap'; import { viteInjectAppLoadingPlugin } from './inject-app-loading'; +import { viteLicensePlugin } from './license'; /** * 获取条件成立的 vite 插件 @@ -94,12 +95,12 @@ async function getApplicationConditionPlugins( compress, compressTypes, extraAppConfig, - html, i18n, importmap, importmapOptions, injectAppLoading, + license, mock, pwa, pwaOptions, @@ -130,6 +131,10 @@ async function getApplicationConditionPlugins( condition: injectAppLoading, plugins: async () => [await viteInjectAppLoadingPlugin(!!isBuild, env)], }, + { + condition: license, + plugins: async () => [await viteLicensePlugin()], + }, { condition: pwa, plugins: () => diff --git a/internal/vite-config/src/plugins/license.ts b/internal/vite-config/src/plugins/license.ts new file mode 100644 index 00000000..aa5e7361 --- /dev/null +++ b/internal/vite-config/src/plugins/license.ts @@ -0,0 +1,76 @@ +import type { + NormalizedOutputOptions, + OutputAsset, + OutputBundle, + OutputChunk, +} from 'rollup'; +import type { PluginOption } from 'vite'; + +import { EOL } from 'node:os'; + +import { dateUtil, readPackageJSON } from '@vben/node-utils'; + +/** + * 用于将配置文件抽离出来并注入到项目中 + * @returns + */ + +async function viteLicensePlugin( + root = process.cwd(), +): Promise { + const { + description = '', + homepage = '', + version = '', + } = await readPackageJSON(root); + + return { + apply: 'build', + enforce: 'post', + generateBundle: { + handler: (_options: NormalizedOutputOptions, bundle: OutputBundle) => { + const date = dateUtil.format('YYYY-MM-DD '); + + const copyrightText = `/*! + * Vben Admin Pro + * Version: ${version} + * Author: vben + * Copyright (C) 2024 Vben + * License: MIT License + * Description: ${description} + * Date Created: ${date} + * Homepage: ${homepage} + * Contact: ann.vben@gmail.com +*/ + `.trim(); + + for (const [, fileContent] of Object.entries(bundle)) { + if ( + fileContent.type === 'asset' || + (fileContent.type === 'chunk' && fileContent.isEntry) + ) { + const chunkContent = fileContent as OutputChunk; + const assetContent = fileContent as OutputAsset; + // 插入版权信息 + const content = + typeof assetContent.source === 'string' + ? assetContent.source + : chunkContent.code; + const updatedContent = `${copyrightText}${EOL}${content}`; + + // 更新bundle + if (assetContent.source === undefined) { + (fileContent as OutputChunk).code = updatedContent; + } else { + (fileContent as OutputAsset).source = updatedContent; + } + } + } + }, + order: 'post', + }, + name: 'vite:license', + }; +} + +export { viteLicensePlugin }; diff --git a/internal/vite-config/src/typing.ts b/internal/vite-config/src/typing.ts index 56c466c9..064a952e 100644 --- a/internal/vite-config/src/typing.ts +++ b/internal/vite-config/src/typing.ts @@ -67,6 +67,8 @@ interface ApplicationPluginOptions extends CommonPluginOptions { importmapOptions?: ImportmapPluginOptions; /** 是否注入app loading */ injectAppLoading?: boolean; + /** 是否注入版权信息 */ + license?: boolean; /** mock 插件配置 */ mock?: boolean; /** 是否开启pwa */ diff --git a/package.json b/package.json index 037ecef9..428ec383 100644 --- a/package.json +++ b/package.json @@ -83,8 +83,9 @@ "packageManager": "pnpm@9.4.0", "pnpm": { "overrides": { - "@ctrl/tinycolor": "4.1.0", - "clsx": "2.1.1", + "@ant-design/colors": "^7.0.2", + "@ctrl/tinycolor": "^4.1.0", + "clsx": "^2.1.1", "vue": "^3.4.30" }, "neverBuiltDependencies": [ diff --git a/packages/@core/uikit/menu-ui/src/components/normal-menu/normal-menu.vue b/packages/@core/uikit/menu-ui/src/components/normal-menu/normal-menu.vue index 7ccded81..5627b763 100644 --- a/packages/@core/uikit/menu-ui/src/components/normal-menu/normal-menu.vue +++ b/packages/@core/uikit/menu-ui/src/components/normal-menu/normal-menu.vue @@ -43,6 +43,7 @@ function handleMouseenter(menu: MenuRecordRaw) { is(theme, true), is('rounded', rounded), ]" + class="relative" >