Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to import TSX files with .jsx extension #3900

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading