Skip to content

Commit

Permalink
fix: remove customConditions tsconfig option (#648)
Browse files Browse the repository at this point in the history
...in order to prevent invalid config errors which could arise from us forcing a module resolution.
fixes #646
  • Loading branch information
dummdidumm committed Jul 8, 2024
1 parent 9918cb6 commit afb80ae
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
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);

Check warning on line 219 in test/transformers/typescript.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected blank line before this statement

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

0 comments on commit afb80ae

Please sign in to comment.