|
| 1 | +import { |
| 2 | + defineConfig, |
| 3 | + LibConfig, |
| 4 | + RsbuildConfigOutputTarget, |
| 5 | +} from '@rslib/core'; |
| 6 | + |
| 7 | +type LibFormat = LibConfig['format']; |
| 8 | +export type BundleType = boolean | 'excludeExternal'; |
| 9 | + |
| 10 | +interface Options { |
| 11 | + format?: LibFormat[]; |
| 12 | + bundle?: BundleType; |
| 13 | + tsconfigPath?: string; |
| 14 | + umdName?: string; |
| 15 | + target?: RsbuildConfigOutputTarget; |
| 16 | +} |
| 17 | +const defaultOptions = { |
| 18 | + format: ['esm', 'cjs'] as LibFormat[], |
| 19 | + bundle: true, |
| 20 | + target: 'web' as RsbuildConfigOutputTarget, |
| 21 | + tsconfigPath: './tsconfig.build.json', |
| 22 | +}; |
| 23 | + |
| 24 | +function getRslibConfig(options: Options) { |
| 25 | + const { format, bundle, umdName, tsconfigPath, target } = { |
| 26 | + ...defaultOptions, |
| 27 | + ...options, |
| 28 | + }; |
| 29 | + |
| 30 | + const libs = format.map(libFormat => { |
| 31 | + const lib = getLibShared(libFormat, bundle); |
| 32 | + if (libFormat === 'umd') { |
| 33 | + if (!umdName) { |
| 34 | + throw new Error( |
| 35 | + 'getRslibConfig: umdName is required when using UMD format', |
| 36 | + ); |
| 37 | + } |
| 38 | + lib.umdName = umdName; |
| 39 | + lib.bundle = true; |
| 40 | + } |
| 41 | + return lib; |
| 42 | + }); |
| 43 | + |
| 44 | + libs[0].dts = { |
| 45 | + distPath: './dist/types', |
| 46 | + }; |
| 47 | + |
| 48 | + return defineConfig({ |
| 49 | + source: { |
| 50 | + tsconfigPath, |
| 51 | + }, |
| 52 | + output: { |
| 53 | + target, |
| 54 | + }, |
| 55 | + lib: libs, |
| 56 | + }); |
| 57 | +} |
| 58 | + |
| 59 | +function getLibShared(format: LibFormat, bundleType: BundleType) { |
| 60 | + const shared: LibConfig = { |
| 61 | + output: { |
| 62 | + distPath: { |
| 63 | + root: `./dist/${format}`, |
| 64 | + }, |
| 65 | + }, |
| 66 | + format, |
| 67 | + syntax: 'es6', |
| 68 | + bundle: !!bundleType, |
| 69 | + autoExternal: bundleType === 'excludeExternal', |
| 70 | + }; |
| 71 | + return shared; |
| 72 | +} |
| 73 | + |
| 74 | +export default getRslibConfig; |
0 commit comments