Open History to see the last commit
1// @ts-nocheck - Config file uses untyped babel/esbuild imports for the custom Solid v2 build plugin
2import { parse } from 'path'
3import { readFile } from 'fs/promises'
4import { transformAsync } from '@babel/core'
5import solid from '@solidjs/babel-plugin'
6import ts from '@babel/preset-typescript'
7import { defineConfig } from 'tsup'
8import { generateTsupOptions, parsePresetOptions } from 'tsup-preset-solid'
9β
10import type { Plugin } from 'esbuild'
11β
12// Custom esbuild plugin that compiles JSX with @solidjs/babel-plugin β the
13// Solid 2.0 compiler, whose output targets the same @solidjs/web runtime the
14// package declares as a peer (tsup-preset-solid's own plugin is Solid 1.x).
15function solidV2Plugin(options: { generate: 'dom' | 'ssr' }): Plugin {
16 return {
17 name: 'esbuild:solid-v2',
18 setup(build) {
19 build.onLoad({ filter: /\.(t|j)sx$/ }, async (args) => {
20 const source = await readFile(args.path, { encoding: 'utf-8' })
21 const { name, ext } = parse(args.path)
22 const filename = name + ext
23 const result = await transformAsync(source, {
24 presets: [[ts, {}]],
25 plugins: [[solid, { generate: options.generate }]],
26 filename,
27 sourceMaps: 'inline',
28 })
29 if (result?.code === undefined || result.code === null) {
30 throw new Error('No result was provided from Babel')
31 }
32 return { contents: result.code, loader: 'js' }
33 })
34 },
35 }
36}
37β
38const preset_options = {
39 entries: {
40 entry: 'src/index.ts',
41 dev_entry: true,
42 },
43 cjs: true,
44 drop_console: true,
45}
46β
47export default defineConfig(() => {
48 const parsed_data = parsePresetOptions(preset_options)
49 const tsup_options = generateTsupOptions(parsed_data)
50β
51 tsup_options.forEach((tsup_option) => {
52 tsup_option.outDir = 'build'
53 tsup_option.experimentalDts = true
54 delete tsup_option.dts
55β
56 // Replace the default solid esbuild plugin (Solid 1.x's babel-preset-solid)
57 // with the Solid 2.0 compiler.
58 if (tsup_option.esbuildPlugins) {
59 const nonSolidPlugins = tsup_option.esbuildPlugins.filter(
60 (p) => !p.name.includes('solid'),
61 )
62 const hasSolidPlugin =
63 nonSolidPlugins.length < tsup_option.esbuildPlugins.length
64 if (hasSolidPlugin) {
65 tsup_option.esbuildPlugins = [
66 solidV2Plugin({ generate: 'dom' }),
67 ...nonSolidPlugins,
68 ]
69 }
70 }
71 })
72β
73 return tsup_options
74})