Skip to content

Commit 1a1d3b7

Browse files
committed
feat(core): enable package hoisting in workspaces
1 parent 1efe198 commit 1a1d3b7

File tree

7 files changed

+67
-27
lines changed

7 files changed

+67
-27
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@
3232
"**/CVS": true,
3333
"**/.DS_Store": true,
3434
"**/Thumbs.db": true
35-
}
35+
},
36+
"hide-files.files": []
3637
}

libs/native-federation-core/src/lib/config/share-utils.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import {
99
prepareSkipList,
1010
SkipList,
1111
} from '../core/default-skip-list';
12-
import { findPackageJsonFiles, getVersionMaps, VersionMap } from '../utils/package-info';
12+
import { findDepPackageJson, findPackageJsonFiles, getVersionMaps, VersionMap } from '../utils/package-info';
1313
import { getConfigContext } from './configuration-context';
14+
import { logger } from '../utils/logger';
1415

1516
let inferVersion = false;
1617

@@ -150,7 +151,7 @@ function findSecondaries(
150151

151152
function getSecondaries(
152153
includeSecondaries: IncludeSecondariesOptions,
153-
packagePath: string,
154+
libPath: string,
154155
key: string,
155156
shareObject: SharedConfig
156157
): Record<string, SharedConfig> | null {
@@ -164,7 +165,7 @@ function getSecondaries(
164165
}
165166
}
166167

167-
const libPath = path.join(path.dirname(packagePath), 'node_modules', key);
168+
// const libPath = path.join(path.dirname(packagePath), 'node_modules', key);
168169

169170
if (!fs.existsSync(libPath)) {
170171
return {};
@@ -331,9 +332,19 @@ export function share(shareObjects: Config, projectPath = ''): Config {
331332
result[key] = shareObject;
332333

333334
if (includeSecondaries) {
335+
336+
const libPackageJson = findDepPackageJson(key, path.dirname(packagePath));
337+
338+
if (!libPackageJson) {
339+
logger.error('Could not find folder containing dep ' + key);
340+
continue;
341+
}
342+
343+
const libPath = path.dirname(libPackageJson);
344+
334345
const secondaries = getSecondaries(
335346
includeSecondaries,
336-
packagePath,
347+
libPath,
337348
key,
338349
shareObject
339350
);

libs/native-federation-core/src/lib/config/with-native-federation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function normalizeShared(
5454
{}
5555
);
5656

57-
result = share(result) as Record<string, NormalizedSharedConfig>;
57+
//result = share(result) as Record<string, NormalizedSharedConfig>;
5858
}
5959

6060
result = Object.keys(result)

libs/native-federation-core/src/lib/core/federation-builder.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useWorkspace } from '../config/configuration-context';
1+
import { getConfigContext, useWorkspace } from '../config/configuration-context';
22
import { NormalizedFederationConfig } from '../config/federation-config';
33
import { BuildAdapter, setBuildAdapter } from './build-adapter';
44
import { buildForFederation, defaultBuildParams } from './build-for-federation';
@@ -20,6 +20,7 @@ async function init(params: BuildHelperParams): Promise<void> {
2020
fedOptions = params.options;
2121
useWorkspace(params.options.workspaceRoot);
2222
config = await loadFederationConfig(fedOptions);
23+
params.options.workspaceRoot = getConfigContext().workspaceRoot ?? params.options.workspaceRoot;
2324
externals = getExternals(config);
2425
}
2526

libs/native-federation-core/src/lib/utils/package-info.ts

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { dir } from 'console';
12
import * as fs from 'fs';
23
import * as path from 'path';
34
import { logger } from './logger';
@@ -54,6 +55,7 @@ export function expandFolders(child: string, parent: string): string[] {
5455
return result;
5556
}
5657

58+
5759
export function getPackageInfo(
5860
packageName: string,
5961
workspaceRoot: string,
@@ -70,7 +72,7 @@ export function getPackageInfo(
7072
const packageJsonInfos = getPackageJsonFiles(projectRoot, workspaceRoot);
7173

7274
for (const info of packageJsonInfos) {
73-
const cand = _getPackageInfo(packageName, info);
75+
const cand = _getPackageInfo(packageName, info.directory);
7476
if (cand) {
7577
return cand;
7678
}
@@ -102,8 +104,8 @@ export function getPackageJsonFiles(project: string, workspace: string): Package
102104

103105
maps = findPackageJsonFiles(project, workspace)
104106
.map(f => {
105-
const content = fs.readFileSync(f, 'utf-8');
106-
const directory = path.dirname(f);
107+
const content = JSON.parse(fs.readFileSync(f, 'utf-8'));
108+
const directory = normalize(path.dirname(f), true);
107109
const result: PackageJsonInfo = {
108110
content, directory
109111
};
@@ -114,26 +116,51 @@ export function getPackageJsonFiles(project: string, workspace: string): Package
114116
return maps;
115117
}
116118

117-
export function _getPackageInfo(
118-
packageName: string,
119-
packageJsonInfo: PackageJsonInfo,
120-
): PackageInfo | null {
121-
119+
export function findDepPackageJson(packageName: string, projectRoot: string): string | null {
122120
const mainPkgName = getPkgFolder(packageName);
123121

124-
const mainPkgPath = path.join(packageJsonInfo.directory, 'node_modules', mainPkgName);
125-
// const mainPkgJsonPath = path.join(mainPkgPath, 'package.json');
122+
let mainPkgPath = path.join(projectRoot, 'node_modules', mainPkgName);
123+
let mainPkgJsonPath = path.join(mainPkgPath, 'package.json');
124+
125+
let directory = projectRoot;
126+
127+
while (path.dirname(directory) !== directory) {
128+
129+
if (fs.existsSync(mainPkgJsonPath)) {
130+
break;
131+
}
132+
133+
directory = normalize(path.dirname(directory), true);
134+
135+
mainPkgPath = path.join(directory, 'node_modules', mainPkgName);
136+
mainPkgJsonPath = path.join(mainPkgPath, 'package.json');
137+
}
126138

127-
if (!fs.existsSync(mainPkgPath)) {
139+
if (!fs.existsSync(mainPkgJsonPath)) {
128140
// TODO: Add logger
129141
// context.logger.warn('No package.json found for ' + packageName);
130142
logger.verbose('No package.json found for ' + packageName + ' in ' + mainPkgPath);
131143

132144
return null;
133145
}
146+
return mainPkgJsonPath;
147+
148+
}
149+
150+
export function _getPackageInfo(
151+
packageName: string,
152+
directory: string,
153+
): PackageInfo | null {
154+
155+
const mainPkgName = getPkgFolder(packageName);
156+
const mainPkgJsonPath = findDepPackageJson(packageName, directory);
157+
158+
if (!mainPkgJsonPath) {
159+
return null;
160+
}
134161

135-
//const mainPkgJson = readJson(mainPkgJsonPath);
136-
const mainPkgJson = packageJsonInfo.content;
162+
const mainPkgPath = path.dirname(mainPkgJsonPath);
163+
const mainPkgJson = readJson(mainPkgJsonPath);
137164

138165
const version = mainPkgJson['version'] as string;
139166
const esm = mainPkgJson['type'] === 'module';
@@ -206,7 +233,7 @@ export function _getPackageInfo(
206233
};
207234
}
208235

209-
const secondaryPgkPath = path.join(packageJsonInfo.directory, 'node_modules', packageName);
236+
const secondaryPgkPath = path.join(mainPkgPath, relSecondaryPath);
210237
const secondaryPgkJsonPath = path.join(secondaryPgkPath, 'package.json');
211238
let secondaryPgkJson: PartialPackageJson | null = null;
212239
if (fs.existsSync(secondaryPgkJsonPath)) {

libs/native-federation/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@angular-architects/native-federation",
3-
"version": "1.0.0-beta.1",
3+
"version": "1.1.0-beta.0",
44
"main": "src/index.js",
55
"generators": "./collection.json",
66
"builders": "./builders.json",
@@ -23,8 +23,8 @@
2323
"rollup-plugin-node-externals": "^4.1.1",
2424
"esbuild": "^0.15.5",
2525
"@babel/core": "^7.19.0",
26-
"@softarc/native-federation": "1.0.0",
27-
"@softarc/native-federation-runtime": "1.0.0",
26+
"@softarc/native-federation": "1.1.0-beta.0",
27+
"@softarc/native-federation-runtime": "1.1.0-beta.0",
2828
"@rollup/plugin-json": "^4.1.0",
2929
"cross-spawn": "^7.0.3",
3030
"rollup-plugin-terser": "^7.0.2",

update-local.bat

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
call npm unpublish @softarc/native-federation@1.1.0-beta.0 --registry http://localhost:4873
22
call npm unpublish @softarc/native-federation-runtime@1.1.0-beta.0 --registry http://localhost:4873
33
call npm unpublish @softarc/native-federation-esbuild@1.1.0-beta.0 --registry http://localhost:4873
4-
REM call npm unpublish @angular-architects/native-federation@1.0.0-beta.1 --registry http://localhost:4873
4+
call npm unpublish @angular-architects/native-federation@1.1.0-beta.0 --registry http://localhost:4873
55

6-
REM call nx build native-federation
6+
call nx build native-federation
77
call nx build native-federation-core
88
call nx build native-federation-runtime
99
call nx build native-federation-esbuild
1010

1111
call npm publish dist\libs\native-federation-core --registry http://localhost:4873
12-
REM call npm publish dist\libs\native-federation --registry http://localhost:4873
12+
call npm publish dist\libs\native-federation --registry http://localhost:4873
1313
call npm publish dist\libs\native-federation-runtime --registry http://localhost:4873
1414
call npm publish dist\libs\native-federation-esbuild --registry http://localhost:4873

0 commit comments

Comments
 (0)