Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,14 @@ function resolveEntryFile(tree: Tree, dir: string, configuredEntry?: string): st
* @param tree The virtual file system tree.
* @param projectRoot The root directory of the library project.
* @param primaryProjectFile The workspace-relative path to the primary `ng-packagr` configuration file.
* @param projectRoots The normalized root directories of all workspace projects.
* @returns An array of discovered secondary entry points.
*/
function findSecondaryEntryPoints(
tree: Tree,
projectRoot: string,
primaryProjectFile: string,
projectRoots: ReadonlySet<string>,
): SecondaryEntryPoint[] {
const secondaryEntryPoints: SecondaryEntryPoint[] = [];
const normalizedRoot = normalizeRelativePath(projectRoot);
Expand Down Expand Up @@ -202,7 +204,9 @@ function findSecondaryEntryPoints(
subdir === 'node_modules' ||
subdir === 'dist' ||
subdir === 'out-tsc' ||
subdir.startsWith('.')
subdir.startsWith('.') ||
// Other projects nested in the directory, such as libraries of a workspace root library.
projectRoots.has(join(currentPath, subdir))
) {
continue;
}
Expand Down Expand Up @@ -370,13 +374,15 @@ function migrateTargetOptions(
* @param buildTarget The library's `build` target definition in `angular.json`.
* @param tree The virtual file system tree.
* @param context The schematic execution context.
* @param projectRoots The normalized root directories of all workspace projects.
* @returns `true` if the library target was migrated, or `false` if it was skipped.
*/
function updateLibraryTarget(
projectRoot: string,
buildTarget: TargetDefinition,
tree: Tree,
context: SchematicContext,
projectRoots: ReadonlySet<string>,
): boolean {
const normalizedProjectRoot = normalizeRelativePath(projectRoot);
let primaryProjectFile =
Expand Down Expand Up @@ -423,6 +429,7 @@ function updateLibraryTarget(
tree,
normalizedProjectRoot,
primaryProjectFile,
projectRoots,
);

const toRelativeExportPath = (filePath: string): string => {
Expand Down Expand Up @@ -588,6 +595,9 @@ export default function (): Rule {
return (tree, context) =>
updateWorkspace((workspace) => {
let hasMigratedLibrary = false;
const projectRoots = new Set(
Array.from(workspace.projects.values(), (project) => normalizeRelativePath(project.root)),
);

for (const [, project] of workspace.projects) {
if (project.extensions.projectType !== ProjectType.Library) {
Expand All @@ -604,7 +614,7 @@ export default function (): Rule {
continue;
}

if (updateLibraryTarget(project.root, buildTarget, tree, context)) {
if (updateLibraryTarget(project.root, buildTarget, tree, context, projectRoots)) {
hasMigratedLibrary = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,35 @@ describe('Migration to use the library builder', () => {
});
});

it('should not treat other libraries as secondary entry points of a workspace root library', async () => {
const angularJson = JSON.parse(tree.readContent('/angular.json'));
angularJson.projects = {
'root-lib': {
root: '',
sourceRoot: 'src',
projectType: ProjectType.Library,
architect: {
build: {
builder: Builders.NgPackagr,
options: { project: 'ng-package.json' },
},
},
},
...angularJson.projects,
};
tree.overwrite('/angular.json', JSON.stringify(angularJson));
tree.create('/ng-package.json', JSON.stringify({ lib: { entryFile: 'src/public-api.ts' } }));
tree.create('/projects/my-lib/src/public-api.ts', '');

const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);

const rootPkg = newTree.readJson('/package.json') as { exports: unknown };
expect(rootPkg.exports).toEqual({ '.': './src/public-api.ts' });
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { projects } = newTree.readJson('/angular.json') as any;
expect(projects['my-lib'].architect['build'].options.outputPath).toBe('dist/my-lib');
});

it('should migrate assets, styleIncludePaths, and sass options from ng-package.json', async () => {
tree.create('/projects/my-lib/CHANGELOG.md', '# Changelog');
tree.overwrite(
Expand Down
Loading