library.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import type { UserConfig } from 'vite';
  2. import type { DefineLibraryOptions } from '../typing';
  3. import { readPackageJSON } from '@vben/node-utils';
  4. import { defineConfig, mergeConfig } from 'vite';
  5. import { getLibraryConditionPlugins } from '../plugins';
  6. import { getCommonConfig } from './common';
  7. function defineLibraryConfig(options: DefineLibraryOptions = {}) {
  8. return defineConfig(async ({ command, mode }) => {
  9. const root = process.cwd();
  10. const { library = {}, vite = {} } = options;
  11. const isBuild = command === 'build';
  12. const plugins = await getLibraryConditionPlugins({
  13. dts: false,
  14. injectLibCss: true,
  15. isBuild,
  16. mode,
  17. ...library,
  18. });
  19. const { dependencies = {}, peerDependencies = {} } =
  20. await readPackageJSON(root);
  21. const external = [
  22. ...Object.keys(dependencies),
  23. ...Object.keys(peerDependencies),
  24. ];
  25. const packageConfig: UserConfig = {
  26. build: {
  27. lib: {
  28. entry: 'src/index.ts',
  29. fileName: 'index.mjs',
  30. formats: ['es'],
  31. },
  32. rollupOptions: {
  33. external,
  34. },
  35. },
  36. plugins,
  37. };
  38. const commonConfig = await getCommonConfig();
  39. const mergedConfig = mergeConfig(commonConfig, packageConfig);
  40. return mergeConfig(mergedConfig, vite);
  41. });
  42. }
  43. export { defineLibraryConfig };