Skip to content

Commit

Permalink
feat: tsx file support (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
altano authored Sep 18, 2024
1 parent 8e5f1b1 commit bd1d642
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export type OptionsResolved = Overwrite<

export function resolveOptions(options: Options): OptionsResolved {
return {
include: options.include || [/\.[cm]?ts$/],
include: options.include || [/\.[cm]?tsx?$/],
exclude: options.exclude || [/node_modules/],
enforce: 'enforce' in options ? options.enforce : 'pre',
transformer: options.transformer || 'typescript',
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
}

let entryFileNames = outputOptions.entryFileNames.replace(
/\.(.)?[jt]s$/,
/\.(.)?[jt]sx?$/,
(_, s) => `.d.${s || ''}ts`,
)

Expand Down Expand Up @@ -122,7 +122,7 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =

const resolved = await resolve(context, i.source.value, id)
if (!resolved || resolved.external) continue
if (resolved.id.endsWith('.ts')) {
if (resolved.id.endsWith('.ts') || resolved.id.endsWith('.tsx')) {
s.overwrite(
// @ts-expect-error
i.source.start,
Expand Down Expand Up @@ -296,7 +296,7 @@ async function resolve(
}

function stripExt(filename: string) {
return filename.replace(/\.(.?)[jt]s$/, '')
return filename.replace(/\.(.?)[jt]sx?$/, '')
}

function patchCjsDefaultExport(source: string) {
Expand Down
14 changes: 14 additions & 0 deletions tests/__snapshots__/esbuild.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
exports[`esbuild > generate mode 1`] = `
[
"// <stdout>
// tests/fixtures/component.tsx
function Component() {
return /* @__PURE__ */ React.createElement("div", null, "I'm a div in a tsx component!");
}
// tests/fixtures/main.ts
function hello(s) {
return "hello" + s;
}
var c = Component;
var num = 1;
export {
c,
hello,
num
};
Expand All @@ -17,22 +24,29 @@ export {
import { type Num } from './types';
export type Str = string;
export declare function hello(s: Str): Str;
export declare let c: React.JSX.Element;
export declare let num: Num;
",
"// temp/types.d.ts
export type Num = number;
",
"// temp/types2.d.ts
export type Num2 = number;
",
"// temp/component.d.ts
export declare function Component(): React.JSX.Element;
",
]
`;
exports[`esbuild > write mode 1`] = `
[
"export declare function Component(): React.JSX.Element;
",
"import { type Num } from './types';
export type Str = string;
export declare function hello(s: Str): Str;
export declare let c: React.JSX.Element;
export declare let num: Num;
",
"export type Num = number;
Expand Down
14 changes: 13 additions & 1 deletion tests/__snapshots__/rolldown.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,31 @@
exports[`rolldown 1`] = `
[
"// main.js
import { jsx as _jsx } from "react/jsx-runtime";
//#region tests/fixtures/component.tsx
function Component() {
return _jsx("div", { children: "I'm a div in a tsx component!" });
}
//#endregion
//#region tests/fixtures/main.ts
function hello(s) {
return "hello" + s;
}
let c = Component;
let num = 1;
//#endregion
export { hello, num };",
export { c, hello, num };",
"// temp/component.d.ts
export declare function Component(): React.JSX.Element;
",
"// temp/main.d.ts
import { type Num } from "./types";
export type Str = string;
export declare function hello(s: Str): Str;
export declare let c: React.JSX.Element;
export declare let num: Num;
",
"// temp/types.d.ts
Expand Down
11 changes: 10 additions & 1 deletion tests/__snapshots__/rollup.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,33 @@
exports[`rollup 1`] = `
[
"// main.js
function Component() {
return /* @__PURE__ */ React.createElement("div", null, "I'm a div in a tsx component!");
}
function hello(s) {
return "hello" + s;
}
let c = Component;
let num = 1;
export { hello, num };
export { c, hello, num };
",
"// temp/main.d.ts
import { type Num } from './types';
export type Str = string;
export declare function hello(s: Str): Str;
export declare let c: React.JSX.Element;
export declare let num: Num;
",
"// temp/types.d.ts
export type Num = number;
",
"// temp/types2.d.ts
export type Num2 = number;
",
"// temp/component.d.ts
export declare function Component(): React.JSX.Element;
",
]
`;
3 changes: 3 additions & 0 deletions tests/fixtures/component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function Component(): React.JSX.Element {
return <div>I'm a div in a tsx component!</div>
}
3 changes: 3 additions & 0 deletions tests/fixtures/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { type Num } from './types'
import { Component } from './component'
export type Str = string

export function hello(s: Str): Str {
return 'hello' + s
}

export let c: React.JSX.Element = Component

export let num: Num = 1

0 comments on commit bd1d642

Please sign in to comment.