Skip to content

Commit ebef2d0

Browse files
authored
chore(dts-plugin): optimize type-001 message (#3681)
1 parent f4fb242 commit ebef2d0

File tree

8 files changed

+31
-10
lines changed

8 files changed

+31
-10
lines changed

.changeset/dry-lizards-enjoy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/dts-plugin': patch
3+
---
4+
5+
chore(dts-plugin): optimize type-001 message

apps/website-new/docs/en/guide/troubleshooting/type/TYPE-001.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ When compiling the exported (`exposes`) file TS type, the current project's `tsc
88

99
## Solutions
1010

11-
1. Delete the `node_modules/.cache/mf-types` directory
11+
1. Delete the tsconfig.json `incremental` and `tsBuildInfoFile` configurations in the `cmd` command.
1212
2. Execute the `cmd` command in the error message parameter in the terminal, and repair the file or `tsconfig` according to the error message.
1313

1414
If you want to ignore the type check error in ts, you can set [`compilerOptions.noCheck`](https://www.typescriptlang.org/tsconfig/#noCheck) to `true` in `tsconfig.json` (This option is only supported for ts 5.5 and above)

apps/website-new/docs/zh/guide/troubleshooting/type/TYPE-001.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import ErrorCodeTitle from '@components/ErrorCodeTitle';
88

99
## 解决方法
1010

11-
1. 删除 `node_modules/.cache/mf-types` 目录
11+
1. 删除 `cmd` 命令中的 tsconfig.json `incremental``tsBuildInfoFile` 配置。
1212
2. 在 terminal 中执行报错信息参数中的 `cmd` 命令,根据错误信息对文件或者 `tsconfig` 进行修复。
1313

1414
若你希望忽略 ts 中的类型检查错误,可在 `tsconfig.json` 中设置 [`compilerOptions.noCheck`](https://www.typescriptlang.org/tsconfig/#noCheck)`true`(该选项仅支持 ts 5.5 及以上版本)

packages/dts-plugin/src/core/lib/DTSManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class DTSManager {
195195
} catch (error) {
196196
if (this.options.remote?.abortOnError === false) {
197197
if (this.options.displayErrorInTerminal) {
198-
logger.error(`Unable to compile federated types ${error}`);
198+
logger.error(error);
199199
}
200200
} else {
201201
throw error;

packages/dts-plugin/src/core/lib/typeScriptCompiler.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ export const compileTs = async (
191191
: undefined,
192192
});
193193
} catch (err) {
194+
if (compilerOptions.tsBuildInfoFile) {
195+
try {
196+
await rm(compilerOptions.tsBuildInfoFile);
197+
} catch (e) {
198+
// noop
199+
}
200+
}
194201
throw new Error(
195202
getShortErrorMsg(TYPE_001, typeDescMap, {
196203
cmd,

packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,11 @@ export class GenerateTypesPlugin implements WebpackPluginInstance {
173173
if (isProd) {
174174
const zipAssetName = path.join(zipPrefix, zipName);
175175
const apiAssetName = path.join(zipPrefix, apiFileName);
176-
if (zipTypesPath && !compilation.getAsset(zipAssetName)) {
176+
if (
177+
zipTypesPath &&
178+
!compilation.getAsset(zipAssetName) &&
179+
fs.existsSync(zipTypesPath)
180+
) {
177181
compilation.emitAsset(
178182
zipAssetName,
179183
new compiler.webpack.sources.RawSource(
@@ -183,7 +187,11 @@ export class GenerateTypesPlugin implements WebpackPluginInstance {
183187
);
184188
}
185189

186-
if (apiTypesPath && !compilation.getAsset(apiAssetName)) {
190+
if (
191+
apiTypesPath &&
192+
!compilation.getAsset(apiAssetName) &&
193+
fs.existsSync(apiTypesPath)
194+
) {
187195
compilation.emitAsset(
188196
apiAssetName,
189197
new compiler.webpack.sources.RawSource(
@@ -197,7 +205,7 @@ export class GenerateTypesPlugin implements WebpackPluginInstance {
197205
const isEEXIST = (err: NodeJS.ErrnoException) => {
198206
return err.code == 'EEXIST';
199207
};
200-
if (zipTypesPath) {
208+
if (zipTypesPath && fs.existsSync(zipTypesPath)) {
201209
const zipContent = fs.readFileSync(zipTypesPath);
202210
const zipOutputPath = path.join(
203211
compiler.outputPath,
@@ -233,7 +241,7 @@ export class GenerateTypesPlugin implements WebpackPluginInstance {
233241
});
234242
}
235243

236-
if (apiTypesPath) {
244+
if (apiTypesPath && fs.existsSync(apiTypesPath)) {
237245
const apiContent = fs.readFileSync(apiTypesPath);
238246
const apiOutputPath = path.join(
239247
compiler.outputPath,
@@ -274,7 +282,7 @@ export class GenerateTypesPlugin implements WebpackPluginInstance {
274282
} catch (err) {
275283
callback();
276284
if (dtsManagerOptions.displayErrorInTerminal) {
277-
console.error('Error in mf:generateTypes processAssets hook:', err);
285+
console.error(err);
278286
}
279287
logger.debug('generate types fail!');
280288
}

packages/error-codes/src/desc.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export const runtimeDescMap = {
2323
};
2424

2525
export const typeDescMap = {
26-
[TYPE_001]: 'Failed to generate type declaration.',
26+
[TYPE_001]:
27+
'Failed to generate type declaration. Execute the below cmd to reproduce and fix the error.',
2728
};
2829

2930
export const buildDescMap = {

packages/error-codes/src/getShortErrorMsg.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const getDocsUrl = (errorCode: string) => {
22
const type = errorCode.split('-')[0].toLowerCase();
3-
return `https://module-federation.io/guide/troubleshooting/${type}/${errorCode}`;
3+
return `View the docs to see how tow solve: https://module-federation.io/guide/troubleshooting/${type}/${errorCode}`;
44
};
55

66
export const getShortErrorMsg = (

0 commit comments

Comments
 (0)