Skip to content

Commit d905ec8

Browse files
authored
feat: tsx support (#1)
* test: add tsx * feat: add tsx compilation
1 parent b27b1ca commit d905ec8

File tree

4 files changed

+205
-19
lines changed

4 files changed

+205
-19
lines changed

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@
2626
"@egoist/prettier-config": "1.0.0",
2727
"prettier": "2.8.4",
2828
"tsup": "6.6.3",
29-
"typescript": "4.9.5",
3029
"vitest": "0.28.5"
3130
},
3231
"dependencies": {
33-
"ts-blank-space": "^0.4.1"
32+
"@rspack/core": "^1.0.8",
33+
"ts-blank-space": "^0.4.1",
34+
"typescript": "5.1.6 - 5.6.x"
35+
},
36+
"peerDependencies": {
37+
"typescript": "^5.1.6"
3438
}
3539
}

pnpm-lock.yaml

Lines changed: 151 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import ts from "typescript"
2+
import type { LoaderDefinitionFunction } from "@rspack/core"
13
import type TsBlankSpace from "ts-blank-space"
4+
import { type blankSourceFile as TsBlankSourceFile } from "ts-blank-space"
25

36
let tsBlankSpace: typeof TsBlankSpace
7+
let tsBlankSourceFile: typeof TsBlankSourceFile
48

59
class TsBlankLoaderError extends Error {
610
name = "TsBlankLoaderError"
@@ -11,20 +15,35 @@ class TsBlankLoaderError extends Error {
1115
}
1216
}
1317

14-
export default async function (content: any) {
15-
// @ts-ignore
18+
export default async function (this: LoaderDefinitionFunction, content: any) {
1619
const callback = this.async()
1720

1821
if (!tsBlankSpace) {
19-
const { default: tsBlankSpaceImport } = await import("ts-blank-space")
22+
const {
23+
default: tsBlankSpaceImport,
24+
blankSourceFile: tsBlankSourceFileImport,
25+
} = await import("ts-blank-space")
2026
tsBlankSpace = tsBlankSpaceImport
27+
tsBlankSourceFile = tsBlankSourceFileImport
2128
}
2229

2330
try {
2431
const onError = (n: any): void => {
2532
throw new TsBlankLoaderError(n)
2633
}
2734

35+
if (this.resourcePath?.includes(".tsx")) {
36+
const tsxSource = ts.createSourceFile(
37+
this.resourcePath,
38+
content,
39+
ts.ScriptTarget.ESNext,
40+
false,
41+
ts.ScriptKind.TSX,
42+
)
43+
const jsxOutput = tsBlankSourceFile(tsxSource, onError)
44+
return callback(null, jsxOutput)
45+
}
46+
2847
const output = tsBlankSpace(content, onError)
2948
return callback(null, output)
3049
} catch (err: any) {

test/index.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { afterEach, describe, expect, it, vi } from "vitest"
22
import tsBlankLoaderOriginal from "../src"
33

4-
function setupModule() {
4+
function setupModule({ mockedThis } = { mockedThis: {} }) {
55
const mockCallback = vi.fn()
66
const mockThis = {
77
async: vi.fn().mockImplementation(() => mockCallback),
8+
...mockedThis,
89
}
910
const tsBlankLoader = tsBlankLoaderOriginal.bind(mockThis)
1011
return { mockCallback, mockThis, tsBlankLoader }
@@ -24,4 +25,28 @@ describe("ts-blank-loader", () => {
2425
expect(mockCallback).toHaveBeenCalledTimes(1)
2526
expect(mockCallback).toHaveBeenCalledWith(null, "let x ;")
2627
})
28+
29+
it("should call callback with preserved tsx", async () => {
30+
const { mockCallback, mockThis, tsBlankLoader } = setupModule({
31+
mockedThis: {
32+
resourcePath: "App.tsx",
33+
},
34+
})
35+
36+
const tsxContent = `
37+
const App = () => {
38+
return (
39+
<div className={styles.container}>
40+
<h1 className={styles.title}>Hello, World!</h1>
41+
</div>
42+
)
43+
}
44+
`
45+
46+
await tsBlankLoader(tsxContent)
47+
48+
expect(mockThis.async).toHaveBeenCalledTimes(1)
49+
expect(mockCallback).toHaveBeenCalledTimes(1)
50+
expect(mockCallback).toHaveBeenCalledWith(null, tsxContent)
51+
})
2752
})

0 commit comments

Comments
 (0)