Skip to content

Commit

Permalink
feat(tsconfig): 添加 modifyTSConfig 钩子 (#11090)
Browse files Browse the repository at this point in the history
* feat(tsconfig): add biggfish/antd paths

* 添加 modifyTSConfig

* 撤销不必要变更

* 去掉不必要的 args
  • Loading branch information
hanzebang committed May 8, 2023
1 parent 8dad8c5 commit c5c292e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 57 deletions.
118 changes: 61 additions & 57 deletions packages/preset-umi/src/features/tmpFiles/tmpFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,66 +47,70 @@ export default (api: IApi) => {
const isTs5 = api.appData.typescript.tsVersion?.startsWith('5');
const isTslibInstalled = !!api.appData.typescript.tslibVersion;

// x 1、basic config
// x 2、alias
// 3、language service platform
// 4、typing
let umiTsConfig = {
compilerOptions: {
target: 'esnext',
module: 'esnext',
lib: ['dom', 'dom.iterable', 'esnext'],
allowJs: true,
skipLibCheck: true,
moduleResolution: isTs5 ? 'bundler' : 'node',
importHelpers: isTslibInstalled,
noEmit: true,
jsx: api.appData.framework === 'vue' ? 'preserve' : 'react-jsx',
esModuleInterop: true,
sourceMap: true,
baseUrl,
strict: true,
resolveJsonModule: true,
allowSyntheticDefaultImports: true,

// Supported by vue only
...(api.appData.framework === 'vue'
? {
// TODO Actually, it should be vite mode, but here it is written as vue only
// Required in Vite https://vitejs.dev/guide/features.html#typescript
isolatedModules: true,
}
: {}),

paths: {
'@/*': [`${srcPrefix}*`],
'@@/*': [`${umiTempDir}/*`],
[`${api.appData.umi.importSource}`]: [umiDir],
[`${api.appData.umi.importSource}/typings`]: [
`${umiTempDir}/typings`,
],
...(api.config.vite
? {
'@fs/*': ['*'],
}
: {}),
},
},
include: [
`${baseUrl}.${frameworkName}rc.ts`,
`${baseUrl}**/*.d.ts`,
`${baseUrl}**/*.ts`,
`${baseUrl}**/*.tsx`,
api.appData.framework === 'vue' && `${baseUrl}**/*.vue`,
].filter(Boolean),
};

umiTsConfig = await api.applyPlugins({
key: 'modifyTSConfig',
type: api.ApplyPluginsType.modify,
initialValue: umiTsConfig,
});

api.writeTmpFile({
noPluginDir: true,
path: 'tsconfig.json',
// x 1、basic config
// x 2、alias
// 3、language service platform
// 4、typing
content: JSON.stringify(
{
compilerOptions: {
target: 'esnext',
module: 'esnext',
lib: ['dom', 'dom.iterable', 'esnext'],
allowJs: true,
skipLibCheck: true,
moduleResolution: isTs5 ? 'bundler' : 'node',
importHelpers: isTslibInstalled,
noEmit: true,
jsx: api.appData.framework === 'vue' ? 'preserve' : 'react-jsx',
esModuleInterop: true,
sourceMap: true,
baseUrl,
strict: true,
resolveJsonModule: true,
allowSyntheticDefaultImports: true,

// Supported by vue only
...(api.appData.framework === 'vue'
? {
// TODO Actually, it should be vite mode, but here it is written as vue only
// Required in Vite https://vitejs.dev/guide/features.html#typescript
isolatedModules: true,
}
: {}),

paths: {
'@/*': [`${srcPrefix}*`],
'@@/*': [`${umiTempDir}/*`],
[`${api.appData.umi.importSource}`]: [umiDir],
[`${api.appData.umi.importSource}/typings`]: [
`${umiTempDir}/typings`,
],
...(api.config.vite
? {
'@fs/*': ['*'],
}
: {}),
},
},
include: [
`${baseUrl}.${frameworkName}rc.ts`,
`${baseUrl}**/*.d.ts`,
`${baseUrl}**/*.ts`,
`${baseUrl}**/*.tsx`,
api.appData.framework === 'vue' && `${baseUrl}**/*.vue`,
].filter(Boolean),
},
null,
2,
),
content: JSON.stringify(umiTsConfig, null, 2),
});

// typings.d.ts
Expand Down
1 change: 1 addition & 0 deletions packages/preset-umi/src/registerMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default (api: IApi) => {
'modifyServerRendererPath',
'modifyRoutes',
'modifyBabelPresetOpts',
'modifyTSConfig',
].forEach((name) => {
api.registerMethod({ name });
});
Expand Down
1 change: 1 addition & 0 deletions packages/preset-umi/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export type IApi = PluginAPI &
modifyRendererPath: IModify<string, {}>;
modifyRoutes: IModify<Record<string, IRoute>, {}>;
modifyServerRendererPath: IModify<string, {}>;
modifyTSConfig: IModify<Record<string, any>, {}>;
modifyViteConfig: IModify<
ViteInlineConfig,
{
Expand Down

0 comments on commit c5c292e

Please sign in to comment.