admin-vben5/internal/vite-config/src/plugins/license.ts

64 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-06-23 19:45:40 +08:00
import type {
NormalizedOutputOptions,
OutputBundle,
OutputChunk,
} from 'rollup';
import type { PluginOption } from 'vite';
import { EOL } from 'node:os';
import { dateUtil, readPackageJSON } from '@vben/node-utils';
/**
2024-06-23 20:05:22 +08:00
*
2024-06-23 19:45:40 +08:00
* @returns
*/
async function viteLicensePlugin(
root = process.cwd(),
): Promise<PluginOption | undefined> {
const {
description = '',
homepage = '',
version = '',
} = await readPackageJSON(root);
return {
apply: 'build',
enforce: 'post',
generateBundle: {
handler: (_options: NormalizedOutputOptions, bundle: OutputBundle) => {
2024-06-23 20:05:22 +08:00
const date = dateUtil().format('YYYY-MM-DD ');
2024-06-23 19:45:40 +08:00
const copyrightText = `/*!
2024-07-14 23:10:48 +08:00
* Vue Vben Admin
2024-06-23 19:45:40 +08:00
* 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 === 'chunk' && fileContent.isEntry) {
2024-06-23 19:45:40 +08:00
const chunkContent = fileContent as OutputChunk;
// 插入版权信息
const content = chunkContent.code;
2024-06-23 19:45:40 +08:00
const updatedContent = `${copyrightText}${EOL}${content}`;
// 更新bundle
(fileContent as OutputChunk).code = updatedContent;
2024-06-23 19:45:40 +08:00
}
}
},
order: 'post',
},
name: 'vite:license',
};
}
export { viteLicensePlugin };