Skip to content

Commit 3483574

Browse files
authored
Merge pull request #18 from retejs/fix/type-gen-target
fix(build) type gen target
2 parents e22035f + 39ebe77 commit 3483574

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"case": "1.6.3",
4343
"chalk": "4.1.2",
4444
"commander": "9.4.1",
45+
"comment-json": "^4.2.4",
4546
"eslint": "8.24.0",
4647
"eslint-plugin-simple-import-sort": "^8.0.0",
4748
"execa": "5.1.1",

src/build/gen-types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import execa from 'execa'
22
import { join, relative } from 'path'
33

44
import { SOURCE_FOLDER } from '../consts'
5+
import { readTSConfig } from './utils'
56

67
export const typesDirectoryName = '_types'
78

@@ -15,13 +16,16 @@ export function getDTSPath(srcScript: string, distPath: string, packageDirectory
1516
}
1617

1718
export async function generateTypes(outputDirectories: string[]) {
19+
const config = await readTSConfig(process.cwd())
20+
const target = config?.compilerOptions?.target || 'es5'
21+
1822
for (let i = 0; i < outputDirectories.length; i++) {
1923
const outputDirectory = outputDirectories[i]
2024

2125
await execa('tsc', [
2226
'-d',
2327
'--pretty',
24-
'--target', 'es5',
28+
'--target', target,
2529
'--outDir', join(outputDirectory, typesDirectoryName),
2630
'--skipLibCheck',
2731
'--declarationMap',

src/build/utils.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import { parse } from 'comment-json'
2+
import fs from 'fs'
3+
import { join } from 'path'
4+
import { CompilerOptions, ScriptTarget } from 'typescript'
5+
16
let verbose = false
27

38
export function setVerbose(enabled: boolean) {
@@ -14,3 +19,21 @@ export async function safeExec<T>(func: () => Promise<T>, failMessage: string, e
1419
return error
1520
}
1621
}
22+
23+
interface TSConfig {
24+
compilerOptions?: Omit<CompilerOptions, 'target'> & {
25+
target?: keyof typeof ScriptTarget
26+
}
27+
}
28+
29+
export async function readTSConfig(cwd: string): Promise<null | TSConfig> {
30+
const configPath = join(cwd, 'tsconfig.json')
31+
const exists = await fs.promises.access(configPath).then(() => true).catch(() => false)
32+
33+
if (!exists) return null
34+
35+
const file = await fs.promises.readFile(configPath, 'utf-8')
36+
const config = parse(file) as TSConfig
37+
38+
return config
39+
}

0 commit comments

Comments
 (0)