Skip to content

Commit 511d876

Browse files
committed
fix(@schematics/angular): add postcss dependency in application migration if needed
The application migration schematic will now attempt to detect the usage of custom postcss plugins within a workspace and install the `postcss` dependency if required. This will only occur if the migration analysis allows for the conversion to use the `@angular/build` package instead of the `@angular-devkit/build-angular` package. Custom postcss configurations will be detected within the same locations as the build system itself which includes the workspace root and any project root for the `postcss.config.json` and `.postcssrc.json` files.
1 parent df82da5 commit 511d876

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

packages/schematics/angular/migrations/use-application-builder/migration.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { JSONFile } from '../../utility/json-file';
2727
import { latestVersions } from '../../utility/latest-versions';
2828
import {
2929
TargetDefinition,
30+
WorkspaceDefinition,
3031
allTargetOptions,
3132
allWorkspaceTargets,
3233
updateWorkspace,
@@ -265,6 +266,18 @@ function updateProjects(tree: Tree, context: SchematicContext) {
265266
}),
266267
);
267268
}
269+
270+
// Add postcss dependency if any projects have a custom postcss configuration file.
271+
// The build system only supports files in a project root or workspace root with
272+
// names of either 'postcss.config.json' or '.postcssrc.json'.
273+
if (hasPostcssConfiguration(tree, workspace)) {
274+
rules.push(
275+
addDependency('postcss', latestVersions['postcss'], {
276+
type: DependencyType.Dev,
277+
existing: ExistingBehavior.Replace,
278+
}),
279+
);
280+
}
268281
}
269282

270283
return chain(rules);
@@ -297,6 +310,31 @@ function hasLessStylesheets(tree: Tree) {
297310
}
298311
}
299312

313+
/**
314+
* Searches for a Postcss configuration file within the workspace root
315+
* or any of the project roots.
316+
*
317+
* @param tree A Schematics tree instance to search
318+
* @param workspace A Workspace to check for projects
319+
* @returns true, if a Postcss configuration file is found; otherwise, false
320+
*/
321+
function hasPostcssConfiguration(tree: Tree, workspace: WorkspaceDefinition) {
322+
// Add workspace root
323+
const searchDirectories = [''];
324+
325+
// Add each project root
326+
for (const { root } of workspace.projects.values()) {
327+
if (root) {
328+
searchDirectories.push(root);
329+
}
330+
}
331+
332+
return searchDirectories.some(
333+
(dir) =>
334+
tree.exists(join(dir, 'postcss.config.json')) || tree.exists(join(dir, '.postcssrc.json')),
335+
);
336+
}
337+
300338
function* visit(
301339
directory: DirEntry,
302340
): IterableIterator<[fileName: string, contents: string, sass: boolean]> {

packages/schematics/angular/migrations/use-application-builder/migration_spec.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,49 @@ describe(`Migration to use the application builder`, () => {
278278
expect(devDependencies['less']).toBeDefined();
279279
});
280280

281-
it('should not add "less" dependency when converting to "@angular/build" and a ".less" file is present', async () => {
281+
it('should not add "less" dependency when converting to "@angular/build" and a ".less" file is not present', async () => {
282282
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
283283

284284
const { devDependencies } = JSON.parse(newTree.readContent('/package.json'));
285285

286286
expect(devDependencies['less']).toBeUndefined();
287287
});
288+
289+
it('should add "postcss" dependency when converting to "@angular/build" and postcss.config.json is present', async () => {
290+
tree.create('postcss.config.json', '{}');
291+
292+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
293+
294+
const { devDependencies } = JSON.parse(newTree.readContent('/package.json'));
295+
296+
expect(devDependencies['postcss']).toBeDefined();
297+
});
298+
299+
it('should add "postcss" dependency when converting to "@angular/build" and .postcssrc.json is present', async () => {
300+
tree.create('.postcssrc.json', '{}');
301+
302+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
303+
304+
const { devDependencies } = JSON.parse(newTree.readContent('/package.json'));
305+
306+
expect(devDependencies['postcss']).toBeDefined();
307+
});
308+
309+
it('should add "postcss" dependency when converting to "@angular/build" and .postcssrc.json is present in project', async () => {
310+
tree.create('/project/app/.postcssrc.json', '{}');
311+
312+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
313+
314+
const { devDependencies } = JSON.parse(newTree.readContent('/package.json'));
315+
316+
expect(devDependencies['postcss']).toBeDefined();
317+
});
318+
319+
it('should not add "postcss" dependency when converting to "@angular/build" and a Postcss configuration is not present', async () => {
320+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
321+
322+
const { devDependencies } = JSON.parse(newTree.readContent('/package.json'));
323+
324+
expect(devDependencies['postcss']).toBeUndefined();
325+
});
288326
});

packages/schematics/angular/utility/latest-versions/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"karma": "~6.4.0",
1919
"less": "^4.2.0",
2020
"ng-packagr": "^18.0.0-next.0",
21+
"postcss": "^8.4.38",
2122
"protractor": "~7.0.0",
2223
"rxjs": "~7.8.0",
2324
"tslib": "^2.3.0",

0 commit comments

Comments
 (0)