From aea7bc8a756c09e651b605bb6cd10019db40cd92 Mon Sep 17 00:00:00 2001 From: high1 Date: Thu, 8 Dec 2022 20:58:06 +0100 Subject: [PATCH 1/4] chore: added test for configuration overrides --- .../node/__tests__/plugins/esbuild.spec.ts | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts b/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts index 2cc8c9304450a8..9b0899cc236353 100644 --- a/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts +++ b/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts @@ -249,6 +249,37 @@ describe('transformWithEsbuild', () => { expect(result?.code).toBeTruthy() expect(result?.map).toBeTruthy() }) + + test('correctly overrides TS configuration and applies transform', async () => { + const foo = 'const foo = () => <>' + const result = await transformWithEsbuild(foo, 'baz.jsx', { + tsconfigRaw: { + compilerOptions: { + jsx: 'preserve', + jsxImportSource: 'react', + }, + }, + jsx: 'automatic', + jsxImportSource: 'bar', + }) + expect(result?.code).toContain('bar/jsx-runtime') + expect(result?.code).toContain('/* @__PURE__ */') + }) + + test('correctly overrides TS configuration and preserves code', async () => { + const foo = 'const foo = () => <>' + const result = await transformWithEsbuild(foo, 'baz.jsx', { + tsconfigRaw: { + compilerOptions: { + jsx: 'react-jsx', + jsxImportSource: 'react', + }, + }, + jsx: 'preserve', + jsxImportSource: 'bar', + }) + expect(result?.code).toContain(foo) + }) }) /** From d1500300859e2965714db3fdcad100cfed48153a Mon Sep 17 00:00:00 2001 From: high1 Date: Thu, 8 Dec 2022 21:14:35 +0100 Subject: [PATCH 2/4] chore: updated test and added one more --- .../node/__tests__/plugins/esbuild.spec.ts | 45 +++++++++++++++---- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts b/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts index 9b0899cc236353..b6044f0db1bb27 100644 --- a/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts +++ b/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts @@ -251,17 +251,20 @@ describe('transformWithEsbuild', () => { }) test('correctly overrides TS configuration and applies transform', async () => { - const foo = 'const foo = () => <>' - const result = await transformWithEsbuild(foo, 'baz.jsx', { - tsconfigRaw: { - compilerOptions: { - jsx: 'preserve', - jsxImportSource: 'react', + const result = await transformWithEsbuild( + 'const foo = () => <>', + 'baz.jsx', + { + tsconfigRaw: { + compilerOptions: { + jsx: 'preserve', + jsxImportSource: 'react', + }, }, + jsx: 'automatic', + jsxImportSource: 'bar', }, - jsx: 'automatic', - jsxImportSource: 'bar', - }) + ) expect(result?.code).toContain('bar/jsx-runtime') expect(result?.code).toContain('/* @__PURE__ */') }) @@ -280,6 +283,30 @@ describe('transformWithEsbuild', () => { }) expect(result?.code).toContain(foo) }) + + test('correctly overrides TS configuration and preserves code', async () => { + const jsxFactory = 'h', + jsxFragment = 'bar' + const result = await transformWithEsbuild( + 'const foo = () => <>', + 'baz.jsx', + { + tsconfigRaw: { + compilerOptions: { + jsxFactory: 'g', + jsxFragmentFactory: 'foo', + jsxImportSource: 'baz', + }, + }, + jsx: 'transform', + jsxFactory, + jsxFragment, + }, + ) + expect(result?.code).toContain( + `/* @__PURE__ */ ${jsxFactory}(${jsxFragment}, null)`, + ) + }) }) /** From 33dae37266e3853afd3db0476fe5561aaab725e5 Mon Sep 17 00:00:00 2001 From: high1 Date: Thu, 8 Dec 2022 21:17:01 +0100 Subject: [PATCH 3/4] chore: test names changed --- packages/vite/src/node/__tests__/plugins/esbuild.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts b/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts index b6044f0db1bb27..3c80a846dcc201 100644 --- a/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts +++ b/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts @@ -250,7 +250,7 @@ describe('transformWithEsbuild', () => { expect(result?.map).toBeTruthy() }) - test('correctly overrides TS configuration and applies transform', async () => { + test('correctly overrides TS configuration and applies automatic transform', async () => { const result = await transformWithEsbuild( 'const foo = () => <>', 'baz.jsx', @@ -284,7 +284,7 @@ describe('transformWithEsbuild', () => { expect(result?.code).toContain(foo) }) - test('correctly overrides TS configuration and preserves code', async () => { + test('correctly overrides TS configuration and transforms code', async () => { const jsxFactory = 'h', jsxFragment = 'bar' const result = await transformWithEsbuild( From 31828a2a572644505fa16d66f3b9471268034e57 Mon Sep 17 00:00:00 2001 From: high1 Date: Thu, 8 Dec 2022 21:24:21 +0100 Subject: [PATCH 4/4] fix: code cleanup --- packages/vite/src/node/__tests__/plugins/esbuild.spec.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts b/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts index 3c80a846dcc201..22884162849a69 100644 --- a/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts +++ b/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts @@ -251,6 +251,7 @@ describe('transformWithEsbuild', () => { }) test('correctly overrides TS configuration and applies automatic transform', async () => { + const jsxImportSource = 'bar' const result = await transformWithEsbuild( 'const foo = () => <>', 'baz.jsx', @@ -258,14 +259,13 @@ describe('transformWithEsbuild', () => { tsconfigRaw: { compilerOptions: { jsx: 'preserve', - jsxImportSource: 'react', }, }, jsx: 'automatic', - jsxImportSource: 'bar', + jsxImportSource, }, ) - expect(result?.code).toContain('bar/jsx-runtime') + expect(result?.code).toContain(`${jsxImportSource}/jsx-runtime`) expect(result?.code).toContain('/* @__PURE__ */') }) @@ -275,11 +275,9 @@ describe('transformWithEsbuild', () => { tsconfigRaw: { compilerOptions: { jsx: 'react-jsx', - jsxImportSource: 'react', }, }, jsx: 'preserve', - jsxImportSource: 'bar', }) expect(result?.code).toContain(foo) })