70 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-07-25 11:55:18 +08:00
/*
* @Description:
* @Author:
* @Date: 2025-06-19 15:33:55
* @LastEditors:
*/
2025-05-27 19:31:37 +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';
/**
*
* @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) => {
const date = dateUtil().format('YYYY-MM-DD ');
const copyrightText = `/*!
2025-07-25 11:55:18 +08:00
*
2025-05-27 19:31:37 +08:00
* Version: ${version}
2025-07-25 11:55:18 +08:00
*
* Copyright (C) 2025
2025-05-27 19:31:37 +08:00
* License: MIT License
* Description: ${description}
* Date Created: ${date}
* Homepage: ${homepage}
2025-07-25 11:55:18 +08:00
* Contact: 集社云信息科技有限公司
2025-05-27 19:31:37 +08:00
*/
`.trim();
for (const [, fileContent] of Object.entries(bundle)) {
if (fileContent.type === 'chunk' && fileContent.isEntry) {
const chunkContent = fileContent as OutputChunk;
// 插入版权信息
const content = chunkContent.code;
const updatedContent = `${copyrightText}${EOL}${content}`;
// 更新bundle
(fileContent as OutputChunk).code = updatedContent;
}
}
},
order: 'post',
},
name: 'vite:license',
};
}
export { viteLicensePlugin };