Skip to content

Commit

Permalink
feat(dts-plugin): add dts.displayErrorInTerminal to help control disp…
Browse files Browse the repository at this point in the history
…lay error (#3438)
  • Loading branch information
2heal1 authored Jan 15, 2025
1 parent 6e3afc6 commit 1be9d62
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-steaks-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/dts-plugin': patch
---

feat(dts-plugin): add dts.displayErrorInTerminal to help control display error
8 changes: 8 additions & 0 deletions apps/website-new/docs/en/configure/dts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions apps/website-new/docs/zh/configure/dts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,19 @@ interface DtsHostOptions {
- 默认值:`path.join(process.cwd(),'./tsconfig.json')`

tsconfig 配置文件路径

### cwd

- 类型:`string`
- 是否必填:否
- 默认值:`undefined`

运行 tsc 的路径,默认为项目根目录。

### displayErrorInTerminal

- 类型:`boolean`
- 是否必填:否
- 默认值:`true`

是否在 terminal 输出错误日志
2 changes: 2 additions & 0 deletions packages/dts-plugin/src/core/interfaces/DTSManagerOptions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { moduleFederationPlugin } from '@module-federation/sdk';
import { HostOptions } from './HostOptions';
import { RemoteOptions } from './RemoteOptions';

export interface DTSManagerOptions {
remote?: RemoteOptions;
host?: HostOptions;
extraOptions?: Record<string, any>;
displayErrorInTerminal?: moduleFederationPlugin.PluginDtsOptions['displayErrorInTerminal'];
}
4 changes: 3 additions & 1 deletion packages/dts-plugin/src/core/lib/DTSManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions packages/dts-plugin/src/plugins/ConsumeTypesPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class ConsumeTypesPlugin implements WebpackPluginInstance {
...normalizedConsumeTypes,
},
extraOptions: dtsOptions.extraOptions || {},
displayErrorInTerminal: dtsOptions.displayErrorInTerminal,
};

validateOptions(finalOptions.host);
Expand Down
4 changes: 4 additions & 0 deletions packages/dts-plugin/src/plugins/DevPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ export class DevPlugin implements WebpackPluginInstance {
readonly name = 'MFDevPlugin';
private _options: moduleFederationPlugin.ModuleFederationPluginOptions;
private _devWorker?: DevWorker;
dtsOptions: moduleFederationPlugin.PluginDtsOptions;
fetchTypesPromise: Promise<void>;

constructor(
options: moduleFederationPlugin.ModuleFederationPluginOptions,
dtsOptions: moduleFederationPlugin.PluginDtsOptions,
fetchTypesPromise: Promise<void>,
) {
this._options = options;
this.fetchTypesPromise = fetchTypesPromise;
this.dtsOptions = dtsOptions;
}

static ensureLiveReloadEntry(
Expand Down Expand Up @@ -173,6 +176,7 @@ export class DevPlugin implements WebpackPluginInstance {
generateTypes: defaultGenerateTypes,
consumeTypes: defaultConsumeTypes,
extraOptions: {},
displayErrorInTerminal: this.dtsOptions?.displayErrorInTerminal,
},
'mfOptions.dts',
)(dts);
Expand Down
5 changes: 4 additions & 1 deletion packages/dts-plugin/src/plugins/DtsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class DtsPlugin implements WebpackPluginInstance {
generateTypes: defaultGenerateTypes,
consumeTypes: defaultConsumeTypes,
extraOptions: {},
displayErrorInTerminal: true,
},
'mfOptions.dts',
)(options.dts);
Expand All @@ -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(
Expand Down
8 changes: 7 additions & 1 deletion packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class GenerateTypesPlugin implements WebpackPluginInstance {
...normalizedGenerateTypes,
},
extraOptions: dtsOptions.extraOptions || {},
displayErrorInTerminal: dtsOptions.displayErrorInTerminal,
};

if (dtsOptions.tsConfigPath && !finalOptions.remote.tsConfigPath) {
Expand Down Expand Up @@ -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,
);
}
}
},
);
Expand Down
2 changes: 1 addition & 1 deletion packages/manifest/src/StatsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
26 changes: 24 additions & 2 deletions packages/manifest/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ export function getFileName(
export function getTypesMetaInfo(
pluginOptions: moduleFederationPlugin.ModuleFederationPluginOptions,
context: string,
compilation: Compilation,
): MetaDataTypes {
const defaultRemoteOptions = {
generateAPITypes: true,
Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/types/plugins/ModuleFederationPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export interface PluginDtsOptions {
extraOptions?: Record<string, any>;
implementation?: string;
cwd?: string;
displayErrorInTerminal?: boolean;
}

export type AsyncBoundaryOptions = {
Expand Down

0 comments on commit 1be9d62

Please sign in to comment.