Skip to content

Commit 8d1e2ac

Browse files
committed
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. Migrate strings that are neither a file nor a directory to `{ glob, input, output }` objects, as ng-packagr resolves them.
1 parent 0fd4404 commit 8d1e2ac

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,19 @@ 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 (tree.exists(assetPath)) {
256+
return assetPath;
257+
}
258+
259+
const assetDir = tree.getDir(assetPath);
260+
if (assetDir.subfiles.length || assetDir.subdirs.length) {
261+
return assetPath;
262+
}
263+
264+
// `ng-packagr` treats a string that is neither a file nor a directory as a glob relative
265+
// to the package root, while the library builder only accepts files and directories.
266+
return { glob: normalize(asset), input: join(projectDir, '.'), output: '/' };
255267
}
256268
if (isJsonObject(asset) && typeof asset['input'] === 'string') {
257269
return {

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

Lines changed: 25 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,30 @@ 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', output: '/' },
349+
]);
350+
});
351+
327352
it('should preserve existing stylePreprocessorOptions.includePaths when migrating styleIncludePaths', async () => {
328353
const angularJson = JSON.parse(tree.readContent('/angular.json'));
329354
angularJson.projects['my-lib'].architect.build.options.stylePreprocessorOptions = {

0 commit comments

Comments
 (0)