Blame view

Yi.Vben5.Vue3/internal/vite-config/src/plugins/license.ts 1.5 KB
515fceeb   “wangming”   框架初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  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 = `/*!
    * Vben Admin
    * 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) {
              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 };