admin-vben5/internal/vite-config/src/config/library.ts

63 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-05-19 21:20:42 +08:00
import type { UserConfig } from 'vite';
2024-06-08 19:49:06 +08:00
import type { DefineLibraryOptions } from '../typing';
2024-05-19 21:20:42 +08:00
import { readPackageJSON } from '@vben/node-utils';
2024-06-08 19:49:06 +08:00
2024-05-19 21:20:42 +08:00
import { defineConfig, mergeConfig } from 'vite';
import { getLibraryConditionPlugins } from '../plugins';
import { getCommonConfig } from './common';
function defineLibraryConfig(options: DefineLibraryOptions = {}) {
2024-06-16 15:45:15 +08:00
return defineConfig(async (config) => {
const { command, mode } = config;
2024-05-19 21:20:42 +08:00
const root = process.cwd();
const { library = {}, vite = {} } = options;
const isBuild = command === 'build';
const plugins = await getLibraryConditionPlugins({
dts: false,
injectLibCss: true,
2024-06-23 20:05:22 +08:00
injectMetadata: true,
2024-05-19 21:20:42 +08:00
isBuild,
mode,
2024-06-16 15:45:15 +08:00
...(typeof library === 'function' ? library(config) : library),
2024-05-19 21:20:42 +08:00
});
const { dependencies = {}, peerDependencies = {} } =
await readPackageJSON(root);
2024-06-29 14:45:02 +08:00
const externalPackages = [
2024-05-19 21:20:42 +08:00
...Object.keys(dependencies),
...Object.keys(peerDependencies),
];
2024-06-29 14:45:02 +08:00
2024-05-19 21:20:42 +08:00
const packageConfig: UserConfig = {
build: {
lib: {
entry: 'src/index.ts',
fileName: () => 'index.mjs',
2024-05-19 21:20:42 +08:00
formats: ['es'],
},
rollupOptions: {
2024-06-29 14:45:02 +08:00
external: (id) => {
return externalPackages.some(
(pkg) => id === pkg || id.startsWith(`${pkg}/`),
);
},
2024-05-19 21:20:42 +08:00
},
},
plugins,
};
const commonConfig = await getCommonConfig();
const mergedConfig = mergeConfig(commonConfig, packageConfig);
2024-06-16 15:45:15 +08:00
return mergeConfig(
mergedConfig,
typeof vite === 'function' ? vite(config) : vite,
);
2024-05-19 21:20:42 +08:00
});
}
export { defineLibraryConfig };