Skip to content

Commit 8867bf2

Browse files
committed
fix(@schematics/angular): update the PostCSS configuration used by the build in tailwind
The tailwind schematic searched the workspace root before the project root and `.postcssrc.json` before `postcss.config.json`, the opposite of the build system. With a configuration in both locations, the schematic added `@tailwindcss/postcss` to a file the build never reads, so the build succeeded but Tailwind CSS utilities were not generated. Search the project root first and `postcss.config.json` first, matching the build system.
1 parent 1651ea0 commit 8867bf2

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

‎packages/schematics/angular/tailwind/index.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ import { createProjectSchematic } from '../utility/project';
3434
import { Schema as TailwindOptions } from './schema';
3535

3636
const TAILWIND_DEPENDENCIES = ['tailwindcss', '@tailwindcss/postcss', 'postcss'];
37-
const POSTCSS_CONFIG_FILES = ['.postcssrc.json', 'postcss.config.json'];
37+
// Same lookup order as the build system, so the configuration that gets updated is the one used.
38+
const POSTCSS_CONFIG_FILES = ['postcss.config.json', '.postcssrc.json'];
3839

3940
function addTailwindStyles(options: { project: string }, project: ProjectDefinition): Rule {
4041
return async (tree) => {
@@ -94,7 +95,7 @@ function addTailwindStyles(options: { project: string }, project: ProjectDefinit
9495

9596
function managePostCssConfiguration(project: ProjectDefinition): Rule {
9697
return async (tree) => {
97-
const searchPaths = ['/', project.root]; // Workspace root and project root
98+
const searchPaths = [project.root, '/']; // Project root and workspace root
9899

99100
for (const path of searchPaths) {
100101
for (const configFile of POSTCSS_CONFIG_FILES) {

‎packages/schematics/angular/tailwind/index_spec.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,28 @@ describe('Tailwind Schematic', () => {
128128
expect(postCssConfig.plugins['autoprefixer']).toBeDefined();
129129
expect(tree.exists('/projects/bar/.postcssrc.json')).toBe(false);
130130
});
131+
132+
it('should prefer a project root configuration over a workspace root configuration', async () => {
133+
appTree.create('/.postcssrc.json', JSON.stringify({ plugins: {} }));
134+
appTree.create('/projects/bar/postcss.config.json', JSON.stringify({ plugins: {} }));
135+
const tree = await schematicRunner.runSchematic('tailwind', { project: 'bar' }, appTree);
136+
137+
const projectConfig = JSON.parse(tree.readContent('/projects/bar/postcss.config.json'));
138+
expect(projectConfig.plugins['@tailwindcss/postcss']).toBeDefined();
139+
const workspaceConfig = JSON.parse(tree.readContent('/.postcssrc.json'));
140+
expect(workspaceConfig.plugins['@tailwindcss/postcss']).toBeUndefined();
141+
});
142+
143+
it('should prefer postcss.config.json over .postcssrc.json in the same directory', async () => {
144+
appTree.create('/projects/bar/.postcssrc.json', JSON.stringify({ plugins: {} }));
145+
appTree.create('/projects/bar/postcss.config.json', JSON.stringify({ plugins: {} }));
146+
const tree = await schematicRunner.runSchematic('tailwind', { project: 'bar' }, appTree);
147+
148+
const config = JSON.parse(tree.readContent('/projects/bar/postcss.config.json'));
149+
expect(config.plugins['@tailwindcss/postcss']).toBeDefined();
150+
const rcConfig = JSON.parse(tree.readContent('/projects/bar/.postcssrc.json'));
151+
expect(rcConfig.plugins['@tailwindcss/postcss']).toBeUndefined();
152+
});
131153
});
132154

133155
describe('with non-workspace stylesheets', () => {

0 commit comments

Comments
 (0)