Skip to content

Commit

Permalink
chore: fall back to verbatimModuleSyntax if possible (#649)
Browse files Browse the repository at this point in the history
In case no tsconfig is found whatsoever, enable `verbatimModuleSyntax` to prevent stripping unused imports.

Also fix a related issue where the config passed manually was not properly parsed.

Also print warning message in bold to make it more visible.

related to #643
  • Loading branch information
dummdidumm authored Jul 9, 2024
1 parent a7a88c6 commit 9e5fe59
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/transformers/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ function getCompilerOptions({
options: Options.Typescript;
basePath: string;
}): CompilerOptions {
const inputOptions = options.compilerOptions ?? {};
const inputOptions = ts.convertCompilerOptionsFromJson(
options.compilerOptions ?? {},
basePath,
);

const { errors, options: convertedCompilerOptions } =
options.tsconfigFile !== false || options.tsconfigDirectory
? loadTsconfig(inputOptions, filename, options)
: ts.convertCompilerOptionsFromJson(inputOptions, basePath);
: inputOptions;

if (errors.length) {
throw new Error(formatDiagnostics(errors, basePath));
Expand All @@ -79,8 +82,16 @@ function getCompilerOptions({
if (!warned_verbatim && !compilerOptions.verbatimModuleSyntax) {
warned_verbatim = true;
console.warn(
'\x1b[1m%s\x1b[0m',
'The TypeScript option verbatimModuleSyntax is now required when using Svelte files with lang="ts". Please add it to your tsconfig.json.',
);
// best effort to still add it, if possible, in case no config was found whatsoever
if (
Object.keys(inputOptions.options).length === 0 &&
convertedCompilerOptions === inputOptions.options
) {
compilerOptions.verbatimModuleSyntax = true;
}
}

if (
Expand Down Expand Up @@ -143,15 +154,18 @@ function transpileTs({
}

export function loadTsconfig(
compilerOptionsJSON: any,
fallback: {
options: ts.CompilerOptions;
errors: ts.Diagnostic[];
},
filename: string,
tsOptions: Options.Typescript,
): {
options: ts.CompilerOptions;
errors: ts.Diagnostic[];
} {
if (typeof tsOptions.tsconfigFile === 'boolean') {
return { errors: [], options: compilerOptionsJSON };
return fallback;
}

let basePath = process.cwd();
Expand All @@ -164,7 +178,7 @@ export function loadTsconfig(
ts.findConfigFile(fileDirectory, ts.sys.fileExists);

if (!tsconfigFile) {
return { errors: [], options: compilerOptionsJSON };
return fallback;
}

tsconfigFile = isAbsolute(tsconfigFile)
Expand Down Expand Up @@ -193,7 +207,7 @@ export function loadTsconfig(
config,
ts.sys,
basePath,
compilerOptionsJSON,
fallback.options,
tsconfigFile,
);

Expand Down

0 comments on commit 9e5fe59

Please sign in to comment.