Skip to content

Commit

Permalink
feat: allow to import TSX files with .jsx extension (#3900)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Nov 5, 2024
1 parent 00917ab commit 29d0b3e
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 0 deletions.
12 changes: 12 additions & 0 deletions e2e/cases/typescript/extension-alias-jsx/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { build } from '@e2e/helper';
import { expect, test } from '@playwright/test';

test('should allow to import TSX files with .jsx extension', async ({
page,
}) => {
await build({
cwd: __dirname,
page,
});
expect(await page.evaluate(() => window.test)).toBe(1);
});
3 changes: 3 additions & 0 deletions e2e/cases/typescript/extension-alias-jsx/src/bar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const bar = () => {
return <div>bar</div>;
};
1 change: 1 addition & 0 deletions e2e/cases/typescript/extension-alias-jsx/src/foo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 1;
12 changes: 12 additions & 0 deletions e2e/cases/typescript/extension-alias-jsx/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { bar } from './bar.jsx';
import { foo } from './foo.jsx';

declare global {
interface Window {
test: number;
}
}

window.test = foo;

console.log(bar);
12 changes: 12 additions & 0 deletions e2e/cases/typescript/extension-alias-jsx/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "@rsbuild/config/tsconfig",
"compilerOptions": {
"jsx": "react-jsx",
"baseUrl": "./",
"outDir": "./dist",
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["src", "*.test.ts"]
}
16 changes: 16 additions & 0 deletions packages/compat/webpack/tests/__snapshots__/default.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
".tsx",
".js",
],
".jsx": [
".tsx",
".jsx",
],
},
"extensions": [
".ts",
Expand Down Expand Up @@ -711,6 +715,10 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when produ
".tsx",
".js",
],
".jsx": [
".tsx",
".jsx",
],
},
"extensions": [
".ts",
Expand Down Expand Up @@ -1032,6 +1040,10 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
".tsx",
".js",
],
".jsx": [
".tsx",
".jsx",
],
},
"extensions": [
".ts",
Expand Down Expand Up @@ -1336,6 +1348,10 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
".tsx",
".js",
],
".jsx": [
".tsx",
".jsx",
],
},
"extensions": [
".ts",
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ function applyExtensions({

if (tsconfigPath) {
// TypeScript allows importing TS files with `.js` extension
// See: https://github.com/microsoft/TypeScript/blob/c09e2ab4/src/compiler/moduleNameResolver.ts#L2151-L2168
chain.resolve.extensionAlias.merge({
'.js': ['.ts', '.tsx', '.js'],
'.jsx': ['.tsx', '.jsx'],
});
}
}
Expand Down
16 changes: 16 additions & 0 deletions packages/core/tests/__snapshots__/default.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,10 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
".tsx",
".js",
],
".jsx": [
".tsx",
".jsx",
],
},
"extensions": [
".ts",
Expand Down Expand Up @@ -829,6 +833,10 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when prod
".tsx",
".js",
],
".jsx": [
".tsx",
".jsx",
],
},
"extensions": [
".ts",
Expand Down Expand Up @@ -1170,6 +1178,10 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
".tsx",
".js",
],
".jsx": [
".tsx",
".jsx",
],
},
"extensions": [
".ts",
Expand Down Expand Up @@ -1589,6 +1601,10 @@ exports[`tools.rspack > should match snapshot 1`] = `
".tsx",
".js",
],
".jsx": [
".tsx",
".jsx",
],
},
"extensions": [
".ts",
Expand Down

0 comments on commit 29d0b3e

Please sign in to comment.