Blame view

Yi.Vben5.Vue3/internal/vite-config/src/plugins/inject-metadata.ts 2.75 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  import type { PluginOption } from 'vite';
  
  import {
    dateUtil,
    findMonorepoRoot,
    getPackages,
    readPackageJSON,
  } from '@vben/node-utils';
  
  import { readWorkspaceManifest } from '@pnpm/workspace.read-manifest';
  
  function resolvePackageVersion(
    pkgsMeta: Record<string, string>,
    name: string,
    value: string,
    catalog: Record<string, string>,
  ) {
    if (value.includes('catalog:')) {
      return catalog[name];
    }
  
    if (value.includes('workspace')) {
      return pkgsMeta[name];
    }
  
    return value;
  }
  
  async function resolveMonorepoDependencies() {
    const { packages } = await getPackages();
    const manifest = await readWorkspaceManifest(findMonorepoRoot());
    const catalog = manifest?.catalog || {};
  
    const resultDevDependencies: Record<string, string | undefined> = {};
    const resultDependencies: Record<string, string | undefined> = {};
    const pkgsMeta: Record<string, string> = {};
  
    for (const { packageJson } of packages) {
      pkgsMeta[packageJson.name] = packageJson.version;
    }
  
    for (const { packageJson } of packages) {
      const { dependencies = {}, devDependencies = {} } = packageJson;
      for (const [key, value] of Object.entries(dependencies)) {
        resultDependencies[key] = resolvePackageVersion(
          pkgsMeta,
          key,
          value,
          catalog,
        );
      }
      for (const [key, value] of Object.entries(devDependencies)) {
        resultDevDependencies[key] = resolvePackageVersion(
          pkgsMeta,
          key,
          value,
          catalog,
        );
      }
    }
    return {
      dependencies: resultDependencies,
      devDependencies: resultDevDependencies,
    };
  }
  
  /**
   * 用于注入项目信息
   */
  async function viteMetadataPlugin(
    root = process.cwd(),
  ): Promise<PluginOption | undefined> {
    const { author, description, homepage, license, version } =
      await readPackageJSON(root);
  
    const buildTime = dateUtil().format('YYYY-MM-DD HH:mm:ss');
  
    return {
      async config() {
        const { dependencies, devDependencies } =
          await resolveMonorepoDependencies();
  
        const isAuthorObject = typeof author === 'object';
        const authorName = isAuthorObject ? author.name : author;
        const authorEmail = isAuthorObject ? author.email : null;
        const authorUrl = isAuthorObject ? author.url : null;
  
        return {
          define: {
            __VBEN_ADMIN_METADATA__: JSON.stringify({
              authorEmail,
              authorName,
              authorUrl,
              buildTime,
              dependencies,
              description,
              devDependencies,
              homepage,
              license,
              version,
            }),
            'import.meta.env.VITE_APP_VERSION': JSON.stringify(version),
          },
        };
      },
      enforce: 'post',
      name: 'vite:inject-metadata',
    };
  }
  
  export { viteMetadataPlugin };