Skip to content

Commit 57c7288

Browse files
committed
chore: fall back to verbatimModuleSyntax if possible
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
1 parent a7a88c6 commit 57c7288

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/transformers/typescript.ts

+20-6
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,15 @@ function getCompilerOptions({
4747
options: Options.Typescript;
4848
basePath: string;
4949
}): CompilerOptions {
50-
const inputOptions = options.compilerOptions ?? {};
50+
const inputOptions = ts.convertCompilerOptionsFromJson(
51+
options.compilerOptions ?? {},
52+
basePath,
53+
);
5154

5255
const { errors, options: convertedCompilerOptions } =
5356
options.tsconfigFile !== false || options.tsconfigDirectory
5457
? loadTsconfig(inputOptions, filename, options)
55-
: ts.convertCompilerOptionsFromJson(inputOptions, basePath);
58+
: inputOptions;
5659

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

8697
if (
@@ -143,15 +154,18 @@ function transpileTs({
143154
}
144155

145156
export function loadTsconfig(
146-
compilerOptionsJSON: any,
157+
fallback: {
158+
options: ts.CompilerOptions;
159+
errors: ts.Diagnostic[];
160+
},
147161
filename: string,
148162
tsOptions: Options.Typescript,
149163
): {
150164
options: ts.CompilerOptions;
151165
errors: ts.Diagnostic[];
152166
} {
153167
if (typeof tsOptions.tsconfigFile === 'boolean') {
154-
return { errors: [], options: compilerOptionsJSON };
168+
return fallback;
155169
}
156170

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

166180
if (!tsconfigFile) {
167-
return { errors: [], options: compilerOptionsJSON };
181+
return fallback;
168182
}
169183

170184
tsconfigFile = isAbsolute(tsconfigFile)
@@ -193,7 +207,7 @@ export function loadTsconfig(
193207
config,
194208
ts.sys,
195209
basePath,
196-
compilerOptionsJSON,
210+
fallback.options,
197211
tsconfigFile,
198212
);
199213

0 commit comments

Comments
 (0)