|
6 | 6 | * found in the LICENSE file at https://angular.dev/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import { SassWorkerImplementation } from '@angular/build/private'; |
| 9 | +import { |
| 10 | + SassWorkerImplementation, |
| 11 | + findTailwindConfiguration, |
| 12 | + generateSearchDirectories, |
| 13 | + loadPostcssConfiguration, |
| 14 | +} from '@angular/build/private'; |
10 | 15 | import MiniCssExtractPlugin from 'mini-css-extract-plugin'; |
11 | 16 | import * as path from 'node:path'; |
12 | 17 | import { fileURLToPath, pathToFileURL } from 'node:url'; |
13 | 18 | import type { FileImporter } from 'sass'; |
14 | 19 | import type { Configuration, LoaderContext, RuleSetUseItem } from 'webpack'; |
15 | 20 | import { WebpackConfigOptions } from '../../../utils/build-options'; |
16 | | -import { findTailwindConfigurationFile } from '../../../utils/tailwind'; |
17 | 21 | import { |
18 | 22 | AnyComponentStyleBudgetChecker, |
19 | 23 | PostcssCliResources, |
@@ -75,25 +79,40 @@ export async function getStylesConfig(wco: WebpackConfigOptions): Promise<Config |
75 | 79 |
|
76 | 80 | const extraPostcssPlugins: import('postcss').Plugin[] = []; |
77 | 81 |
|
78 | | - // Attempt to setup Tailwind CSS |
79 | | - // Only load Tailwind CSS plugin if configuration file was found. |
80 | | - // This acts as a guard to ensure the project actually wants to use Tailwind CSS. |
81 | | - // The package may be unknowningly present due to a third-party transitive package dependency. |
82 | | - const tailwindConfigPath = await findTailwindConfigurationFile(root, projectRoot); |
83 | | - if (tailwindConfigPath) { |
84 | | - let tailwindPackagePath; |
85 | | - try { |
86 | | - tailwindPackagePath = require.resolve('tailwindcss', { paths: [root] }); |
87 | | - } catch { |
88 | | - const relativeTailwindConfigPath = path.relative(root, tailwindConfigPath); |
89 | | - logger.warn( |
90 | | - `Tailwind CSS configuration file found (${relativeTailwindConfigPath})` + |
91 | | - ` but the 'tailwindcss' package is not installed.` + |
92 | | - ` To enable Tailwind CSS, please install the 'tailwindcss' package.`, |
93 | | - ); |
| 82 | + const searchDirectories = await generateSearchDirectories([projectRoot, root]); |
| 83 | + const postcssConfig = await loadPostcssConfiguration(searchDirectories); |
| 84 | + |
| 85 | + if (postcssConfig) { |
| 86 | + for (const [pluginName, pluginOptions] of postcssConfig.plugins) { |
| 87 | + const resolvedPlugin = require.resolve(pluginName, { paths: [root] }); |
| 88 | + const { default: plugin } = await import(resolvedPlugin); |
| 89 | + if (typeof plugin !== 'function' || plugin.postcss !== true) { |
| 90 | + throw new Error(`Attempted to load invalid Postcss plugin: "${pluginName}"`); |
| 91 | + } |
| 92 | + |
| 93 | + extraPostcssPlugins.push(plugin(pluginOptions)); |
94 | 94 | } |
95 | | - if (tailwindPackagePath) { |
96 | | - extraPostcssPlugins.push(require(tailwindPackagePath)({ config: tailwindConfigPath })); |
| 95 | + } else { |
| 96 | + // Attempt to setup Tailwind CSS |
| 97 | + // Only load Tailwind CSS plugin if configuration file was found. |
| 98 | + // This acts as a guard to ensure the project actually wants to use Tailwind CSS. |
| 99 | + // The package may be unknowningly present due to a third-party transitive package dependency. |
| 100 | + const tailwindConfigPath = findTailwindConfiguration(searchDirectories); |
| 101 | + if (tailwindConfigPath) { |
| 102 | + let tailwindPackagePath; |
| 103 | + try { |
| 104 | + tailwindPackagePath = require.resolve('tailwindcss', { paths: [root] }); |
| 105 | + } catch { |
| 106 | + const relativeTailwindConfigPath = path.relative(root, tailwindConfigPath); |
| 107 | + logger.warn( |
| 108 | + `Tailwind CSS configuration file found (${relativeTailwindConfigPath})` + |
| 109 | + ` but the 'tailwindcss' package is not installed.` + |
| 110 | + ` To enable Tailwind CSS, please install the 'tailwindcss' package.`, |
| 111 | + ); |
| 112 | + } |
| 113 | + if (tailwindPackagePath) { |
| 114 | + extraPostcssPlugins.push(require(tailwindPackagePath)({ config: tailwindConfigPath })); |
| 115 | + } |
97 | 116 | } |
98 | 117 | } |
99 | 118 |
|
|
0 commit comments