Skip to content

Commit 0aee174

Browse files
committed
refactor: get rid of tsx
1 parent 307464e commit 0aee174

12 files changed

+114
-427
lines changed

.npm/compile.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Compile source for NPM
3+
*/
4+
5+
import swc from '@swc/core'
6+
import { mkdirSync, rmdirSync, writeFileSync } from 'node:fs'
7+
import { glob } from 'node:fs/promises'
8+
import path, { dirname } from 'node:path'
9+
import { fileURLToPath } from 'node:url'
10+
import { updateImports } from './updateImports.ts'
11+
12+
const __dirname = dirname(fileURLToPath(import.meta.url))
13+
14+
const outDir = path.join(__dirname, '..', 'dist')
15+
16+
try {
17+
rmdirSync(outDir, { recursive: true })
18+
} catch {
19+
// pass
20+
}
21+
22+
for await (const file of glob(`./src/**/*.ts`)) {
23+
if (file.endsWith('.spec.ts')) continue
24+
let compiled = (
25+
await swc.transformFile(file, {
26+
jsc: {
27+
parser: {
28+
syntax: 'typescript',
29+
},
30+
target: 'es2024',
31+
},
32+
module: {
33+
type: 'es6',
34+
},
35+
})
36+
).code
37+
38+
compiled = updateImports(compiled)
39+
40+
const targetFile = path.join(
41+
outDir,
42+
file.replace('src/', '').replace(/\.ts$/, '.js'),
43+
)
44+
45+
mkdirSync(dirname(targetFile), { recursive: true })
46+
47+
writeFileSync(targetFile, compiled, 'utf8')
48+
49+
console.log(file)
50+
}

.npm/tsconfig.npm.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"emitDeclarationOnly": true,
5+
"declaration": true,
6+
"noEmit": false
7+
},
8+
"include": ["../src"]
9+
}

.npm/updateImports.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import assert from 'node:assert'
2+
import { describe, it } from 'node:test'
3+
import { updateImports } from './updateImports.ts'
4+
5+
void describe('updateImports', () => {
6+
void it('replaces .ts with .js in relative imports', () => {
7+
const input = `import { foo } from './bar.ts';`
8+
const expected = `import { foo } from './bar.js';`
9+
assert.equal(updateImports(input), expected)
10+
})
11+
})

.npm/updateImports.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const updateImports = (source: string): string =>
2+
source.replace(/from ['"](.+?)\.ts['"]/g, "from '$1.js'")

0 commit comments

Comments
 (0)