Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

build-tools.ts

74 lines2.1 KB

1import type { AddOn, BundlerDefinition, Framework, Options } from './types.js'
2​
3export const VITE_BUNDLER: BundlerDefinition = {
4 id: 'vite',
5 name: 'Vite',
6 description: 'Build with Vite',
7}
8​
9export function getBundlers(framework: Framework): Array<BundlerDefinition> {
10 return framework.bundlers?.length ? framework.bundlers : [VITE_BUNDLER]
12​
13export function getDefaultBundler(framework: Framework): BundlerDefinition {
14 const bundlers = getBundlers(framework)
15 return (
16 bundlers.find((bundler) => bundler.id === framework.defaultBundler) ??
17 bundlers.find((bundler) => bundler.id === VITE_BUNDLER.id) ??
18 bundlers[0]
19 )
21​
22export function resolveBundler(
23 framework: Framework,
24 bundlerId?: string,
25): BundlerDefinition {
26 const bundlers = getBundlers(framework)
27 const requested = bundlerId ?? getDefaultBundler(framework).id
28 const bundler =
29 bundlers.find((item) => item.id === requested) ??
30 bundlers.find((item) => item.id.toLowerCase() === requested.toLowerCase())
31​
32 if (!bundler) {
33 throw new Error(
34 `Build tool "${requested}" is not supported by ${framework.name}. Supported build tools: ${bundlers.map((item) => item.id).join(', ')}`,
35 )
36 }
37​
38 return bundler
40​
41export function isAddOnSupportedByBundler(
42 addOn: Pick<AddOn, 'type'>,
43 bundlerId: string,
44) {
45 return bundlerId !== 'rsbuild' || addOn.type === 'toolchain'
47​
48export function assertAddOnSupportedByBundler(
49 addOn: Pick<AddOn, 'id' | 'name' | 'type'>,
50 bundlerId: string,
51) {
52 if (!isAddOnSupportedByBundler(addOn, bundlerId)) {
53 throw new Error(
54 `Rsbuild currently supports toolchain add-ons only. "${addOn.id}" (${addOn.name}) is a ${addOn.type} add-on.`,
55 )
56 }
58​
59export function normalizeAndValidateBundlerOptions(options: Options): Options {
60 const bundler = resolveBundler(options.framework, options.bundler).id
61​
62 if (bundler === 'rsbuild' && options.starter) {
63 throw new Error('Rsbuild does not currently support templates.')
64 }
65​
66 for (const addOn of options.chosenAddOns) {
67 assertAddOnSupportedByBundler(addOn, bundler)
68 }
69​
70 return {
71 ...options,
72 bundler,
73 }