@@ -16,6 +16,8 @@ import {
1616import { PrefetchPlugin } from '@module-federation/data-prefetch/cli' ;
1717import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path' ;
1818import type { Compiler , WebpackPluginInstance } from 'webpack' ;
19+ import fs from 'node:fs' ;
20+ import path from 'node:path' ;
1921import SharePlugin from '../sharing/SharePlugin' ;
2022import ContainerPlugin from './ContainerPlugin' ;
2123import ContainerReferencePlugin from './ContainerReferencePlugin' ;
@@ -46,6 +48,33 @@ const validate = createSchemaValidation(
4648 } ,
4749) ;
4850
51+ function getEnhancedPackageVersion ( ) : string {
52+ let currentDir = __dirname ;
53+
54+ while ( true ) {
55+ const packageJsonPath = path . join ( currentDir , 'package.json' ) ;
56+
57+ if ( fs . existsSync ( packageJsonPath ) ) {
58+ const pkg = JSON . parse ( fs . readFileSync ( packageJsonPath , 'utf-8' ) ) as {
59+ name ?: string ;
60+ version ?: string ;
61+ } ;
62+
63+ if ( pkg . name === '@module-federation/enhanced' && pkg . version ) {
64+ return pkg . version ;
65+ }
66+ }
67+
68+ const parentDir = path . dirname ( currentDir ) ;
69+ if ( parentDir === currentDir ) {
70+ break ;
71+ }
72+ currentDir = parentDir ;
73+ }
74+
75+ throw new Error ( 'Unable to resolve @module-federation/enhanced package.json' ) ;
76+ }
77+
4978class ModuleFederationPlugin implements WebpackPluginInstance {
5079 private _options : moduleFederationPlugin . ModuleFederationPluginOptions ;
5180 private _statsPlugin ?: StatsPlugin ;
@@ -237,9 +266,8 @@ class ModuleFederationPlugin implements WebpackPluginInstance {
237266 } ) ;
238267
239268 if ( ! disableManifest ) {
240- const pkg = require ( '../../../../package.json' ) ;
241269 this . _statsPlugin = new StatsPlugin ( options , {
242- pluginVersion : pkg . version ,
270+ pluginVersion : getEnhancedPackageVersion ( ) ,
243271 bundler : 'webpack' ,
244272 } ) ;
245273 this . _statsPlugin . apply ( compiler ) ;
0 commit comments