-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathtsconfig.ts
61 lines (54 loc) · 1.78 KB
/
tsconfig.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { GeneratorType } from '@umijs/core';
import { logger, semver } from '@umijs/utils';
import { existsSync, writeFileSync } from 'fs';
import { join } from 'path';
import { IApi } from '../../types';
import { GeneratorHelper } from './utils';
export default (api: IApi) => {
api.describe({
key: 'generator:tsconfig',
});
api.registerGenerator({
key: 'tsconfig',
name: 'Enable Typescript',
description: 'Setup tsconfig.json',
type: GeneratorType.enable,
checkEnable: () => {
return !existsSync(join(api.paths.cwd, 'tsconfig.json'));
},
disabledDescription:
'tsconfig has been enabled; you can remove tsconfig.json then run this again to re-setup',
fn: async () => {
const h = new GeneratorHelper(api);
const reactVersion = api.appData.react.version;
const reactDomVersion = api.appData['react-dom'].version;
if (semver.neq(reactVersion, reactDomVersion)) {
logger.warn(
`The react version ${reactVersion} is not equal to the react-dom version ${reactDomVersion}, please check.`,
);
}
const reactMajorVersion = parseInt(reactVersion.split('.')[0], 10) || 18;
h.addDevDeps({
typescript: '^5',
'@types/react': `^${reactMajorVersion}`,
'@types/react-dom': `^${reactMajorVersion}`,
});
writeFileSync(
join(api.cwd, 'tsconfig.json'),
`
{
"extends": "./${api.appData.hasSrcDir ? 'src/' : ''}.umi/tsconfig.json"
}
`.trimStart(),
);
logger.info('Write tsconfig.json');
const importSource = api.appData.umi.importSource;
writeFileSync(
join(api.cwd, 'typings.d.ts'),
`import '${importSource}/typings';`.trimStart(),
);
logger.info('Write typings.d.ts');
h.installDeps();
},
});
};