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

tsdown.config.ts

93 lines2.2 KB

1import { transformAsync } from '@babel/core'
2// @ts-expect-error - Ships no type declarations
3import ts from '@babel/preset-typescript'
4// @ts-expect-error - Ships no type declarations
5import solidPlugin from '@solidjs/babel-plugin'
6import { defineConfig } from 'tsdown'
7import type { Rolldown, UserConfig } from 'tsdown'
8​
9// Compiles JSX with the Solid 2.0 compiler; 'unplugin-solid' ships the Solid 1.x one.
10const solid = (): Rolldown.Plugin => ({
11 name: 'solid-v2',
12 transform: {
13 filter: { id: /\.[jt]sx$/ },
14 async handler(code, id) {
15 const result = await transformAsync(code, {
16 presets: [[ts, {}]],
17 plugins: [[solidPlugin, { generate: 'dom' }]],
18 filename: id,
19 sourceMaps: true,
20 })
21 if (result?.code == null) {
22 throw new Error(`Babel returned no code for ${id}`)
23 }
24 return { code: result.code, map: result.map, moduleType: 'js' }
25 },
26 },
27})
28​
29const baseConfig: UserConfig = {
30 target: 'esnext',
31 platform: 'browser',
32 format: ['esm', 'cjs'],
33 outDir: 'build',
34 fixedExtension: false,
36​
37const environment = (development: boolean) => ({
38 NODE_ENV: development ? 'development' : 'production',
39 PROD: !development,
40 DEV: development,
41 SSR: false,
42})
43​
44const compiledConfig = (
45 entry: Record<string, string>,
46 development: boolean,
47 clean: boolean,
48): UserConfig => ({
49 ...baseConfig,
50 entry,
51 clean,
52 dts: !development,
53 env: environment(development),
54 ...(development
55 ? {}
56 : {
57 minify: {
58 compress: {
59 dropConsole: true,
60 dropDebugger: true,
61 },
62 },
63 }),
64 plugins: [solid()],
65})
66​
67const jsxConfig = (
68 entry: Record<string, string>,
69 development: boolean,
70): UserConfig => ({
71 ...baseConfig,
72 entry,
73 format: 'esm',
74 clean: false,
75 dts: false,
76 env: environment(development),
77 outExtensions: () => ({ js: '.jsx' }),
78 plugins: [],
79})
80​
81export default defineConfig([
82 compiledConfig(
83 { index: 'src/index.tsx', production: 'src/production.tsx' },
84 false,
85 true,
86 ),
87 jsxConfig(
88 { index: 'src/index.tsx', production: 'src/production.tsx' },
89 false,
90 ),
91 compiledConfig({ dev: 'src/index.tsx' }, true, false),
92 jsxConfig({ dev: 'src/index.tsx' }, true),
93])