Skip to content

Commit 16ed5a0

Browse files
committed
feat: add license plugin
1 parent 24aab5b commit 16ed5a0

File tree

16 files changed

+156
-74
lines changed

16 files changed

+156
-74
lines changed

.github/semantic.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
titleAndCommits: true
2+
types:
3+
- feat
4+
- fix
5+
- docs
6+
- chore
7+
- style
8+
- refactor
9+
- perf
10+
- test
11+
- build
12+
- ci
13+
- revert

internal/node-utils/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"@changesets/git": "^3.0.0",
3232
"@manypkg/get-packages": "^2.2.1",
3333
"consola": "^3.2.3",
34+
"dayjs": "^1.11.11",
3435
"find-up": "^7.0.0",
3536
"nanoid": "^5.0.7",
3637
"pkg-types": "^1.1.1",

internal/node-utils/src/date.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import dayjs from 'dayjs';
2+
import timezone from 'dayjs/plugin/timezone';
3+
import utc from 'dayjs/plugin/utc';
4+
5+
dayjs.extend(utc);
6+
dayjs.extend(timezone);
7+
8+
const dateUtil = dayjs().tz('Asia/Shanghai');
9+
10+
export { dateUtil };

internal/node-utils/src/index.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
export { UNICODE } from './constants';
1+
export * from './constants';
2+
export * from './date';
23
export * from './git';
34
export { add as gitAdd, getStagedFiles } from './git';
45
export { generatorContentHash } from './hash';
5-
export {
6-
findMonorepoRoot,
7-
getPackage,
8-
getPackages,
9-
getPackagesSync,
10-
} from './monorepo';
6+
export * from './monorepo';
117
export { toPosixPath } from './path';
128
export { prettierFormat } from './prettier';
139
export type { Package } from '@manypkg/get-packages';

internal/vite-config/src/config/application.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function defineApplicationConfig(options: DefineApplicationOptions = {}) {
2525
i18n: true,
2626
injectAppLoading: true,
2727
isBuild,
28+
license: true,
2829
mock: true,
2930
mode,
3031
pwa: true,

internal/vite-config/src/plugins/extra-app-config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import type { PluginOption } from 'vite';
2+
13
import {
24
colors,
35
generatorContentHash,
46
readPackageJSON,
57
} from '@vben/node-utils';
68

7-
import { type PluginOption } from 'vite';
8-
99
import { getEnvConfig } from '../utils/env';
1010

1111
interface PluginOptions {

internal/vite-config/src/plugins/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import viteVueDevTools from 'vite-plugin-vue-devtools';
2727
import { viteExtraAppConfigPlugin } from './extra-app-config';
2828
import { viteImportMapPlugin } from './importmap';
2929
import { viteInjectAppLoadingPlugin } from './inject-app-loading';
30+
import { viteLicensePlugin } from './license';
3031

3132
/**
3233
* 获取条件成立的 vite 插件
@@ -94,12 +95,12 @@ async function getApplicationConditionPlugins(
9495
compress,
9596
compressTypes,
9697
extraAppConfig,
97-
9898
html,
9999
i18n,
100100
importmap,
101101
importmapOptions,
102102
injectAppLoading,
103+
license,
103104
mock,
104105
pwa,
105106
pwaOptions,
@@ -130,6 +131,10 @@ async function getApplicationConditionPlugins(
130131
condition: injectAppLoading,
131132
plugins: async () => [await viteInjectAppLoadingPlugin(!!isBuild, env)],
132133
},
134+
{
135+
condition: license,
136+
plugins: async () => [await viteLicensePlugin()],
137+
},
133138
{
134139
condition: pwa,
135140
plugins: () =>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import type {
2+
NormalizedOutputOptions,
3+
OutputAsset,
4+
OutputBundle,
5+
OutputChunk,
6+
} from 'rollup';
7+
import type { PluginOption } from 'vite';
8+
9+
import { EOL } from 'node:os';
10+
11+
import { dateUtil, readPackageJSON } from '@vben/node-utils';
12+
13+
/**
14+
* 用于将配置文件抽离出来并注入到项目中
15+
* @returns
16+
*/
17+
18+
async function viteLicensePlugin(
19+
root = process.cwd(),
20+
): Promise<PluginOption | undefined> {
21+
const {
22+
description = '',
23+
homepage = '',
24+
version = '',
25+
} = await readPackageJSON(root);
26+
27+
return {
28+
apply: 'build',
29+
enforce: 'post',
30+
generateBundle: {
31+
handler: (_options: NormalizedOutputOptions, bundle: OutputBundle) => {
32+
const date = dateUtil.format('YYYY-MM-DD ');
33+
34+
const copyrightText = `/*!
35+
* Vben Admin Pro
36+
* Version: ${version}
37+
* Author: vben
38+
* Copyright (C) 2024 Vben
39+
* License: MIT License
40+
* Description: ${description}
41+
* Date Created: ${date}
42+
* Homepage: ${homepage}
43+
* Contact: ann.vben@gmail.com
44+
*/
45+
`.trim();
46+
47+
for (const [, fileContent] of Object.entries(bundle)) {
48+
if (
49+
fileContent.type === 'asset' ||
50+
(fileContent.type === 'chunk' && fileContent.isEntry)
51+
) {
52+
const chunkContent = fileContent as OutputChunk;
53+
const assetContent = fileContent as OutputAsset;
54+
// 插入版权信息
55+
const content =
56+
typeof assetContent.source === 'string'
57+
? assetContent.source
58+
: chunkContent.code;
59+
const updatedContent = `${copyrightText}${EOL}${content}`;
60+
61+
// 更新bundle
62+
if (assetContent.source === undefined) {
63+
(fileContent as OutputChunk).code = updatedContent;
64+
} else {
65+
(fileContent as OutputAsset).source = updatedContent;
66+
}
67+
}
68+
}
69+
},
70+
order: 'post',
71+
},
72+
name: 'vite:license',
73+
};
74+
}
75+
76+
export { viteLicensePlugin };

internal/vite-config/src/typing.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ interface ApplicationPluginOptions extends CommonPluginOptions {
6767
importmapOptions?: ImportmapPluginOptions;
6868
/** 是否注入app loading */
6969
injectAppLoading?: boolean;
70+
/** 是否注入版权信息 */
71+
license?: boolean;
7072
/** mock 插件配置 */
7173
mock?: boolean;
7274
/** 是否开启pwa */

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@
8383
"packageManager": "pnpm@9.4.0",
8484
"pnpm": {
8585
"overrides": {
86-
"@ctrl/tinycolor": "4.1.0",
87-
"clsx": "2.1.1",
86+
"@ant-design/colors": "^7.0.2",
87+
"@ctrl/tinycolor": "^4.1.0",
88+
"clsx": "^2.1.1",
8889
"vue": "^3.4.30"
8990
},
9091
"neverBuiltDependencies": [

0 commit comments

Comments
 (0)