Skip to content

chore(dts-plugin): optimize type-001 message #3681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dry-lizards-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/dts-plugin': patch
---

chore(dts-plugin): optimize type-001 message
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 及以上版本)
Expand Down
2 changes: 1 addition & 1 deletion packages/dts-plugin/src/core/lib/DTSManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions packages/dts-plugin/src/core/lib/typeScriptCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 13 additions & 5 deletions packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -274,7 +282,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!');
}
Expand Down
3 changes: 2 additions & 1 deletion packages/error-codes/src/desc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion packages/error-codes/src/getShortErrorMsg.ts
Original file line number Diff line number Diff line change
@@ -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 = (
Expand Down