From 11cb5083b1c9a946ea8c0f499ccbca753908e411 Mon Sep 17 00:00:00 2001 From: Daniel Imfeld Date: Thu, 30 Sep 2021 04:15:30 -1000 Subject: [PATCH] fix: don't overwrite target from tsconfig.json (#408) --- src/transformers/typescript.ts | 14 ++++----- test/fixtures/TypeScriptES2021.svelte | 9 ++++++ test/fixtures/tsconfig.es2021target.json | 6 ++++ test/transformers/typescript.test.ts | 38 ++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 test/fixtures/TypeScriptES2021.svelte create mode 100644 test/fixtures/tsconfig.es2021target.json diff --git a/src/transformers/typescript.ts b/src/transformers/typescript.ts index 127c99b7..f9591ab7 100644 --- a/src/transformers/typescript.ts +++ b/src/transformers/typescript.ts @@ -249,24 +249,20 @@ function getCompilerOptions({ options: Options.Typescript; basePath: string; }): CompilerOptions { - // default options - const compilerOptionsJSON = { - moduleResolution: 'node', - target: 'es6', - }; - - Object.assign(compilerOptionsJSON, options.compilerOptions); + const inputOptions = options.compilerOptions ?? {}; const { errors, options: convertedCompilerOptions } = options.tsconfigFile !== false || options.tsconfigDirectory - ? loadTsconfig(compilerOptionsJSON, filename, options) - : ts.convertCompilerOptionsFromJson(compilerOptionsJSON, basePath); + ? loadTsconfig(inputOptions, filename, options) + : ts.convertCompilerOptionsFromJson(inputOptions, basePath); if (errors.length) { throw new Error(formatDiagnostics(errors, basePath)); } const compilerOptions: CompilerOptions = { + target: ts.ScriptTarget.ES2015, + moduleResolution: ts.ModuleResolutionKind.NodeJs, ...(convertedCompilerOptions as CompilerOptions), importsNotUsedAsValues: ts.ImportsNotUsedAsValues.Error, allowNonTsExtensions: true, diff --git a/test/fixtures/TypeScriptES2021.svelte b/test/fixtures/TypeScriptES2021.svelte new file mode 100644 index 00000000..cd296578 --- /dev/null +++ b/test/fixtures/TypeScriptES2021.svelte @@ -0,0 +1,9 @@ + diff --git a/test/fixtures/tsconfig.es2021target.json b/test/fixtures/tsconfig.es2021target.json new file mode 100644 index 00000000..0874efbc --- /dev/null +++ b/test/fixtures/tsconfig.es2021target.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "target": "es2021", + "skipLibCheck": true + } +} diff --git a/test/transformers/typescript.test.ts b/test/transformers/typescript.test.ts index ce5bd32f..c4366801 100644 --- a/test/transformers/typescript.test.ts +++ b/test/transformers/typescript.test.ts @@ -1,5 +1,6 @@ import { resolve } from 'path'; +import { compile } from 'svelte/compiler'; import type { Diagnostic } from 'typescript'; import sveltePreprocess from '../../src'; @@ -202,5 +203,42 @@ describe('transformer - typescript', () => { esModuleInterop: true, }); }); + + it('should not override the target setting if one is present', async () => { + const tpl = getFixtureContent('TypeScriptES2021.svelte'); + + const opts = sveltePreprocess({ + typescript: { + tsconfigFile: './test/fixtures/tsconfig.es2021target.json', + }, + }); + + const { code } = await preprocess(tpl, opts); + + expect(code).toContain('await '); + expect(code).toContain('async function doIt'); + expect(code).toContain('&&='); + expect(code).toContain('||='); + + // Svelte should be able to compile ES2021. + const { warnings } = compile(code, { name: 'Test' }); + + expect(warnings).toHaveLength(0); + }); + + it('should target ES6 if the configuration has no target setting', async () => { + const tpl = getFixtureContent('TypeScriptES2021.svelte'); + + const opts = sveltePreprocess({ + typescript: { tsconfigFile: false }, + }); + + const { code } = await preprocess(tpl, opts); + + expect(code).not.toContain('await '); + expect(code).not.toContain('async function doIt'); + expect(code).not.toContain('&&='); + expect(code).not.toContain('||='); + }); }); });