Skip to content

Commit 7000c1f

Browse files
fix: buildVersion now correctly reads from project's package.json (#3928)
Co-authored-by: Claude <[email protected]>
1 parent bc3bc10 commit 7000c1f

File tree

7 files changed

+65
-6
lines changed

7 files changed

+65
-6
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
"@module-federation/manifest": patch
3+
"@module-federation/managers": patch
4+
"@module-federation/enhanced": patch
5+
---
6+
7+
fix: BuildVersion now correctly reads from project's package.json
8+
9+
- Fixed getBuildVersion() to accept optional root parameter for correct directory resolution
10+
- Updated StatsManager to use compiler.context when determining build version
11+
- Ensures buildVersion in mf-manifest.json matches the project's package.json version
12+
- Resolves issue #3835 where buildVersion was reading from wrong package.json location
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
it('should read buildVersion from package.json', () => {
2+
const fs = require('fs');
3+
const path = require('path');
4+
5+
// Read the generated mf-manifest.json file (Module Federation manifest)
6+
const manifestPath = path.join(__dirname, 'mf-manifest.json');
7+
expect(fs.existsSync(manifestPath)).toBe(true);
8+
9+
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
10+
11+
// Check that buildVersion matches package.json version
12+
expect(manifest.metaData.buildInfo).toBeDefined();
13+
expect(manifest.metaData.buildInfo.buildVersion).toBe('1.2.3');
14+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "test-build-version",
3+
"version": "1.2.3",
4+
"description": "Test case for BuildVersion reading from package.json"
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'test module';
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const ModuleFederationPlugin =
2+
require('../../../../dist/src/lib/container/ModuleFederationPlugin').default;
3+
4+
module.exports = {
5+
mode: 'development',
6+
entry: './index.js',
7+
devtool: false,
8+
output: {
9+
publicPath: 'http://localhost:3000/',
10+
},
11+
optimization: {
12+
minimize: false,
13+
},
14+
stats: 'none',
15+
plugins: [
16+
new ModuleFederationPlugin({
17+
name: 'build_version_test',
18+
library: { type: 'commonjs-module' },
19+
filename: 'remoteEntry.js',
20+
exposes: {
21+
'./test': './test.js',
22+
},
23+
manifest: true,
24+
}),
25+
],
26+
};

packages/managers/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ export function parseOptions<T, R>(
6262
return items;
6363
}
6464

65-
export function getBuildVersion(): string {
65+
export function getBuildVersion(root?: string): string {
6666
if (process.env['MF_BUILD_VERSION']) {
6767
return process.env['MF_BUILD_VERSION'];
6868
}
69-
const pkg = new PKGJsonManager().readPKGJson();
69+
const pkg = new PKGJsonManager().readPKGJson(root);
7070
if (pkg?.['version'] && typeof pkg['version'] === 'string') {
7171
return pkg['version'];
7272
}

packages/manifest/src/StatsManager.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ class StatsManager {
5050
private _sharedManager: SharedManager = new SharedManager();
5151
private _pkgJsonManager: PKGJsonManager = new PKGJsonManager();
5252

53-
get buildInfo(): StatsBuildInfo {
54-
const pkg = this._pkgJsonManager.readPKGJson(process.cwd());
53+
private getBuildInfo(context?: string): StatsBuildInfo {
54+
const rootPath = context || process.cwd();
55+
const pkg = this._pkgJsonManager.readPKGJson(rootPath);
5556

5657
return {
57-
buildVersion: utils.getBuildVersion(),
58+
buildVersion: utils.getBuildVersion(rootPath),
5859
buildName: utils.getBuildName() || pkg['name'],
5960
};
6061
}
@@ -70,8 +71,8 @@ class StatsManager {
7071
const { context } = compiler.options;
7172
const {
7273
_options: { name },
73-
buildInfo,
7474
} = this;
75+
const buildInfo = this.getBuildInfo(context);
7576
const type = this._pkgJsonManager.getExposeGarfishModuleType(
7677
context || process.cwd(),
7778
);

0 commit comments

Comments
 (0)