Skip to content

Commit ffb94ba

Browse files
committed
fix(@angular-devkit/build-angular): detect tailwind.config.cjs as valid tailwinds configuration
`tailwind.config.cjs` is a valid tailwindcss configuration file as it's listed in https://github.com/tailwindlabs/tailwindcss/blob/8845d112fb62d79815b50b3bae80c317450b8b92/src/util/resolveConfigPath.js#L46-L52 as such we should also take this filename into consideration. Closes angular#23236
1 parent e569d3b commit ffb94ba

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed

packages/angular_devkit/build_angular/src/webpack/configs/styles.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,10 @@ export function getStylesConfig(wco: WebpackConfigOptions): Configuration {
121121
const extraPostcssPlugins: import('postcss').Plugin[] = [];
122122

123123
// Attempt to setup Tailwind CSS
124-
// A configuration file can exist in the project or workspace root
125-
const tailwindConfigFile = 'tailwind.config.js';
126-
let tailwindConfigPath;
127-
for (const basePath of [wco.projectRoot, wco.root]) {
128-
const fullPath = path.join(basePath, tailwindConfigFile);
129-
if (fs.existsSync(fullPath)) {
130-
tailwindConfigPath = fullPath;
131-
break;
132-
}
133-
}
134124
// Only load Tailwind CSS plugin if configuration file was found.
135125
// This acts as a guard to ensure the project actually wants to use Tailwind CSS.
136126
// The package may be unknowningly present due to a third-party transitive package dependency.
127+
const tailwindConfigPath = getTailwindConfigPath(wco);
137128
if (tailwindConfigPath) {
138129
let tailwindPackagePath;
139130
try {
@@ -402,3 +393,21 @@ export function getStylesConfig(wco: WebpackConfigOptions): Configuration {
402393
plugins: extraPlugins,
403394
};
404395
}
396+
397+
function getTailwindConfigPath({ projectRoot, root }: WebpackConfigOptions): string | undefined {
398+
// A configuration file can exist in the project or workspace root
399+
// The list of valid config files can be found:
400+
// https://github.com/tailwindlabs/tailwindcss/blob/8845d112fb62d79815b50b3bae80c317450b8b92/src/util/resolveConfigPath.js#L46-L52
401+
const tailwindConfigFiles = ['tailwind.config.js', 'tailwind.config.cjs'];
402+
for (const basePath of [projectRoot, root]) {
403+
for (const configFile of tailwindConfigFiles) {
404+
// Irrespective of the name project level configuration should always take precedence.
405+
const fullPath = path.join(basePath, configFile);
406+
if (fs.existsSync(fullPath)) {
407+
return fullPath;
408+
}
409+
}
410+
}
411+
412+
return undefined;
413+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { expectFileToMatch, moveFile, writeFile } from '../../../utils/fs';
2+
import { installPackage } from '../../../utils/packages';
3+
import { ng, silentExec } from '../../../utils/process';
4+
import { updateJsonFile } from '../../../utils/project';
5+
import { expectToFail } from '../../../utils/utils';
6+
7+
export default async function () {
8+
// Temporarily turn off caching until the build cache accounts for the presence of tailwind
9+
// and its configuration file. Otherwise cached builds without tailwind will cause test failures.
10+
await ng('cache', 'off');
11+
12+
// Add type module in package.json.
13+
await updateJsonFile('package.json', (json) => {
14+
json['type'] = 'module';
15+
});
16+
17+
// Install Tailwind
18+
await installPackage('tailwindcss@3');
19+
20+
// Create configuration file
21+
await silentExec('npx', 'tailwindcss', 'init');
22+
23+
// tailwind doesn't create the config file with a cjs extension.
24+
// We need to rename it manually until
25+
// https://github.com/tailwindlabs/tailwindcss/commit/6c63f67d20c433b5c7281e9e26744038ed41ccc2 is released.
26+
await moveFile('tailwind.config.js', 'tailwind.config.cjs');
27+
28+
// Add Tailwind directives to a global style
29+
await writeFile('src/styles.css', '@tailwind base; @tailwind components;');
30+
31+
// Build should succeed and process Tailwind directives
32+
await ng('build', '--configuration=development');
33+
34+
// Check for Tailwind output
35+
await expectFileToMatch('dist/test-project/styles.css', /::placeholder/);
36+
await expectToFail(() =>
37+
expectFileToMatch('dist/test-project/styles.css', '@tailwind base; @tailwind components;'),
38+
);
39+
}

0 commit comments

Comments
 (0)