Open History to see the last commit
1// @vitest-environment node
2import { createRsbuild } from '@rsbuild/core'
3import { describe, expect, test, vi } from 'vitest'
4import { tanstackStart } from '../src/plugin/rsbuild'
5β
6// Exercise the Solid-specific hook with Rsbuild's real chain, independently of
7// route generation and the core Start plugin's filesystem setup.
8vi.mock('@tanstack/start-plugin-core/rsbuild', () => ({
9 RSBUILD_ENVIRONMENT_NAMES: { server: 'server' },
10 tanStackStartRsbuild: () => ({ setup() {} }),
11}))
12β
13async function configureBabel(
14 target: 'web' | 'node',
15 options: string | { presets: Array<unknown> },
16) {
17 const results: Array<unknown> = []
18 const rsbuild = await createRsbuild({
19 rsbuildConfig: {
20 source: { entry: { index: './src/index.ts' } },
21 output: { target },
22 plugins: [
23 {
24 name: 'test-babel-options',
25 setup(api) {
26 api.modifyBundlerChain((chain, { CHAIN_ID }) => {
27 for (const id of [
28 CHAIN_ID.RULE.JS,
29 CHAIN_ID.RULE.JS_DATA_URI,
30 'babel-js',
31 ]) {
32 chain.module
33 .rule(id)
34 .use(CHAIN_ID.USE.BABEL)
35 .loader('babel-loader')
36 .options(structuredClone(options))
37 }
38 })
39 },
40 },
41 tanstackStart(),
42 {
43 name: 'test-read-babel-options',
44 setup(api) {
45 api.modifyBundlerChain((chain, { CHAIN_ID }) => {
46 for (const id of [
47 CHAIN_ID.RULE.JS,
48 CHAIN_ID.RULE.JS_DATA_URI,
49 'babel-js',
50 ]) {
51 results.push(
52 chain.module.rule(id).use(CHAIN_ID.USE.BABEL).get('options'),
53 )
54 }
55 })
56 },
57 },
58 ],
59 },
60 })
61 await rsbuild.initConfigs()
62 return results
63}
64β
65describe('Solid Start Rsbuild Babel options', () => {
66 test.each(['web', 'node'] as const)(
67 'preserves string loader options for %s',
68 async (target) => {
69 const options = 'babelrc=true&configFile=./babel.config.json'
70 expect(await configureBabel(target, options)).toEqual([
71 options,
72 options,
73 options,
74 ])
75 },
76 )
77β
78 test.each(['web', 'node'] as const)(
79 'retains Solid hydration and target selection for %s',
80 async (target) => {
81 const options = {
82 presets: [
83 ['babel-preset-solid', { custom: true, hydratable: false }],
84 ['another-preset', { untouched: true }],
85 ],
86 }
87 const expected = {
88 presets: [
89 [
90 'babel-preset-solid',
91 {
92 custom: true,
93 hydratable: true,
94 generate: target === 'node' ? 'ssr' : 'dom',
95 },
96 ],
97 ['another-preset', { untouched: true }],
98 ],
99 }
100 expect(await configureBabel(target, options)).toEqual([
101 expected,
102 expected,
103 expected,
104 ])
105 },
106 )
107})