Skip to content

Commit 9732721

Browse files
authored
fix(@schematics/angular): migrate ng-packagr glob string assets in use-library-builder
ng-packagr treats an `assets` string that is neither a file nor a directory as a glob relative to the package root. The migration copied every string as a path, and the library builder treats such a path as a directory, so glob assets like `docs/**/*.md` copied nothing after the migration.
1 parent 97efdcf commit 9732721

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,23 @@ function migrateTargetOptions(
251251
: [];
252252
const migratedAssets = ngPackageConfig.assets.map((asset) => {
253253
if (typeof asset === 'string') {
254-
return join(projectDir, asset);
254+
const assetPath = join(projectDir, asset);
255+
if (assetPath.startsWith('..')) {
256+
return assetPath;
257+
}
258+
259+
if (tree.exists(assetPath)) {
260+
return assetPath;
261+
}
262+
263+
const assetDir = tree.getDir(assetPath);
264+
if (assetDir.subfiles.length || assetDir.subdirs.length) {
265+
return assetPath;
266+
}
267+
268+
// `ng-packagr` treats a string that is neither a file nor a directory as a glob relative
269+
// to the package root, while the library builder only accepts files and directories.
270+
return { glob: asset, input: projectDir };
255271
}
256272
if (isJsonObject(asset) && typeof asset['input'] === 'string') {
257273
return {

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ describe('Migration to use the library builder', () => {
281281
});
282282

283283
it('should migrate assets, styleIncludePaths, and sass options from ng-package.json', async () => {
284+
tree.create('/projects/my-lib/CHANGELOG.md', '# Changelog');
284285
tree.overwrite(
285286
'/projects/my-lib/ng-package.json',
286287
JSON.stringify(
@@ -324,6 +325,48 @@ describe('Migration to use the library builder', () => {
324325
});
325326
});
326327

328+
it('should migrate ng-packagr glob string assets to glob objects', async () => {
329+
tree.create('/projects/my-lib/docs/guide.md', '# Guide');
330+
tree.create('/projects/my-lib/CHANGELOG.md', '# Changelog');
331+
tree.overwrite(
332+
'/projects/my-lib/ng-package.json',
333+
JSON.stringify({
334+
assets: ['CHANGELOG.md', 'docs', 'docs/**/*.md'],
335+
lib: { entryFile: 'src/public-api.ts' },
336+
}),
337+
);
338+
339+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
340+
const {
341+
projects: { 'my-lib': lib },
342+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
343+
} = newTree.readJson('/angular.json') as any;
344+
345+
expect(lib.architect['build'].options.assets).toEqual([
346+
'projects/my-lib/CHANGELOG.md',
347+
'projects/my-lib/docs',
348+
{ glob: 'docs/**/*.md', input: 'projects/my-lib' },
349+
]);
350+
});
351+
352+
it('should keep ng-packagr assets outside the project root as paths', async () => {
353+
tree.overwrite(
354+
'/projects/my-lib/ng-package.json',
355+
JSON.stringify({
356+
assets: ['../../../shared-assets'],
357+
lib: { entryFile: 'src/public-api.ts' },
358+
}),
359+
);
360+
361+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
362+
const {
363+
projects: { 'my-lib': lib },
364+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
365+
} = newTree.readJson('/angular.json') as any;
366+
367+
expect(lib.architect['build'].options.assets).toEqual(['../shared-assets']);
368+
});
369+
327370
it('should preserve existing stylePreprocessorOptions.includePaths when migrating styleIncludePaths', async () => {
328371
const angularJson = JSON.parse(tree.readContent('/angular.json'));
329372
angularJson.projects['my-lib'].architect.build.options.stylePreprocessorOptions = {

0 commit comments

Comments
 (0)