From ef69e6299d68b2186071eee560c176720d2b3c86 Mon Sep 17 00:00:00 2001 From: 2heal1 Date: Tue, 21 Jan 2025 17:52:44 +0800 Subject: [PATCH] fix: adjust stats fetch logic --- .changeset/clever-pianos-cough.md | 2 +- .../src/plugins/GenerateTypesPlugin.ts | 4 ++-- packages/manifest/src/utils.ts | 16 ++++++++++++++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/.changeset/clever-pianos-cough.md b/.changeset/clever-pianos-cough.md index f5921f2e7a..20a5b8db85 100644 --- a/.changeset/clever-pianos-cough.md +++ b/.changeset/clever-pianos-cough.md @@ -2,4 +2,4 @@ '@module-federation/dts-plugin': patch --- -perf(dts-plugin): only block build process in prod env when generating types +fix(dts-plugin): only block build process in prod env when generating types diff --git a/packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts b/packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts index 1b96954510..5c62059f3d 100644 --- a/packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts +++ b/packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts @@ -120,7 +120,7 @@ export class GenerateTypesPlugin implements WebpackPluginInstance { const apiAssetName = path.join(zipPrefix, apiFileName); if (zipTypesPath && !compilation.getAsset(zipAssetName)) { compilation.emitAsset( - path.join(zipPrefix, zipName), + zipAssetName, new compiler.webpack.sources.RawSource( fs.readFileSync(zipTypesPath), false, @@ -130,7 +130,7 @@ export class GenerateTypesPlugin implements WebpackPluginInstance { if (apiTypesPath && !compilation.getAsset(apiAssetName)) { compilation.emitAsset( - path.join(zipPrefix, apiFileName), + apiAssetName, new compiler.webpack.sources.RawSource( fs.readFileSync(apiTypesPath), false, diff --git a/packages/manifest/src/utils.ts b/packages/manifest/src/utils.ts index cecd6a40bf..b1e3288535 100644 --- a/packages/manifest/src/utils.ts +++ b/packages/manifest/src/utils.ts @@ -1,5 +1,6 @@ import { Chunk, Compilation, StatsCompilation, StatsModule } from 'webpack'; import path from 'path'; +import fs from 'fs'; import { StatsAssets, moduleFederationPlugin, @@ -307,7 +308,18 @@ export function getTypesMetaInfo( const zip = path.join(zipPrefix, zipName); const api = path.join(zipPrefix, apiFileName); - if (!zip || !compilation.getAsset(zip)) { + const getTargetFile = (filename: string) => { + try { + if (!isDev()) { + return Boolean(compilation.getAsset(path.join(zipPrefix, filename))); + } + return Boolean(fs.readFileSync(filename)); + } catch (e) { + return false; + } + }; + + if (!zip || !getTargetFile(zipName)) { return { path: '', name: '', @@ -316,7 +328,7 @@ export function getTypesMetaInfo( }; } - if (!api || !compilation.getAsset(api)) { + if (!api || !getTargetFile(apiFileName)) { return { path: '', name: '',