Skip to content

Commit d0b4766

Browse files
dummdidummSimon Holthausen
and
Simon Holthausen
authored
feat: support tsconfig extends (#328)
* (feat) Support tsconfig extends Fixes #300 * lint Co-authored-by: Simon Holthausen <[email protected]>
1 parent 795c02b commit d0b4766

File tree

4 files changed

+84
-31
lines changed

4 files changed

+84
-31
lines changed

src/transformers/typescript.ts

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { dirname } from 'path';
1+
import { dirname, isAbsolute, join } from 'path';
22

33
import ts from 'typescript';
44

@@ -53,6 +53,53 @@ const importTransformer: ts.TransformerFactory<ts.SourceFile> = (context) => {
5353
return (node) => ts.visitNode(node, visit);
5454
};
5555

56+
export function loadTsconfig(
57+
compilerOptionsJSON: any,
58+
filename: string,
59+
tsOptions: Options.Typescript,
60+
) {
61+
if (typeof tsOptions.tsconfigFile === 'boolean') {
62+
return { errors: [], options: compilerOptionsJSON };
63+
}
64+
65+
let basePath = process.cwd();
66+
67+
const fileDirectory = (tsOptions.tsconfigDirectory ||
68+
dirname(filename)) as string;
69+
70+
let tsconfigFile =
71+
tsOptions.tsconfigFile ||
72+
ts.findConfigFile(fileDirectory, ts.sys.fileExists);
73+
74+
tsconfigFile = isAbsolute(tsconfigFile)
75+
? tsconfigFile
76+
: join(basePath, tsconfigFile);
77+
78+
basePath = dirname(tsconfigFile);
79+
80+
const { error, config } = ts.readConfigFile(tsconfigFile, ts.sys.readFile);
81+
82+
if (error) {
83+
throw new Error(formatDiagnostics(error, basePath));
84+
}
85+
86+
// Do this so TS will not search for initial files which might take a while
87+
config.include = [];
88+
89+
let { errors, options } = ts.parseJsonConfigFileContent(
90+
config,
91+
ts.sys,
92+
basePath,
93+
compilerOptionsJSON,
94+
tsconfigFile,
95+
);
96+
97+
// Filter out "no files found error"
98+
errors = errors.filter((d) => d.code !== 18003);
99+
100+
return { errors, options };
101+
}
102+
56103
const transformer: Transformer<Options.Typescript> = ({
57104
content,
58105
filename,
@@ -64,38 +111,14 @@ const transformer: Transformer<Options.Typescript> = ({
64111
target: 'es6',
65112
};
66113

67-
let basePath = process.cwd();
68-
69-
if (options.tsconfigFile !== false || options.tsconfigDirectory) {
70-
const fileDirectory = (options.tsconfigDirectory ||
71-
dirname(filename)) as string;
72-
73-
const tsconfigFile =
74-
options.tsconfigFile ||
75-
ts.findConfigFile(fileDirectory, ts.sys.fileExists);
76-
77-
if (typeof tsconfigFile === 'string') {
78-
basePath = dirname(tsconfigFile);
79-
80-
const { error, config } = ts.readConfigFile(
81-
tsconfigFile,
82-
ts.sys.readFile,
83-
);
84-
85-
if (error) {
86-
throw new Error(formatDiagnostics(error, basePath));
87-
}
88-
89-
Object.assign(compilerOptionsJSON, config.compilerOptions);
90-
}
91-
}
114+
const basePath = process.cwd();
92115

93116
Object.assign(compilerOptionsJSON, options.compilerOptions);
94117

95-
const {
96-
errors,
97-
options: convertedCompilerOptions,
98-
} = ts.convertCompilerOptionsFromJson(compilerOptionsJSON, basePath);
118+
const { errors, options: convertedCompilerOptions } =
119+
options.tsconfigFile !== false || options.tsconfigDirectory
120+
? loadTsconfig(compilerOptionsJSON, filename, options)
121+
: ts.convertCompilerOptionsFromJson(compilerOptionsJSON, basePath);
99122

100123
if (errors.length) {
101124
throw new Error(formatDiagnostics(errors, basePath));

test/fixtures/tsconfig.extends1.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "./tsconfig.extends2.json",
3+
"compilerOptions": {
4+
"esModuleInterop": true,
5+
}
6+
}

test/fixtures/tsconfig.extends2.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"skipLibCheck": false,
5+
}
6+
}

test/transformers/typescript.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ import { resolve } from 'path';
33
import type { Diagnostic } from 'typescript';
44

55
import sveltePreprocess from '../../src';
6+
import { loadTsconfig } from '../../src/transformers/typescript';
67
import type { Processed } from '../../src/types';
7-
import { preprocess, getFixtureContent, spyConsole } from '../utils';
8+
import {
9+
preprocess,
10+
getFixtureContent,
11+
spyConsole,
12+
getTestAppFilename,
13+
} from '../utils';
814

915
spyConsole({ silent: true });
1016

@@ -103,5 +109,17 @@ describe('transformer - typescript', () => {
103109

104110
return expect(code).toContain(getFixtureContent('script.js'));
105111
});
112+
113+
it('supports extends field', () => {
114+
const { options } = loadTsconfig({}, getTestAppFilename(), {
115+
tsconfigFile: './test/fixtures/tsconfig.extends1.json',
116+
});
117+
118+
expect(options).toMatchObject({
119+
module: 5,
120+
skipLibCheck: false,
121+
esModuleInterop: true,
122+
});
123+
});
106124
});
107125
});

0 commit comments

Comments
 (0)