Open History to see the last commit
1import { readFileSync } from 'node:fs'
2import { createRequire } from 'node:module'
3import { dirname, resolve } from 'node:path'
4import { defineConfig } from 'tsdown'
5import solid from 'unplugin-solid/rolldown'
6import type { UserConfig } from 'tsdown'
7β
8const require = createRequire(import.meta.url)
9β
10const solidBrowserIds: Record<string, string> = {
11 'solid-js/web': require.resolve('solid-js/web/dist/web.js'),
12 'solid-js/store': require.resolve('solid-js/store/dist/store.js'),
13 'solid-js/html': require.resolve('solid-js/html/dist/html.js'),
14 'solid-js/h': require.resolve('solid-js/h/dist/h.js'),
15 'solid-js': require.resolve('solid-js/dist/solid.js'),
16}
17β
18const solidBrowserImports = () => ({
19 name: 'solid-browser-imports',
20 resolveId(id: string, importer?: string) {
21 // Resolve the browser runtime explicitly. This keeps Solid bundled in
22 // query-devtools, as it was with tsup-preset-solid, while keeping d.ts
23 // imports unchanged.
24 if (importer?.includes('.d.')) return undefined
25 const browserId = solidBrowserIds[id]
26 if (browserId) {
27 return { id: browserId, external: false }
28 }
29 return undefined
30 },
31})
32β
33// Loads `./styles.css?inline` as a string, as Vite does in the tests. The CSS
34// is not minified by the JS minifier, so remove comments and extra whitespace.
35const inlineCss = () => ({
36 name: 'inline-css',
37 resolveId(id: string, importer?: string) {
38 if (id.endsWith('.css?inline') && importer) {
39 return resolve(dirname(importer), id)
40 }
41 return undefined
42 },
43 load(id: string) {
44 if (!id.endsWith('.css?inline')) return undefined
45 const css = readFileSync(id.slice(0, -'?inline'.length), 'utf8')
46 .replace(/\/\*[\s\S]*?\*\//g, '')
47 .replace(/\s+/g, ' ')
48 .replace(/ ?([{};]) ?/g, '$1')
49 .trim()
50 return { code: `export default ${JSON.stringify(css)}`, moduleType: 'js' }
51 },
52})
53β
54const baseConfig: UserConfig = {
55 target: 'esnext',
56 platform: 'neutral',
57 alias: {
58 'solid-transition-group': 'solid-transition-group/dist/index.js',
59 },
60 format: ['esm', 'cjs'],
61 outDir: 'build',
62 fixedExtension: false,
63 // Published source needs these dependencies declared, but the compiled
64 // devtools must still bundle them with the Solid browser runtime.
65 deps: {
66 alwaysBundle: [/./],
67 dts: { alwaysBundle: [] },
68 },
69 outputOptions: {
70 postBanner: '/* @refresh skip */',
71 },
72 inputOptions: {
73 // Keep package imports in declaration output. Runtime Solid imports are
74 // bundled from the browser files by solidBrowserImports above.
75 external: (id: string, importer?: string) =>
76 (id.startsWith('solid-js') && importer?.includes('.d.')) ||
77 (id === '@tanstack/query-core' && importer?.includes('.d.')),
78 },
79}
80β
81const environment = (development: boolean) => ({
82 NODE_ENV: development ? 'development' : 'production',
83 PROD: !development,
84 DEV: development,
85 SSR: false,
86})
87β
88export default defineConfig([
89 {
90 ...baseConfig,
91 entry: { index: 'src/index.ts' },
92 clean: true,
93 dts: true,
94 env: environment(false),
95 minify: {
96 compress: {
97 dropConsole: true,
98 dropDebugger: true,
99 },
100 },
101 plugins: [solid(), solidBrowserImports(), inlineCss()],
102 },
103 {
104 ...baseConfig,
105 entry: { dev: 'src/index.ts' },
106 clean: false,
107 dts: false,
108 env: environment(true),
109 plugins: [solid(), solidBrowserImports(), inlineCss()],
110 },
111])