|
| 1 | +import path from 'node:path'; |
| 2 | +import { fileURLToPath } from 'node:url'; |
| 3 | +import { getJsTransformRules } from '@callstack/repack'; |
| 4 | +import { describe, expect, inject, it } from 'vitest'; |
| 5 | +import { |
| 6 | + compile, |
| 7 | + createCompiler, |
| 8 | + createVirtualModulePlugin, |
| 9 | +} from '../helpers.js'; |
| 10 | + |
| 11 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 12 | + |
| 13 | +// JSX source info that development transforms add for the fixture |
| 14 | +// (`jsxDEV` source argument for automatic runtime, `__source` prop for classic) |
| 15 | +const SOURCE_INFO = /fileName: ".*App\.jsx"/; |
| 16 | + |
| 17 | +async function compileJsx( |
| 18 | + mode: 'development' | 'production', |
| 19 | + swc: NonNullable<Parameters<typeof getJsTransformRules>[0]>['swc'] |
| 20 | +) { |
| 21 | + const virtualPlugin = await createVirtualModulePlugin({ |
| 22 | + 'App.jsx': 'export const App = () => <view title="hello" />;', |
| 23 | + }); |
| 24 | + |
| 25 | + const compiler = await createCompiler({ |
| 26 | + context: __dirname, |
| 27 | + mode, |
| 28 | + devtool: false, |
| 29 | + entry: './App.jsx', |
| 30 | + output: { path: '/out' }, |
| 31 | + optimization: { minimize: false }, |
| 32 | + externals: { |
| 33 | + react: 'commonjs react', |
| 34 | + 'react/jsx-runtime': 'commonjs react/jsx-runtime', |
| 35 | + 'react/jsx-dev-runtime': 'commonjs react/jsx-dev-runtime', |
| 36 | + }, |
| 37 | + module: { rules: getJsTransformRules({ swc }) }, |
| 38 | + plugins: [virtualPlugin], |
| 39 | + }); |
| 40 | + |
| 41 | + const { code } = await compile(compiler); |
| 42 | + return code; |
| 43 | +} |
| 44 | + |
| 45 | +// builtin:swc-loader only exists in Rspack |
| 46 | +describe.runIf(inject('bundlerType') === 'rspack')( |
| 47 | + 'SWC JSX development transforms', |
| 48 | + () => { |
| 49 | + it('emits jsxDEV with source info for automatic runtime in development', async () => { |
| 50 | + const code = await compileJsx('development', { jsxRuntime: 'automatic' }); |
| 51 | + |
| 52 | + expect(code).toContain('react/jsx-dev-runtime'); |
| 53 | + expect(code).toContain('jsxDEV'); |
| 54 | + expect(code).toMatch(SOURCE_INFO); |
| 55 | + }); |
| 56 | + |
| 57 | + it('emits jsx without source info for automatic runtime in production', async () => { |
| 58 | + const code = await compileJsx('production', { jsxRuntime: 'automatic' }); |
| 59 | + |
| 60 | + expect(code).toContain('react/jsx-runtime'); |
| 61 | + expect(code).not.toContain('jsx-dev-runtime'); |
| 62 | + expect(code).not.toContain('jsxDEV'); |
| 63 | + expect(code).not.toMatch(SOURCE_INFO); |
| 64 | + }); |
| 65 | + |
| 66 | + it('adds __source for classic runtime in development', async () => { |
| 67 | + const code = await compileJsx('development', { jsxRuntime: 'classic' }); |
| 68 | + |
| 69 | + expect(code).toContain('createElement'); |
| 70 | + expect(code).toContain('__source'); |
| 71 | + expect(code).toMatch(SOURCE_INFO); |
| 72 | + }); |
| 73 | + |
| 74 | + it('omits __source for classic runtime in production', async () => { |
| 75 | + const code = await compileJsx('production', { jsxRuntime: 'classic' }); |
| 76 | + |
| 77 | + expect(code).toContain('createElement'); |
| 78 | + expect(code).not.toContain('__source'); |
| 79 | + expect(code).not.toMatch(SOURCE_INFO); |
| 80 | + }); |
| 81 | + |
| 82 | + it('respects an explicit development: false in development', async () => { |
| 83 | + const code = await compileJsx('development', { development: false }); |
| 84 | + |
| 85 | + expect(code).toContain('react/jsx-runtime'); |
| 86 | + expect(code).not.toContain('jsxDEV'); |
| 87 | + expect(code).not.toMatch(SOURCE_INFO); |
| 88 | + }); |
| 89 | + |
| 90 | + it('respects an explicit development: true in production', async () => { |
| 91 | + const code = await compileJsx('production', { development: true }); |
| 92 | + |
| 93 | + expect(code).toContain('react/jsx-dev-runtime'); |
| 94 | + expect(code).toContain('jsxDEV'); |
| 95 | + expect(code).toMatch(SOURCE_INFO); |
| 96 | + }); |
| 97 | + } |
| 98 | +); |
0 commit comments