|
7 | 7 | */
|
8 | 8 |
|
9 | 9 | import { BuilderContext } from '@angular-devkit/architect';
|
10 |
| -import * as path from 'path'; |
| 10 | +import fs from 'node:fs'; |
| 11 | +import { createRequire } from 'node:module'; |
| 12 | +import path from 'node:path'; |
11 | 13 | import { normalizeAssetPatterns, normalizeOptimization, normalizeSourceMaps } from '../../utils';
|
12 | 14 | import { normalizeCacheOptions } from '../../utils/normalize-cache';
|
13 | 15 | import { normalizePolyfills } from '../../utils/normalize-polyfills';
|
@@ -100,6 +102,25 @@ export async function normalizeOptions(
|
100 | 102 | }
|
101 | 103 | }
|
102 | 104 |
|
| 105 | + let tailwindConfiguration: { file: string; package: string } | undefined; |
| 106 | + const tailwindConfigurationPath = findTailwindConfigurationFile(workspaceRoot, projectRoot); |
| 107 | + if (tailwindConfigurationPath) { |
| 108 | + const resolver = createRequire(projectRoot); |
| 109 | + try { |
| 110 | + tailwindConfiguration = { |
| 111 | + file: tailwindConfigurationPath, |
| 112 | + package: resolver.resolve('tailwindcss'), |
| 113 | + }; |
| 114 | + } catch { |
| 115 | + const relativeTailwindConfigPath = path.relative(workspaceRoot, tailwindConfigurationPath); |
| 116 | + context.logger.warn( |
| 117 | + `Tailwind CSS configuration file found (${relativeTailwindConfigPath})` + |
| 118 | + ` but the 'tailwindcss' package is not installed.` + |
| 119 | + ` To enable Tailwind CSS, please install the 'tailwindcss' package.`, |
| 120 | + ); |
| 121 | + } |
| 122 | + } |
| 123 | + |
103 | 124 | let serviceWorkerOptions;
|
104 | 125 | if (options.serviceWorker) {
|
105 | 126 | // If ngswConfigPath is not specified, the default is 'ngsw-config.json' within the project root
|
@@ -181,5 +202,27 @@ export async function normalizeOptions(
|
181 | 202 | globalStyles,
|
182 | 203 | serviceWorkerOptions,
|
183 | 204 | indexHtmlOptions,
|
| 205 | + tailwindConfiguration, |
184 | 206 | };
|
185 | 207 | }
|
| 208 | + |
| 209 | +function findTailwindConfigurationFile( |
| 210 | + workspaceRoot: string, |
| 211 | + projectRoot: string, |
| 212 | +): string | undefined { |
| 213 | + // A configuration file can exist in the project or workspace root |
| 214 | + // The list of valid config files can be found: |
| 215 | + // https://github.com/tailwindlabs/tailwindcss/blob/8845d112fb62d79815b50b3bae80c317450b8b92/src/util/resolveConfigPath.js#L46-L52 |
| 216 | + const tailwindConfigFiles = ['tailwind.config.js', 'tailwind.config.cjs']; |
| 217 | + for (const basePath of [projectRoot, workspaceRoot]) { |
| 218 | + for (const configFile of tailwindConfigFiles) { |
| 219 | + // Project level configuration should always take precedence. |
| 220 | + const fullPath = path.join(basePath, configFile); |
| 221 | + if (fs.existsSync(fullPath)) { |
| 222 | + return fullPath; |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + return undefined; |
| 228 | +} |
0 commit comments