From 20d7852c639a1e63960ea8dfb211216c51ffe154 Mon Sep 17 00:00:00 2001 From: 2heal1 Date: Wed, 9 Apr 2025 15:29:00 +0800 Subject: [PATCH 1/2] chore(dts-plugin): optimize type-001 message --- .changeset/dry-lizards-enjoy.md | 5 +++++ .../docs/en/guide/troubleshooting/type/TYPE-001.mdx | 2 +- .../docs/zh/guide/troubleshooting/type/TYPE-001.mdx | 2 +- packages/dts-plugin/src/core/lib/DTSManager.ts | 2 +- packages/dts-plugin/src/core/lib/typeScriptCompiler.ts | 7 +++++++ packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts | 2 +- packages/error-codes/src/desc.ts | 3 ++- packages/error-codes/src/getShortErrorMsg.ts | 2 +- 8 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 .changeset/dry-lizards-enjoy.md diff --git a/.changeset/dry-lizards-enjoy.md b/.changeset/dry-lizards-enjoy.md new file mode 100644 index 00000000000..44d12c5f259 --- /dev/null +++ b/.changeset/dry-lizards-enjoy.md @@ -0,0 +1,5 @@ +--- +'@module-federation/dts-plugin': patch +--- + +chore(dts-plugin): optimize type-001 message diff --git a/apps/website-new/docs/en/guide/troubleshooting/type/TYPE-001.mdx b/apps/website-new/docs/en/guide/troubleshooting/type/TYPE-001.mdx index 3c63e55a012..7b3014442bc 100644 --- a/apps/website-new/docs/en/guide/troubleshooting/type/TYPE-001.mdx +++ b/apps/website-new/docs/en/guide/troubleshooting/type/TYPE-001.mdx @@ -8,7 +8,7 @@ When compiling the exported (`exposes`) file TS type, the current project's `tsc ## Solutions -1. Delete the `node_modules/.cache/mf-types` directory +1. Delete the tsconfig.json `incremental` and `tsBuildInfoFile` configurations in the `cmd` command. 2. Execute the `cmd` command in the error message parameter in the terminal, and repair the file or `tsconfig` according to the error message. 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) diff --git a/apps/website-new/docs/zh/guide/troubleshooting/type/TYPE-001.mdx b/apps/website-new/docs/zh/guide/troubleshooting/type/TYPE-001.mdx index 3c3ef6774d6..89951128173 100644 --- a/apps/website-new/docs/zh/guide/troubleshooting/type/TYPE-001.mdx +++ b/apps/website-new/docs/zh/guide/troubleshooting/type/TYPE-001.mdx @@ -8,7 +8,7 @@ import ErrorCodeTitle from '@components/ErrorCodeTitle'; ## 解决方法 -1. 删除 `node_modules/.cache/mf-types` 目录 +1. 删除 `cmd` 命令中的 tsconfig.json `incremental` 和 `tsBuildInfoFile` 配置。 2. 在 terminal 中执行报错信息参数中的 `cmd` 命令,根据错误信息对文件或者 `tsconfig` 进行修复。 若你希望忽略 ts 中的类型检查错误,可在 `tsconfig.json` 中设置 [`compilerOptions.noCheck`](https://www.typescriptlang.org/tsconfig/#noCheck) 为 `true`(该选项仅支持 ts 5.5 及以上版本) diff --git a/packages/dts-plugin/src/core/lib/DTSManager.ts b/packages/dts-plugin/src/core/lib/DTSManager.ts index 647bf170a27..4c1ba63eca2 100644 --- a/packages/dts-plugin/src/core/lib/DTSManager.ts +++ b/packages/dts-plugin/src/core/lib/DTSManager.ts @@ -195,7 +195,7 @@ class DTSManager { } catch (error) { if (this.options.remote?.abortOnError === false) { if (this.options.displayErrorInTerminal) { - logger.error(`Unable to compile federated types ${error}`); + logger.error(error); } } else { throw error; diff --git a/packages/dts-plugin/src/core/lib/typeScriptCompiler.ts b/packages/dts-plugin/src/core/lib/typeScriptCompiler.ts index 2f01d3e4c5e..eeafc4289e2 100644 --- a/packages/dts-plugin/src/core/lib/typeScriptCompiler.ts +++ b/packages/dts-plugin/src/core/lib/typeScriptCompiler.ts @@ -191,6 +191,13 @@ export const compileTs = async ( : undefined, }); } catch (err) { + if (compilerOptions.tsBuildInfoFile) { + try { + await rm(compilerOptions.tsBuildInfoFile); + } catch (e) { + // noop + } + } throw new Error( getShortErrorMsg(TYPE_001, typeDescMap, { cmd, diff --git a/packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts b/packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts index 80b7e2ee62f..9277e8c602b 100644 --- a/packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts +++ b/packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts @@ -274,7 +274,7 @@ export class GenerateTypesPlugin implements WebpackPluginInstance { } catch (err) { callback(); if (dtsManagerOptions.displayErrorInTerminal) { - console.error('Error in mf:generateTypes processAssets hook:', err); + console.error(err); } logger.debug('generate types fail!'); } diff --git a/packages/error-codes/src/desc.ts b/packages/error-codes/src/desc.ts index af4eb17d7af..658aea2be07 100644 --- a/packages/error-codes/src/desc.ts +++ b/packages/error-codes/src/desc.ts @@ -23,7 +23,8 @@ export const runtimeDescMap = { }; export const typeDescMap = { - [TYPE_001]: 'Failed to generate type declaration.', + [TYPE_001]: + 'Failed to generate type declaration. Execute the below cmd to reproduce and fix the error.', }; export const buildDescMap = { diff --git a/packages/error-codes/src/getShortErrorMsg.ts b/packages/error-codes/src/getShortErrorMsg.ts index 636edc7bdb6..647f19a38b7 100644 --- a/packages/error-codes/src/getShortErrorMsg.ts +++ b/packages/error-codes/src/getShortErrorMsg.ts @@ -1,6 +1,6 @@ const getDocsUrl = (errorCode: string) => { const type = errorCode.split('-')[0].toLowerCase(); - return `https://module-federation.io/guide/troubleshooting/${type}/${errorCode}`; + return `View the docs to see how tow solve: https://module-federation.io/guide/troubleshooting/${type}/${errorCode}`; }; export const getShortErrorMsg = ( From 56ef691ac74de5c99559dd10b44c8102abec06ac Mon Sep 17 00:00:00 2001 From: 2heal1 Date: Wed, 9 Apr 2025 15:41:32 +0800 Subject: [PATCH 2/2] chore: check type file is exist before reading it --- .../src/plugins/GenerateTypesPlugin.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts b/packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts index 9277e8c602b..8112e18149d 100644 --- a/packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts +++ b/packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts @@ -173,7 +173,11 @@ export class GenerateTypesPlugin implements WebpackPluginInstance { if (isProd) { const zipAssetName = path.join(zipPrefix, zipName); const apiAssetName = path.join(zipPrefix, apiFileName); - if (zipTypesPath && !compilation.getAsset(zipAssetName)) { + if ( + zipTypesPath && + !compilation.getAsset(zipAssetName) && + fs.existsSync(zipTypesPath) + ) { compilation.emitAsset( zipAssetName, new compiler.webpack.sources.RawSource( @@ -183,7 +187,11 @@ export class GenerateTypesPlugin implements WebpackPluginInstance { ); } - if (apiTypesPath && !compilation.getAsset(apiAssetName)) { + if ( + apiTypesPath && + !compilation.getAsset(apiAssetName) && + fs.existsSync(apiTypesPath) + ) { compilation.emitAsset( apiAssetName, new compiler.webpack.sources.RawSource( @@ -197,7 +205,7 @@ export class GenerateTypesPlugin implements WebpackPluginInstance { const isEEXIST = (err: NodeJS.ErrnoException) => { return err.code == 'EEXIST'; }; - if (zipTypesPath) { + if (zipTypesPath && fs.existsSync(zipTypesPath)) { const zipContent = fs.readFileSync(zipTypesPath); const zipOutputPath = path.join( compiler.outputPath, @@ -233,7 +241,7 @@ export class GenerateTypesPlugin implements WebpackPluginInstance { }); } - if (apiTypesPath) { + if (apiTypesPath && fs.existsSync(apiTypesPath)) { const apiContent = fs.readFileSync(apiTypesPath); const apiOutputPath = path.join( compiler.outputPath,