Skip to content

Commit 9674f74

Browse files
fixup! fix(@schematics/angular): migrate secondary entry point style options in use-library-builder
Co-authored-by: Alan Agius <alanagius@google.com>
1 parent cc0b0ea commit 9674f74

2 files changed

Lines changed: 71 additions & 32 deletions

File tree

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

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,13 @@ function findSecondaryEntryPoints(
220220
* @param projectDir The directory containing the primary `ng-packagr` configuration file.
221221
* @param buildTarget The library's `build` target definition in `angular.json`.
222222
* @param ngPackageConfig The parsed `NgPackageConfig`, if available.
223-
* @param secondaryEntryPoints The discovered secondary entry points of the library.
224223
* @param tree The virtual file system tree.
225224
*/
226225
function migrateTargetOptions(
227226
projectRoot: string,
228227
projectDir: string,
229228
buildTarget: TargetDefinition,
230229
ngPackageConfig: NgPackageConfig | undefined,
231-
secondaryEntryPoints: SecondaryEntryPoint[],
232230
tree: Tree,
233231
): void {
234232
buildTarget.builder = Builders.BuildLibrary;
@@ -286,40 +284,31 @@ function migrateTargetOptions(
286284
buildTarget.options['assets'] = [...existingAssets, ...migratedAssets];
287285
}
288286

289-
// The library builder applies style options to all entry points, so the options of
290-
// secondary entry points are merged with the primary ones.
291-
const styleConfigs = [
292-
{ dir: projectDir, lib: ngPackageConfig?.lib },
293-
...secondaryEntryPoints.map(({ configFile, config }) => ({
294-
dir: dirname(configFile),
295-
lib: config?.lib,
296-
})),
297-
];
298-
const migratedIncludePaths = styleConfigs.flatMap(({ dir, lib }) =>
299-
(lib?.styleIncludePaths ?? []).map((p) => join(dir, p)),
300-
);
301-
const migratedSassOptions = styleConfigs.flatMap(({ lib }) => (lib?.sass ? [lib.sass] : []));
302-
303-
if (migratedIncludePaths.length || migratedSassOptions.length) {
287+
if (ngPackageConfig?.lib?.styleIncludePaths?.length || ngPackageConfig?.lib?.sass) {
304288
const stylePreprocessorOptions = buildTarget.options['stylePreprocessorOptions'];
305289
const existingPreprocessorOptions: JsonObject =
306290
stylePreprocessorOptions && isJsonObject(stylePreprocessorOptions)
307291
? { ...stylePreprocessorOptions }
308292
: {};
309293

310-
if (migratedIncludePaths.length) {
294+
if (ngPackageConfig.lib.styleIncludePaths?.length) {
311295
const existingIncludePaths = Array.isArray(existingPreprocessorOptions['includePaths'])
312296
? (existingPreprocessorOptions['includePaths'] as string[])
313297
: [];
298+
const migratedIncludePaths = ngPackageConfig.lib.styleIncludePaths.map((p) =>
299+
join(projectDir, p),
300+
);
314301
existingPreprocessorOptions['includePaths'] = [
315302
...new Set([...existingIncludePaths, ...migratedIncludePaths]),
316303
];
317304
}
318-
if (migratedSassOptions.length) {
319-
const sass: Record<string, string[]> = {};
320-
for (const options of migratedSassOptions) {
321-
for (const [key, value] of Object.entries(options)) {
322-
sass[key] = [...new Set([...(sass[key] ?? []), ...(value ?? [])])];
305+
if (ngPackageConfig.lib.sass) {
306+
const existingSass = existingPreprocessorOptions['sass'];
307+
const sass: JsonObject = isJsonObject(existingSass) ? { ...existingSass } : {};
308+
for (const [key, value] of Object.entries(ngPackageConfig.lib.sass)) {
309+
if (Array.isArray(value)) {
310+
const existing = Array.isArray(sass[key]) ? (sass[key] as string[]) : [];
311+
sass[key] = [...new Set([...existing, ...value])];
323312
}
324313
}
325314
existingPreprocessorOptions['sass'] = sass;
@@ -421,7 +410,7 @@ function updateLibraryTarget(
421410
return false;
422411
}
423412

424-
const ngPackageConfig = primaryProjectFile
413+
let ngPackageConfig = primaryProjectFile
425414
? parseNgPackageFile(tree, primaryProjectFile)
426415
: undefined;
427416

@@ -451,16 +440,34 @@ function updateLibraryTarget(
451440
? secondary.subpath
452441
: `./${secondary.subpath}`;
453442
entryPointExports[exportKey] = toRelativeExportPath(secondary.entryPoint);
443+
444+
if (secondary.config?.lib) {
445+
ngPackageConfig ??= {};
446+
ngPackageConfig.lib ??= {};
447+
448+
if (secondary.config.lib.styleIncludePaths?.length) {
449+
const secondaryDir = dirname(secondary.configFile);
450+
ngPackageConfig.lib.styleIncludePaths ??= [];
451+
for (const includePath of secondary.config.lib.styleIncludePaths) {
452+
ngPackageConfig.lib.styleIncludePaths.push(
453+
relative(projectDir, join(secondaryDir, includePath)),
454+
);
455+
}
456+
}
457+
458+
if (secondary.config.lib.sass) {
459+
const sass: Record<string, unknown> = (ngPackageConfig.lib.sass ??= {});
460+
for (const [key, value] of Object.entries(secondary.config.lib.sass)) {
461+
if (Array.isArray(value)) {
462+
const existing = Array.isArray(sass[key]) ? (sass[key] as string[]) : [];
463+
sass[key] = [...new Set([...existing, ...value])];
464+
}
465+
}
466+
}
467+
}
454468
}
455469

456-
migrateTargetOptions(
457-
normalizedProjectRoot,
458-
projectDir,
459-
buildTarget,
460-
ngPackageConfig,
461-
secondaryEntryPoints,
462-
tree,
463-
);
470+
migrateTargetOptions(normalizedProjectRoot, projectDir, buildTarget, ngPackageConfig, tree);
464471

465472
const packageJsonPath = join(normalizedProjectRoot, 'package.json');
466473

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,38 @@ describe('Migration to use the library builder', () => {
399399
});
400400
});
401401

402+
it('should merge existing stylePreprocessorOptions.sass when migrating sass options', async () => {
403+
const angularJson = JSON.parse(tree.readContent('/angular.json'));
404+
angularJson.projects['my-lib'].architect.build.options.stylePreprocessorOptions = {
405+
sass: { silenceDeprecations: ['mixed-decls'] },
406+
};
407+
tree.overwrite('/angular.json', JSON.stringify(angularJson, undefined, 2));
408+
409+
tree.overwrite(
410+
'/projects/my-lib/ng-package.json',
411+
JSON.stringify(
412+
{
413+
lib: {
414+
entryFile: 'src/public-api.ts',
415+
sass: { silenceDeprecations: ['import'] },
416+
},
417+
},
418+
undefined,
419+
2,
420+
),
421+
);
422+
423+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
424+
const {
425+
projects: { 'my-lib': lib },
426+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
427+
} = newTree.readJson('/angular.json') as any;
428+
429+
expect(lib.architect['build'].options.stylePreprocessorOptions).toEqual({
430+
sass: { silenceDeprecations: ['mixed-decls', 'import'] },
431+
});
432+
});
433+
402434
it('should migrate styleIncludePaths and sass options from secondary entry points', async () => {
403435
tree.overwrite(
404436
'/projects/my-lib/ng-package.json',

0 commit comments

Comments
 (0)