Skip to content

Commit 349ef18

Browse files
committed
fixup! feat(@schematics/angular): add use-library-builder migration to migrate from ng-packagr
1 parent 178b3d1 commit 349ef18

2 files changed

Lines changed: 60 additions & 10 deletions

File tree

‎packages/schematics/angular/migrations/use-library-builder/migration.ts‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
*/
88

99
import { type JsonObject, type JsonValue, isJsonObject } from '@angular-devkit/core';
10-
import { type Rule, type SchematicContext, type Tree, chain } from '@angular-devkit/schematics';
10+
import {
11+
type Rule,
12+
type SchematicContext,
13+
SchematicsException,
14+
type Tree,
15+
chain,
16+
} from '@angular-devkit/schematics';
1117
import { dirname, join, normalize, relative } from 'node:path/posix';
1218
import {
1319
DependencyType,
@@ -100,7 +106,7 @@ function parseNgPackageFile(tree: Tree, filePath: string): NgPackageConfig | und
100106
/**
101107
* Resolves the TypeScript entry file path for a library entry point directory.
102108
* Uses the explicitly configured `entryFile` when provided, or checks common
103-
* default locations before falling back to `'src/public-api.ts'`.
109+
* default locations.
104110
*
105111
* @param tree The virtual file system tree.
106112
* @param dir The directory of the primary or secondary entry point.
@@ -127,7 +133,9 @@ function resolveEntryFile(tree: Tree, dir: string, configuredEntry?: string): st
127133
}
128134
}
129135

130-
return 'src/public-api.ts';
136+
throw new SchematicsException(
137+
`Could not find an entry file for library entry point at "${dir || '.'}".`,
138+
);
131139
}
132140

133141
/**
@@ -150,12 +158,7 @@ function findSecondaryEntryPoints(
150158
const rootDir = tree.getDir(normalizedRoot);
151159
const directories = [rootDir];
152160

153-
while (directories.length > 0) {
154-
const current = directories.shift();
155-
if (!current) {
156-
break;
157-
}
158-
161+
for (const current of directories) {
159162
const currentPath = normalizeRelativePath(current.path);
160163

161164
if (currentPath !== normalizedRoot) {
@@ -270,9 +273,16 @@ function migrateTargetOptions(
270273
: {};
271274

272275
if (ngPackageConfig.lib.styleIncludePaths?.length) {
273-
existingPreprocessorOptions['includePaths'] = ngPackageConfig.lib.styleIncludePaths.map((p) =>
276+
const existingIncludePaths = Array.isArray(existingPreprocessorOptions['includePaths'])
277+
? (existingPreprocessorOptions['includePaths'] as string[])
278+
: [];
279+
const migratedIncludePaths = ngPackageConfig.lib.styleIncludePaths.map((p) =>
274280
join(projectDir, p),
275281
);
282+
existingPreprocessorOptions['includePaths'] = [
283+
...existingIncludePaths,
284+
...migratedIncludePaths,
285+
];
276286
}
277287
if (ngPackageConfig.lib.sass) {
278288
existingPreprocessorOptions['sass'] = ngPackageConfig.lib.sass;

‎packages/schematics/angular/migrations/use-library-builder/migration_spec.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,46 @@ describe('Migration to use the library builder', () => {
324324
});
325325
});
326326

327+
it('should preserve existing stylePreprocessorOptions.includePaths when migrating styleIncludePaths', async () => {
328+
const angularJson = JSON.parse(tree.readContent('/angular.json'));
329+
angularJson.projects['my-lib'].architect.build.options.stylePreprocessorOptions = {
330+
includePaths: ['existing/styles'],
331+
};
332+
tree.overwrite('/angular.json', JSON.stringify(angularJson, undefined, 2));
333+
334+
tree.overwrite(
335+
'/projects/my-lib/ng-package.json',
336+
JSON.stringify(
337+
{
338+
lib: {
339+
entryFile: 'src/public-api.ts',
340+
styleIncludePaths: ['../theme'],
341+
},
342+
},
343+
undefined,
344+
2,
345+
),
346+
);
347+
348+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
349+
const {
350+
projects: { 'my-lib': lib },
351+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
352+
} = newTree.readJson('/angular.json') as any;
353+
354+
expect(lib.architect['build'].options.stylePreprocessorOptions).toEqual({
355+
includePaths: ['existing/styles', 'projects/theme'],
356+
});
357+
});
358+
359+
it('should throw an error when entry file is not configured and no default candidate exists', async () => {
360+
tree.overwrite('/projects/my-lib/ng-package.json', JSON.stringify({}, undefined, 2));
361+
362+
await expectAsync(schematicRunner.runSchematic(schematicName, {}, tree)).toBeRejectedWithError(
363+
'Could not find an entry file for library entry point at "projects/my-lib".',
364+
);
365+
});
366+
327367
it('should migrate package.json with ngPackage configuration', async () => {
328368
tree.overwrite(
329369
'/projects/my-lib/package.json',

0 commit comments

Comments
 (0)