Skip to content

Commit

Permalink
fix(angular): support secondary entry points nrwl#10329
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Jun 7, 2022
1 parent f828da9 commit 9d265f6
Showing 1 changed file with 73 additions and 2 deletions.
75 changes: 73 additions & 2 deletions packages/angular/src/utils/mfe/mfe-webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,65 @@ export interface SharedLibraryConfig {
eager?: boolean;
}

function traverseUpFileTreeAndPerformActionUntil(
dirPath: string,
endPath: string,
action: (dirname: string) => boolean
) {
while (dirPath !== endPath) {
if (action(dirPath)) {
break;
}
dirPath = dirname(dirPath);
}
}

function collectWorkspaceLibrarySecondaryEntryPoints(
library: string,
libraryPath: string,
tsconfigPathAliases: Record<string, string[]>
) {
let libraryRootDirPath = libraryPath;
traverseUpFileTreeAndPerformActionUntil(
dirname(libraryRootDirPath),
workspaceRoot,
(currentDirPath) => {
if (existsSync(join(currentDirPath, 'package.json'))) {
libraryRootDirPath = currentDirPath;
return true;
}
}
);

const aliasesUnderLibrary = Object.keys(tsconfigPathAliases).filter(
(libName) => libName.startsWith(library) && libName !== library
);
const secondaryEntryPoints = [];
for (const alias of aliasesUnderLibrary) {
const pathToLib = dirname(
join(workspaceRoot, tsconfigPathAliases[alias][0])
);
let isSecondaryEntrypoint = false;
let searchDir = pathToLib;
traverseUpFileTreeAndPerformActionUntil(
searchDir,
libraryRootDirPath,
(currentDirPath) => {
if (existsSync(join(currentDirPath, 'ng-package.json'))) {
isSecondaryEntrypoint = true;
return true;
}
}
);

if (isSecondaryEntrypoint) {
secondaryEntryPoints.push({ name: alias, path: pathToLib });
}
}

return secondaryEntryPoints;
}

export function shareWorkspaceLibraries(
libraries: string[],
tsConfigPath = process.env.NX_TSCONFIG_PATH ?? getRootTsConfigPath()
Expand All @@ -48,6 +107,17 @@ export function shareWorkspaceLibraries(
for (const [key, paths] of Object.entries(tsconfigPathAliases)) {
if (libraries && libraries.includes(key)) {
const pathToLib = normalize(join(workspaceRoot, paths[0]));
collectWorkspaceLibrarySecondaryEntryPoints(
key,
pathToLib,
tsconfigPathAliases
).forEach(({ name, path }) =>
pathMappings.push({
name,
path,
})
);

pathMappings.push({
name: key,
path: pathToLib,
Expand All @@ -61,14 +131,15 @@ export function shareWorkspaceLibraries(
(aliases, library) => ({ ...aliases, [library.name]: library.path }),
{}
),
getLibraries: (eager?: boolean): Record<string, SharedLibraryConfig> =>
pathMappings.reduce(
getLibraries: (eager?: boolean): Record<string, SharedLibraryConfig> => ({
...pathMappings.reduce(
(libraries, library) => ({
...libraries,
[library.name]: { requiredVersion: false, eager },
}),
{} as Record<string, SharedLibraryConfig>
),
}),
getReplacementPlugin: () =>
new NormalModuleReplacementPlugin(/./, (req) => {
if (!req.request.startsWith('.')) {
Expand Down

0 comments on commit 9d265f6

Please sign in to comment.