From 906f124ada3a7379e7d476dd6557df4c4695da3b Mon Sep 17 00:00:00 2001 From: await-ovo <41503212+await-ovo@users.noreply.github.com> Date: Thu, 3 Nov 2022 17:30:22 +0800 Subject: [PATCH] feat: respect target config in tsconfig.json (#757) Co-authored-by: EGOIST <0x142857@gmail.com> --- src/index.ts | 8 +++++++- test/index.test.ts | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 8ed9c026..a1b9840c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -53,7 +53,6 @@ const normalizeOptions = async ( ...optionsOverride, } const options: Partial = { - target: 'node14', outDir: 'dist', ..._options, format: @@ -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 } diff --git a/test/index.test.ts b/test/index.test.ts index 663d789e..f15bc24f 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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")`) +}) \ No newline at end of file