Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/generated/packages/next/generators/application.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
"value": "less",
"label": "LESS [ https://lesscss.org ]"
},
{
"value": "tailwind",
"label": "tailwind [ https://tailwindcss.com/ ]"
},
{
"value": "styled-components",
"label": "styled-components [ https://styled-components.com ]"
Expand Down
9 changes: 9 additions & 0 deletions packages/next/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Tree,
} from '@nx/devkit';
import { initGenerator as jsInitGenerator } from '@nx/js';
import { setupTailwindGenerator } from '@nx/react';
import {
testingLibraryReactVersion,
typesReactDomVersion,
Expand Down Expand Up @@ -70,6 +71,14 @@ export async function applicationGeneratorInternal(host: Tree, schema: Schema) {
const lintTask = await addLinting(host, options);
tasks.push(lintTask);

if (options.style === 'tailwind') {
const tailwindTask = await setupTailwindGenerator(host, {
project: options.projectName,
});

tasks.push(tailwindTask);
}

const styledTask = addStyleDependencies(host, {
style: options.style,
swc: !host.exists(joinPathFragments(options.appProjectRoot, '.babelrc')),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export function createApplicationFiles(host: Tree, options: NormalizedSchema) {
pageStyleContent: `.page {}`,

stylesExt: options.style === 'less' ? options.style : 'css',
style: options.style === 'tailwind' ? 'css' : options.style,
};

const generatedAppFilePath = options.src
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export async function normalizeOptions(
const appDir = options.appDir ?? true;
const src = options.src ?? true;

const styledModule = /^(css|scss|less)$/.test(options.style)
const styledModule = /^(css|scss|less|tailwind)$/.test(options.style)
? null
: options.style;

Expand Down
4 changes: 4 additions & 0 deletions packages/next/src/generators/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
"value": "less",
"label": "LESS [ https://lesscss.org ]"
},
{
"value": "tailwind",
"label": "tailwind [ https://tailwindcss.com/ ]"
},
{
"value": "styled-components",
"label": "styled-components [ https://styled-components.com ]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,29 @@ import {
stripIndents,
Tree,
} from '@nx/devkit';
import * as chalk from 'chalk';

import { SetupTailwindOptions } from '../schema';

const knownLocations = [
// Plain React
'src/styles.css',
'src/styles.scss',
'src/styles.less',
// base directories and file types to simplify locating the stylesheet
const baseDirs = ['src', 'pages', 'src/pages', 'src/app', 'app'];
const fileNames = ['styles', 'global'];
const extensions = ['.css', '.scss', '.less'];

// Next.js
'pages/styles.css',
'pages/styles.scss',
'pages/styles.less',

'app/global.css',
'app/global.scss',
'app/global.less',
];
const knownLocations = baseDirs.flatMap((dir) =>
fileNames.flatMap((name) => extensions.map((ext) => `${dir}/${name}${ext}`))
);

export function addTailwindStyleImports(
tree: Tree,
project: ProjectConfiguration,
_options: SetupTailwindOptions
) {
const candidates = knownLocations.map((x) =>
joinPathFragments(project.root, x)
const candidates = knownLocations.map((currentPath) =>
joinPathFragments(project.root, currentPath)
);
const stylesPath = candidates.find((currentStylePath) =>
tree.exists(currentStylePath)
);
const stylesPath = candidates.find((x) => tree.exists(x));

if (stylesPath) {
const content = tree.read(stylesPath).toString();
Expand Down