Skip to content

Commit

Permalink
feat(react): add tailwind as style prompt option for app gen
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Feb 13, 2024
1 parent ca90764 commit 4da75d2
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/generated/packages/react/generators/application.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,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
4 changes: 4 additions & 0 deletions docs/generated/packages/react/generators/host.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
4 changes: 4 additions & 0 deletions docs/generated/packages/react/generators/remote.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,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
4 changes: 4 additions & 0 deletions packages/create-nx-workspace/bin/create-nx-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,10 @@ async function determineReactOptions(
name: 'less',
message: 'LESS [ https://lesscss.org ]',
},
{
name: 'tailwind',
message: 'tailwind [ https://tailwindcss.com ]',
},
{
name: 'styled-components',
message:
Expand Down
15 changes: 15 additions & 0 deletions packages/react/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,21 @@ describe('app', () => {
});
});

describe('--style tailwind', () => {
it('should generate tailwind setup', async () => {
await applicationGenerator(appTree, { ...schema, style: 'tailwind' });
expect(appTree.exists('my-app/tailwind.config.js')).toEqual(true);
expect(appTree.read('my-app/src/styles.css', 'utf-8'))
.toMatchInlineSnapshot(`
"@tailwind base;
@tailwind components;
@tailwind utilities;
/* You can add global styles to this file, and also import other style files */
"
`);
});
});

it('should setup jest with tsx support', async () => {
await applicationGenerator(appTree, { ...schema, name: 'my-app' });

Expand Down
8 changes: 8 additions & 0 deletions packages/react/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
} from '@nx/eslint/src/generators/utils/eslint-file';
import { initGenerator as jsInitGenerator } from '@nx/js';
import { logShowProjectCommand } from '@nx/devkit/src/utils/log-show-project-command';
import { setupTailwindGenerator } from '../setup-tailwind/setup-tailwind';

async function addLinting(host: Tree, options: NormalizedSchema) {
const tasks: GeneratorCallback[] = [];
Expand Down Expand Up @@ -133,6 +134,13 @@ export async function applicationGeneratorInternal(
createApplicationFiles(host, options);
addProject(host, options);

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

if (options.bundler === 'vite') {
const { createOrEditViteConfig, viteConfigurationGenerator } =
ensurePackage<typeof import('@nx/vite')>('@nx/vite', nxVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<% if (!styledModule && style !== 'none') { %><link rel="stylesheet" href="/src/styles.<%= style %>" /><% } %>
<% if (!styledModule && style !== 'none') { %><link rel="stylesheet" href="/src/styles.<%= style === 'tailwind' ? 'css' : style %>" /><% } %>
</head>
<body>
<div id="root"></div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<% if (classComponent) { %>
import { Component } from 'react';
<% } if (!minimal) { %>
import NxWelcome from "./nx-welcome";
<% } %>

<% if (classComponent) { %>
export class App extends Component {
render() {
<% } else { %>
export function App() {
<% } %>
return (
<div>
<% if (!minimal) { %>
<NxWelcome title="<%= projectName %>"/>
<% } else { %>
<h1>
<span> Hello there, </span>
Welcome <%= projectName %> 👋
</h1>
<% } %>
</div>);
<% if (classComponent) { %>
}
}
<% } else { %>
}
<% } %>

export default App;

<% if (inSourceTests === true) { %> <%- inSourceVitestTests %> <% } %>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* You can add global styles to this file, and also import other style files */
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export function createApplicationFiles(host: Tree, options: NormalizedSchema) {
styleSolutionSpecificAppFiles = '../files/style-styled-module';
} else if (options.style === 'styled-jsx') {
styleSolutionSpecificAppFiles = '../files/style-styled-jsx';
} else if (options.style === 'tailwind') {
styleSolutionSpecificAppFiles = '../files/style-tailwind';
} else if (options.style === 'none') {
styleSolutionSpecificAppFiles = '../files/style-none';
} else if (options.globalCss) {
Expand Down Expand Up @@ -185,6 +187,10 @@ function createNxWebpackPluginOptions(
styles:
options.styledModule || !options.hasStyles
? []
: [`./src/styles.${options.style}`],
: [
`./src/styles.${
options.style !== 'tailwind' ? options.style : 'css'
}`,
],
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function normalizeOptions<T extends Schema = Schema>(

const fileName = options.pascalCaseFiles ? 'App' : 'app';

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

Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/generators/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,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
4 changes: 4 additions & 0 deletions packages/react/src/generators/host/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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
4 changes: 4 additions & 0 deletions packages/react/src/generators/remote/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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
1 change: 1 addition & 0 deletions packages/react/src/utils/assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const VALID_STYLES = [
'css',
'scss',
'less',
'tailwind',
'styled-components',
'@emotion/styled',
'styled-jsx',
Expand Down
1 change: 1 addition & 0 deletions packages/react/typings/style.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export type SupportedStyles =
| 'css'
| 'scss'
| 'less'
| 'tailwind'
| 'styled-components'
| '@emotion/styled'
| 'styled-jsx'
Expand Down

0 comments on commit 4da75d2

Please sign in to comment.