Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/transformers/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,17 @@ function getCompilerOptions({

const compilerOptions: CompilerOptions = {
target: ts.ScriptTarget.ES2015,
...(convertedCompilerOptions as CompilerOptions),
...convertedCompilerOptions,
// force module(resolution) to esnext and a compatible moduleResolution. Reason:
// transpileModule treats NodeNext as CommonJS because it doesn't read the package.json.
// Also see https://github.com/microsoft/TypeScript/issues/53022 (the filename workaround doesn't work).
module: ts.ModuleKind.ESNext,
moduleResolution: ts.ModuleResolutionKind.Node10,
moduleResolution:
convertedCompilerOptions.moduleResolution ===
ts.ModuleResolutionKind.Bundler
? ts.ModuleResolutionKind.Bundler
: ts.ModuleResolutionKind.Node10,
customConditions: undefined, // fails when using an invalid moduleResolution combination which could happen when we force moduleResolution to Node10
allowNonTsExtensions: true,
// Clear outDir since it causes source map issues when the files aren't actually written to disk.
outDir: undefined,
Expand Down Expand Up @@ -141,7 +146,10 @@ export function loadTsconfig(
compilerOptionsJSON: any,
filename: string,
tsOptions: Options.Typescript,
) {
): {
options: ts.CompilerOptions;
errors: ts.Diagnostic[];
} {
if (typeof tsOptions.tsconfigFile === 'boolean') {
return { errors: [], options: compilerOptionsJSON };
}
Expand Down
19 changes: 18 additions & 1 deletion test/transformers/typescript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import type { Processed } from '../../src/types';
import type { Diagnostic } from 'typescript';

spyConsole({ silent: true });
spyConsole({ silent: false });

const EXPECTED_SCRIPT = getFixtureContent('script.js');

Expand Down Expand Up @@ -203,5 +203,22 @@ describe('transformer - typescript', () => {
expect(code).not.toContain('&&=');
expect(code).not.toContain('||=');
});

it('should remove customConditions option if necessary to prevent config error', async () => {
const opts = sveltePreprocess({
typescript: {
tsconfigFile: false,
compilerOptions: {
// we force a different module resolution in our transformer which
// would fail if we wouldn't also remove the customConditions
moduleResolution: 'NodeNext',
customConditions: ['development'],
},
},
});
const preprocessed = await preprocess(template, opts);

expect(preprocessed.toString?.()).toContain('export var hello');
});
});
});