| 37 | const POSTCSS_CONFIG_FILES = ['.postcssrc.json', 'postcss.config.json']; |
| 38 | |
| 39 | function addTailwindStyles(options: { project: string }, project: ProjectDefinition): Rule { |
| 40 | return async (tree) => { |
| 41 | const buildTarget = project.targets.get('build'); |
| 42 | |
| 43 | if (!buildTarget) { |
| 44 | throw new SchematicsException(`Project "${options.project}" does not have a build target.`); |
| 45 | } |
| 46 | |
| 47 | const styles = buildTarget.options?.['styles'] as (string | { input: string })[] | undefined; |
| 48 | |
| 49 | let stylesheetPath: string | undefined; |
| 50 | if (styles) { |
| 51 | stylesheetPath = styles |
| 52 | .map((s) => (typeof s === 'string' ? s : s.input)) |
| 53 | .find((p) => p.endsWith('.css')); |
| 54 | } |
| 55 | |
| 56 | if (!stylesheetPath) { |
| 57 | const newStylesheetPath = join(project.sourceRoot ?? 'src', 'tailwind.css'); |
| 58 | tree.create(newStylesheetPath, `@import 'tailwindcss';\n`); |
| 59 | |
| 60 | return updateWorkspace((workspace) => { |
| 61 | const project = workspace.projects.get(options.project); |
| 62 | if (project) { |
| 63 | const buildTarget = project.targets.get('build'); |
| 64 | assert(buildTarget, 'Build target should still be present'); |
| 65 | |
| 66 | // Update main styles |
| 67 | const buildOptions = buildTarget.options; |
| 68 | assert(buildOptions, 'Build options should still be present'); |
| 69 | const existingStyles = (buildOptions['styles'] as (string | { input: string })[]) ?? []; |
| 70 | buildOptions['styles'] = [newStylesheetPath, ...existingStyles]; |
| 71 | |
| 72 | // Update configuration styles |
| 73 | if (buildTarget.configurations) { |
| 74 | for (const config of Object.values(buildTarget.configurations)) { |
| 75 | if (config && 'styles' in config) { |
| 76 | const existingStyles = (config['styles'] as (string | { input: string })[]) ?? []; |
| 77 | config['styles'] = [newStylesheetPath, ...existingStyles]; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | }); |
| 83 | } else { |
| 84 | let stylesheetContent = tree.readText(stylesheetPath); |
| 85 | if (!/@import ["']tailwindcss["'];/.test(stylesheetContent)) { |
| 86 | stylesheetContent += `\n@import 'tailwindcss';\n`; |
| 87 | tree.overwrite(stylesheetPath, stylesheetContent); |
| 88 | } |
| 89 | } |
| 90 | }; |
| 91 | } |
| 92 | |
| 93 | function managePostCssConfiguration(project: ProjectDefinition): Rule { |
| 94 | return async (tree) => { |