Skip to content

Commit a22bd45

Browse files
feat: find package json file (#6)
* feat: find package json file * feat: find package json file * v1.1.0 * fix: better resolution * fix: better resolution * v1.2.0
1 parent 13cbf34 commit a22bd45

File tree

3 files changed

+49
-16
lines changed

3 files changed

+49
-16
lines changed

AutomaticVendorFederation.js

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
const finder = require("find-package-json");
2+
const path = require("path");
3+
const fs = require("fs");
4+
15
const AutomaticVendorFederation = ({
2-
exclude,
3-
ignoreVersion,
4-
packageJson,
5-
ignorePatchVersion = true,
6-
shareFrom = ["dependencies"],
7-
}) => {
6+
exclude,
7+
ignoreVersion,
8+
packageJson,
9+
ignorePatchVersion = true,
10+
shareFrom = ["dependencies"],
11+
}) => {
812
let combinedDependencies;
913
if (!packageJson) {
1014
throw new Error(
@@ -28,17 +32,38 @@ const AutomaticVendorFederation = ({
2832
}
2933
);
3034
return shareableDependencies.reduce((shared, pkg) => {
31-
let packageVersion = require(pkg + "/package.json").version.split(".");
32-
if (ignorePatchVersion) {
33-
packageVersion.pop();
34-
}
35-
if (ignoreVersion && ignoreVersion.includes(pkg)) {
36-
Object.assign(shared, { [pkg]: pkg });
37-
} else {
38-
Object.assign(shared, { [`${pkg}-${packageVersion.join(".")}`]: pkg });
35+
let packageVersion;
36+
try {
37+
const packageExists = fs.existsSync(
38+
path.join(path.dirname(require.resolve(pkg)), "/package.json")
39+
);
40+
if (packageExists) {
41+
console.log('package exists')
42+
const resolvedPackage = path.join(
43+
path.dirname(require.resolve(pkg)),
44+
"/package.json"
45+
);
46+
packageVersion = require(resolvedPackage).version.split(".");
47+
} else {
48+
console.log("searching for package");
49+
const f = finder(path.dirname(require.resolve(pkg)));
50+
const jsonValue = f.next().value;
51+
packageVersion = require(f.next().filename).version.split(".");
52+
}
53+
console.log(packageVersion);
54+
if (ignorePatchVersion) {
55+
packageVersion.pop();
56+
}
57+
if (ignoreVersion && ignoreVersion.includes(pkg)) {
58+
Object.assign(shared, {[pkg]: pkg});
59+
} else {
60+
Object.assign(shared, {[`${pkg}-${packageVersion.join(".")}`]: pkg});
61+
}
62+
} catch (e) {
63+
return shared
3964
}
40-
4165
return shared;
4266
}, {});
4367
};
68+
4469
module.exports = AutomaticVendorFederation;

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@module-federation/automatic-vendor-federation",
3-
"version": "1.0.1",
3+
"version": "1.2.0",
44
"main": "AutomaticVendorFederation.js",
55
"license": "MIT",
66
"scripts": {
@@ -30,5 +30,8 @@
3030
"repository": {
3131
"type": "git",
3232
"url": "https://github.com/module-federation/automatic-vendor-sharing.git"
33+
},
34+
"dependencies": {
35+
"find-package-json": "^1.2.0"
3336
}
3437
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,6 +1631,11 @@ find-npm-prefix@^1.0.2:
16311631
resolved "https://registry.yarnpkg.com/find-npm-prefix/-/find-npm-prefix-1.0.2.tgz#8d8ce2c78b3b4b9e66c8acc6a37c231eb841cfdf"
16321632
integrity sha512-KEftzJ+H90x6pcKtdXZEPsQse8/y/UnvzRKrOSQFprnrGaFuJ62fVkP34Iu2IYuMvyauCyoLTNkJZgrrGA2wkA==
16331633

1634+
find-package-json@^1.2.0:
1635+
version "1.2.0"
1636+
resolved "https://registry.yarnpkg.com/find-package-json/-/find-package-json-1.2.0.tgz#4057d1b943f82d8445fe52dc9cf456f6b8b58083"
1637+
integrity sha512-+SOGcLGYDJHtyqHd87ysBhmaeQ95oWspDKnMXBrnQ9Eq4OkLNqejgoaD8xVWu6GPa0B6roa6KinCMEMcVeqONw==
1638+
16341639
find-up@^2.0.0, find-up@^2.1.0:
16351640
version "2.1.0"
16361641
resolved "https://registry.yarnpkg.com/find-up/-/find-up-2.1.0.tgz#45d1b7e506c717ddd482775a2b77920a3c0c57a7"

0 commit comments

Comments
 (0)