Skip to content

Commit

Permalink
fix: don't overwrite target from tsconfig.json (#408)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimfeld authored Sep 30, 2021
1 parent 879960b commit 11cb508
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/transformers/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/TypeScriptES2021.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script lang="ts">
let x = true;
x &&= false;
x ||= true;

async function doIt() {
await (() => true)();
}
</script>
6 changes: 6 additions & 0 deletions test/fixtures/tsconfig.es2021target.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"target": "es2021",
"skipLibCheck": true
}
}
38 changes: 38 additions & 0 deletions test/transformers/typescript.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { resolve } from 'path';

import { compile } from 'svelte/compiler';
import type { Diagnostic } from 'typescript';

import sveltePreprocess from '../../src';
Expand Down Expand Up @@ -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('||=');
});
});
});

0 comments on commit 11cb508

Please sign in to comment.