diff --git a/.changeset/empty-steaks-appear.md b/.changeset/empty-steaks-appear.md new file mode 100644 index 0000000000..8168610e0f --- /dev/null +++ b/.changeset/empty-steaks-appear.md @@ -0,0 +1,5 @@ +--- +'@module-federation/dts-plugin': patch +--- + +feat(dts-plugin): add dts.displayErrorInTerminal to help control display error diff --git a/apps/website-new/docs/en/configure/dts.mdx b/apps/website-new/docs/en/configure/dts.mdx index 4cb4bab148..a584fa0f74 100644 --- a/apps/website-new/docs/en/configure/dts.mdx +++ b/apps/website-new/docs/en/configure/dts.mdx @@ -224,3 +224,11 @@ tsconfig configuration file path - Default value: `undefined` The working directory to run the compiler + +### displayErrorInTerminal + +- Type: `boolean` +- Required: No +- Default value: `true` + +Whether print error log in terminal diff --git a/apps/website-new/docs/zh/configure/dts.mdx b/apps/website-new/docs/zh/configure/dts.mdx index 56533fbafd..3c4b945abb 100644 --- a/apps/website-new/docs/zh/configure/dts.mdx +++ b/apps/website-new/docs/zh/configure/dts.mdx @@ -216,3 +216,19 @@ interface DtsHostOptions { - 默认值:`path.join(process.cwd(),'./tsconfig.json')` tsconfig 配置文件路径 + +### cwd + +- 类型:`string` +- 是否必填:否 +- 默认值:`undefined` + +运行 tsc 的路径,默认为项目根目录。 + +### displayErrorInTerminal + +- 类型:`boolean` +- 是否必填:否 +- 默认值:`true` + +是否在 terminal 输出错误日志 diff --git a/packages/dts-plugin/src/core/interfaces/DTSManagerOptions.ts b/packages/dts-plugin/src/core/interfaces/DTSManagerOptions.ts index 13590eb74c..99cded775e 100644 --- a/packages/dts-plugin/src/core/interfaces/DTSManagerOptions.ts +++ b/packages/dts-plugin/src/core/interfaces/DTSManagerOptions.ts @@ -1,3 +1,4 @@ +import { moduleFederationPlugin } from '@module-federation/sdk'; import { HostOptions } from './HostOptions'; import { RemoteOptions } from './RemoteOptions'; @@ -5,4 +6,5 @@ export interface DTSManagerOptions { remote?: RemoteOptions; host?: HostOptions; extraOptions?: Record; + displayErrorInTerminal?: moduleFederationPlugin.PluginDtsOptions['displayErrorInTerminal']; } diff --git a/packages/dts-plugin/src/core/lib/DTSManager.ts b/packages/dts-plugin/src/core/lib/DTSManager.ts index 183f6316f0..ab1c6e3d7e 100644 --- a/packages/dts-plugin/src/core/lib/DTSManager.ts +++ b/packages/dts-plugin/src/core/lib/DTSManager.ts @@ -166,7 +166,9 @@ class DTSManager { logger.success('Federated types created correctly'); } catch (error) { if (this.options.remote?.abortOnError === false) { - logger.error(`Unable to compile federated types${error}`); + if (this.options.displayErrorInTerminal) { + logger.error(`Unable to compile federated types${error}`); + } } else { throw error; } diff --git a/packages/dts-plugin/src/plugins/ConsumeTypesPlugin.ts b/packages/dts-plugin/src/plugins/ConsumeTypesPlugin.ts index fd9be8a41a..ac1a899607 100644 --- a/packages/dts-plugin/src/plugins/ConsumeTypesPlugin.ts +++ b/packages/dts-plugin/src/plugins/ConsumeTypesPlugin.ts @@ -52,6 +52,7 @@ export class ConsumeTypesPlugin implements WebpackPluginInstance { ...normalizedConsumeTypes, }, extraOptions: dtsOptions.extraOptions || {}, + displayErrorInTerminal: dtsOptions.displayErrorInTerminal, }; validateOptions(finalOptions.host); diff --git a/packages/dts-plugin/src/plugins/DevPlugin.ts b/packages/dts-plugin/src/plugins/DevPlugin.ts index 5a9a178e75..a46e2858c5 100644 --- a/packages/dts-plugin/src/plugins/DevPlugin.ts +++ b/packages/dts-plugin/src/plugins/DevPlugin.ts @@ -36,14 +36,17 @@ export class DevPlugin implements WebpackPluginInstance { readonly name = 'MFDevPlugin'; private _options: moduleFederationPlugin.ModuleFederationPluginOptions; private _devWorker?: DevWorker; + dtsOptions: moduleFederationPlugin.PluginDtsOptions; fetchTypesPromise: Promise; constructor( options: moduleFederationPlugin.ModuleFederationPluginOptions, + dtsOptions: moduleFederationPlugin.PluginDtsOptions, fetchTypesPromise: Promise, ) { this._options = options; this.fetchTypesPromise = fetchTypesPromise; + this.dtsOptions = dtsOptions; } static ensureLiveReloadEntry( @@ -173,6 +176,7 @@ export class DevPlugin implements WebpackPluginInstance { generateTypes: defaultGenerateTypes, consumeTypes: defaultConsumeTypes, extraOptions: {}, + displayErrorInTerminal: this.dtsOptions?.displayErrorInTerminal, }, 'mfOptions.dts', )(dts); diff --git a/packages/dts-plugin/src/plugins/DtsPlugin.ts b/packages/dts-plugin/src/plugins/DtsPlugin.ts index 0c2835f806..84d1a9bdd5 100644 --- a/packages/dts-plugin/src/plugins/DtsPlugin.ts +++ b/packages/dts-plugin/src/plugins/DtsPlugin.ts @@ -31,6 +31,7 @@ export class DtsPlugin implements WebpackPluginInstance { generateTypes: defaultGenerateTypes, consumeTypes: defaultConsumeTypes, extraOptions: {}, + displayErrorInTerminal: true, }, 'mfOptions.dts', )(options.dts); @@ -51,7 +52,9 @@ export class DtsPlugin implements WebpackPluginInstance { // Because the plugin will delete dist/@mf-types.zip while generating types, which will be used in GenerateTypesPlugin // So it should apply after GenerateTypesPlugin - new DevPlugin(options, generateTypesPromise).apply(compiler); + new DevPlugin(options, normalizedDtsOptions, generateTypesPromise).apply( + compiler, + ); // The exposes files may use remote types, so it need to consume types first, otherwise the generate types will fail new GenerateTypesPlugin( diff --git a/packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts b/packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts index 13ae3cde2e..d617cf06b1 100644 --- a/packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts +++ b/packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts @@ -65,6 +65,7 @@ export class GenerateTypesPlugin implements WebpackPluginInstance { ...normalizedGenerateTypes, }, extraOptions: dtsOptions.extraOptions || {}, + displayErrorInTerminal: dtsOptions.displayErrorInTerminal, }; if (dtsOptions.tsConfigPath && !finalOptions.remote.tsConfigPath) { @@ -157,7 +158,12 @@ export class GenerateTypesPlugin implements WebpackPluginInstance { callback(); } catch (err) { callback(); - console.error('Error in mf:generateTypes processAssets hook:', err); + if (finalOptions.displayErrorInTerminal) { + console.error( + 'Error in mf:generateTypes processAssets hook:', + err, + ); + } } }, ); diff --git a/packages/manifest/src/StatsManager.ts b/packages/manifest/src/StatsManager.ts index 873624a1e8..941374a0d5 100644 --- a/packages/manifest/src/StatsManager.ts +++ b/packages/manifest/src/StatsManager.ts @@ -134,7 +134,7 @@ class StatsManager { (this._options?.library?.type as RemoteEntryType | undefined) || 'global', }, - types: getTypesMetaInfo(this._options, compiler.context), + types: getTypesMetaInfo(this._options, compiler.context, compilation), globalName: globalName, pluginVersion: this._pluginVersion, }; diff --git a/packages/manifest/src/utils.ts b/packages/manifest/src/utils.ts index bc90b5158d..cecd6a40bf 100644 --- a/packages/manifest/src/utils.ts +++ b/packages/manifest/src/utils.ts @@ -261,6 +261,7 @@ export function getFileName( export function getTypesMetaInfo( pluginOptions: moduleFederationPlugin.ModuleFederationPluginOptions, context: string, + compilation: Compilation, ): MetaDataTypes { const defaultRemoteOptions = { generateAPITypes: true, @@ -303,11 +304,32 @@ export function getTypesMetaInfo( moduleFederationConfig: pluginOptions, }); + const zip = path.join(zipPrefix, zipName); + const api = path.join(zipPrefix, apiFileName); + + if (!zip || !compilation.getAsset(zip)) { + return { + path: '', + name: '', + zip: '', + api: '', + }; + } + + if (!api || !compilation.getAsset(api)) { + return { + path: '', + name: '', + zip, + api: '', + }; + } + return { path: '', name: '', - zip: path.join(zipPrefix, zipName), - api: path.join(zipPrefix, apiFileName), + zip, + api, }; } catch (err) { logger.warn( diff --git a/packages/sdk/src/types/plugins/ModuleFederationPlugin.ts b/packages/sdk/src/types/plugins/ModuleFederationPlugin.ts index eaebe46717..70105e26ac 100644 --- a/packages/sdk/src/types/plugins/ModuleFederationPlugin.ts +++ b/packages/sdk/src/types/plugins/ModuleFederationPlugin.ts @@ -167,6 +167,7 @@ export interface PluginDtsOptions { extraOptions?: Record; implementation?: string; cwd?: string; + displayErrorInTerminal?: boolean; } export type AsyncBoundaryOptions = {