Blame view

Yi.Vben5.Vue3/internal/vite-config/src/config/library.ts 1.59 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
  import type { ConfigEnv, UserConfig } from 'vite';
  
  import type { DefineLibraryOptions } from '../typing';
  
  import { readPackageJSON } from '@vben/node-utils';
  
  import { defineConfig, mergeConfig } from 'vite';
  
  import { loadLibraryPlugins } from '../plugins';
  import { getCommonConfig } from './common';
  
  function defineLibraryConfig(userConfigPromise?: DefineLibraryOptions) {
    return defineConfig(async (config: ConfigEnv) => {
      const options = await userConfigPromise?.(config);
      const { command, mode } = config;
      const { library = {}, vite = {} } = options || {};
      const root = process.cwd();
      const isBuild = command === 'build';
  
      const plugins = await loadLibraryPlugins({
        dts: false,
        injectMetadata: true,
        isBuild,
        mode,
        ...library,
      });
  
      const { dependencies = {}, peerDependencies = {} } =
        await readPackageJSON(root);
  
      const externalPackages = [
        ...Object.keys(dependencies),
        ...Object.keys(peerDependencies),
      ];
  
      const packageConfig: UserConfig = {
        build: {
          lib: {
            entry: 'src/index.ts',
            fileName: () => 'index.mjs',
            formats: ['es'],
          },
          rollupOptions: {
            external: (id) => {
              return externalPackages.some(
                (pkg) => id === pkg || id.startsWith(`${pkg}/`),
              );
            },
          },
        },
        plugins,
      };
      const commonConfig = await getCommonConfig();
      const mergedConmonConfig = mergeConfig(commonConfig, packageConfig);
      return mergeConfig(mergedConmonConfig, vite);
    });
  }
  
  export { defineLibraryConfig };