fix(nextjs): Adding tailwind should work when creating an app OOTB (#22709)

This commit is contained in:
Nicholas Cunningham 2024-04-08 08:44:20 -06:00 committed by GitHub
parent 53d7913953
commit 50d89c7d5a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 31 additions and 19 deletions

View File

@ -46,6 +46,10 @@
"value": "less", "value": "less",
"label": "LESS [ https://lesscss.org ]" "label": "LESS [ https://lesscss.org ]"
}, },
{
"value": "tailwind",
"label": "tailwind [ https://tailwindcss.com/ ]"
},
{ {
"value": "styled-components", "value": "styled-components",
"label": "styled-components [ https://styled-components.com ]" "label": "styled-components [ https://styled-components.com ]"

View File

@ -7,6 +7,7 @@ import {
Tree, Tree,
} from '@nx/devkit'; } from '@nx/devkit';
import { initGenerator as jsInitGenerator } from '@nx/js'; import { initGenerator as jsInitGenerator } from '@nx/js';
import { setupTailwindGenerator } from '@nx/react';
import { import {
testingLibraryReactVersion, testingLibraryReactVersion,
typesReactDomVersion, typesReactDomVersion,
@ -70,6 +71,14 @@ export async function applicationGeneratorInternal(host: Tree, schema: Schema) {
const lintTask = await addLinting(host, options); const lintTask = await addLinting(host, options);
tasks.push(lintTask); tasks.push(lintTask);
if (options.style === 'tailwind') {
const tailwindTask = await setupTailwindGenerator(host, {
project: options.projectName,
});
tasks.push(tailwindTask);
}
const styledTask = addStyleDependencies(host, { const styledTask = addStyleDependencies(host, {
style: options.style, style: options.style,
swc: !host.exists(joinPathFragments(options.appProjectRoot, '.babelrc')), swc: !host.exists(joinPathFragments(options.appProjectRoot, '.babelrc')),

View File

@ -56,6 +56,7 @@ export function createApplicationFiles(host: Tree, options: NormalizedSchema) {
pageStyleContent: `.page {}`, pageStyleContent: `.page {}`,
stylesExt: options.style === 'less' ? options.style : 'css', stylesExt: options.style === 'less' ? options.style : 'css',
style: options.style === 'tailwind' ? 'css' : options.style,
}; };
const generatedAppFilePath = options.src const generatedAppFilePath = options.src

View File

@ -94,7 +94,7 @@ export async function normalizeOptions(
const appDir = options.appDir ?? true; const appDir = options.appDir ?? true;
const src = options.src ?? true; const src = options.src ?? true;
const styledModule = /^(css|scss|less)$/.test(options.style) const styledModule = /^(css|scss|less|tailwind)$/.test(options.style)
? null ? null
: options.style; : options.style;

View File

@ -46,6 +46,10 @@
"value": "less", "value": "less",
"label": "LESS [ https://lesscss.org ]" "label": "LESS [ https://lesscss.org ]"
}, },
{
"value": "tailwind",
"label": "tailwind [ https://tailwindcss.com/ ]"
},
{ {
"value": "styled-components", "value": "styled-components",
"label": "styled-components [ https://styled-components.com ]" "label": "styled-components [ https://styled-components.com ]"

View File

@ -5,35 +5,29 @@ import {
stripIndents, stripIndents,
Tree, Tree,
} from '@nx/devkit'; } from '@nx/devkit';
import * as chalk from 'chalk';
import { SetupTailwindOptions } from '../schema'; import { SetupTailwindOptions } from '../schema';
const knownLocations = [ // base directories and file types to simplify locating the stylesheet
// Plain React const baseDirs = ['src', 'pages', 'src/pages', 'src/app', 'app'];
'src/styles.css', const fileNames = ['styles', 'global'];
'src/styles.scss', const extensions = ['.css', '.scss', '.less'];
'src/styles.less',
// Next.js const knownLocations = baseDirs.flatMap((dir) =>
'pages/styles.css', fileNames.flatMap((name) => extensions.map((ext) => `${dir}/${name}${ext}`))
'pages/styles.scss', );
'pages/styles.less',
'app/global.css',
'app/global.scss',
'app/global.less',
];
export function addTailwindStyleImports( export function addTailwindStyleImports(
tree: Tree, tree: Tree,
project: ProjectConfiguration, project: ProjectConfiguration,
_options: SetupTailwindOptions _options: SetupTailwindOptions
) { ) {
const candidates = knownLocations.map((x) => const candidates = knownLocations.map((currentPath) =>
joinPathFragments(project.root, x) joinPathFragments(project.root, currentPath)
);
const stylesPath = candidates.find((currentStylePath) =>
tree.exists(currentStylePath)
); );
const stylesPath = candidates.find((x) => tree.exists(x));
if (stylesPath) { if (stylesPath) {
const content = tree.read(stylesPath).toString(); const content = tree.read(stylesPath).toString();