From db603acd7eca03aa12d104b964843d956a7d9578 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Mon, 8 Jul 2024 22:04:07 +0200 Subject: [PATCH] fix: remove customConditions tsconfig option ...in order to prevent invalid config errors which could arise from us forcing a module resolution. fixes #646 --- src/transformers/typescript.ts | 14 +++++++++++--- test/transformers/typescript.test.ts | 19 ++++++++++++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/transformers/typescript.ts b/src/transformers/typescript.ts index d652a56..a04efbc 100644 --- a/src/transformers/typescript.ts +++ b/src/transformers/typescript.ts @@ -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, @@ -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 }; } diff --git a/test/transformers/typescript.test.ts b/test/transformers/typescript.test.ts index 53c67e9..756affc 100644 --- a/test/transformers/typescript.test.ts +++ b/test/transformers/typescript.test.ts @@ -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'); @@ -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'); + }); }); });