From 10c45174503e0a98cc7c05df597828c613f3a5c5 Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Fri, 13 Mar 2026 19:57:08 +0900 Subject: [PATCH] fix(babel): ignore queries when applying ts / jsx parsers --- packages/babel/src/index.test.ts | 41 +++++++++++++++++++++++++++++++- packages/babel/src/index.ts | 6 ++--- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/packages/babel/src/index.test.ts b/packages/babel/src/index.test.ts index 257df9e..366a98d 100644 --- a/packages/babel/src/index.test.ts +++ b/packages/babel/src/index.test.ts @@ -550,6 +550,31 @@ describe('per-environment state isolation', () => { }) }) +test('jsx file with query string is parsed with jsx plugin', async () => { + const result = await build('foo.jsx?query', 'export const result =
{__FOO__}
', { + plugins: [identifierReplaceBabelPlugin('__FOO__', true)], + }) + expect(result.code).toContain('jsx("div"') +}) + +test('ts file with query string is parsed with typescript plugin', async () => { + const result = await build('foo.ts?query', 'export const result: number = __FOO__', { + plugins: [identifierReplaceBabelPlugin('__FOO__', true)], + }) + expect(result.code).toContain('const result = true') +}) + +test('tsx file with query string is parsed with typescript and jsx plugins', async () => { + const result = await build( + 'foo.tsx?query', + 'export const result: JSX.Element =
{__FOO__}
', + { + plugins: [identifierReplaceBabelPlugin('__FOO__', true)], + }, + ) + expect(result.code).toContain('jsx("div"') +}) + test('babel syntax error produces enhanced error message', async () => { const err = await build('foo.js', 'export const = ;', { plugins: [identifierReplaceBabelPlugin('foo', true)], @@ -737,7 +762,11 @@ async function build(filename: string, code: string, options: PluginOptions): Pr }, load(id) { if (id === filename) { - return code + const ext = path.extname(id.replace(/\?.*$/, '')).slice(1) + return { + code, + moduleType: extensionToModuleType(ext), + } } }, }, @@ -749,6 +778,16 @@ async function build(filename: string, code: string, options: PluginOptions): Pr return output[0] } +function extensionToModuleType(ext: string): string | undefined { + if (ext === 'tsx' || ext === 'jsx') { + return ext + } + if (ext === 'ts' || ext === 'mts' || ext === 'cts') { + return 'ts' + } + return undefined +} + function identifierReplaceBabelPlugin(name: string, value: boolean): babel.PluginItem { return ({ types: t }): babel.PluginObject => ({ visitor: { diff --git a/packages/babel/src/index.ts b/packages/babel/src/index.ts index d07d3c7..7b3e2b3 100644 --- a/packages/babel/src/index.ts +++ b/packages/babel/src/index.ts @@ -83,15 +83,15 @@ async function babelPlugin(rawOptions: PluginOptions): Promise { }, overrides: [ { - test: '**/*.jsx', + test: /\.jsx(?:$|\?)/, parserOpts: { plugins: ['jsx'] }, }, { - test: '**/*.ts', + test: /\.ts(?:$|\?)/, parserOpts: { plugins: ['typescript'] }, }, { - test: '**/*.tsx', + test: /\.tsx(?:$|\?)/, parserOpts: { plugins: ['typescript', 'jsx'] }, }, ...(babelOptions.overrides ?? []),