Skip to content

Commit

Permalink
feat: respect target config in tsconfig.json (#757)
Browse files Browse the repository at this point in the history
Co-authored-by: EGOIST <0x142857@gmail.com>
  • Loading branch information
await-ovo and egoist committed Nov 3, 2022
1 parent 48b3381 commit 906f124
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/index.ts
Expand Up @@ -53,7 +53,6 @@ const normalizeOptions = async (
...optionsOverride,
}
const options: Partial<NormalizedOptions> = {
target: 'node14',
outDir: 'dist',
..._options,
format:
Expand Down Expand Up @@ -113,10 +112,17 @@ const normalizeOptions = async (
...(options.dts.compilerOptions || {}),
}
}
if (!options.target) {
options.target = tsconfig.data?.compilerOptions?.target?.toLowerCase()
}
} else if (options.tsconfig) {
throw new PrettyError(`Cannot find tsconfig: ${options.tsconfig}`)
}

if (!options.target) {
options.target = 'node14'
}

return options as NormalizedOptions
}

Expand Down
41 changes: 41 additions & 0 deletions test/index.test.ts
Expand Up @@ -1102,3 +1102,44 @@ test('treeshake should work with hashbang', async () => {
"
`)
})

test('support target in tsconfig.json', async () => {
const { getFileContent } = await run(
getTestName(),
{
'input.ts': `await import('./foo')`,
'foo.ts': `export default 'foo'`,
'tsconfig.json': `{
"compilerOptions": {
"baseUrl":".",
"target": "esnext"
}
}`
},
{
flags: ['--format', 'esm', ],
}
)
expect(await getFileContent('dist/input.mjs')).contains('await import(')
})

test('override target in tsconfig.json', async () => {
await expect(
run(
getTestName(),
{
'input.ts': `await import('./foo')`,
'foo.ts': `export default 'foo'`,
'tsconfig.json': `{
"compilerOptions": {
"baseUrl":".",
"target": "esnext"
}
}`
},
{
flags: ['--format', 'esm', '--target', 'es2018' ],
}
)
).rejects.toThrowError(`Top-level await is not available in the configured target environment ("es2018")`)
})

0 comments on commit 906f124

Please sign in to comment.