feat: add license plugin

This commit is contained in:
vben
2024-06-23 19:45:40 +08:00
parent 24aab5b4bb
commit 16ed5a05ba
16 changed files with 156 additions and 74 deletions

View File

@@ -25,6 +25,7 @@ function defineApplicationConfig(options: DefineApplicationOptions = {}) {
i18n: true,
injectAppLoading: true,
isBuild,
license: true,
mock: true,
mode,
pwa: true,

View File

@@ -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 {

View File

@@ -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: () =>

View File

@@ -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<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 = `/*!
* 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 };

View File

@@ -67,6 +67,8 @@ interface ApplicationPluginOptions extends CommonPluginOptions {
importmapOptions?: ImportmapPluginOptions;
/** 是否注入app loading */
injectAppLoading?: boolean;
/** 是否注入版权信息 */
license?: boolean;
/** mock 插件配置 */
mock?: boolean;
/** 是否开启pwa */