Skip to content

Commit

Permalink
fix(react): fix tailwind for react library and component (#29319)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #27954
  • Loading branch information
xiongemi authored Dec 12, 2024
1 parent 36eaafd commit cfcd4d1
Show file tree
Hide file tree
Showing 17 changed files with 838 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/generated/packages/next/generators/component.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,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/next/generators/library.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,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/component.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,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/library.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

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions packages/next/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,48 @@ describe('app', () => {
});
});

describe('--style tailwind', () => {
it('should generate tailwind styles', async () => {
await applicationGenerator(tree, {
directory: 'myapp',
style: 'tailwind',
});

expect(tree.exists(`myapp/src/app/page.module.scss`)).toBeFalsy();
expect(tree.exists(`myapp/src/app/global.css`)).toBeTruthy();

const indexContent = tree.read(`myapp/src/app/page.tsx`, 'utf-8');
expect(indexContent).not.toContain(`import styles from './page.module`);
expect(indexContent).not.toContain(
`import styled from 'styled-components'`
);
expect(indexContent).toMatchSnapshot();

expect(tree.read(`myapp/src/app/layout.tsx`, 'utf-8'))
.toMatchInlineSnapshot(`
"import './global.css';
export const metadata = {
title: 'Welcome to myapp',
description: 'Generated by create-nx-workspace',
};
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
"
`);
});
});

it('should setup jest with tsx support', async () => {
const name = uniq();

Expand Down
4 changes: 4 additions & 0 deletions packages/next/src/generators/component/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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/next/src/generators/library/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,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 @@ -192,6 +192,40 @@ export default defineConfig({
"
`;

exports[`app --style scss should generate scss styles 1`] = `
"// Uncomment this line to use CSS modules
// import styles from './app.module.scss';
import NxWelcome from "./nx-welcome";
export function App() {
return (
<div>
<NxWelcome title="my-app"/>
</div>
);
}
export default App;
"
`;

exports[`app --style tailwind should not generate any styles files 1`] = `
"import NxWelcome from './nx-welcome';
export function App() {
return (
<div>
<NxWelcome title="my-app" />
</div>
);
}
export default App;
"
`;

exports[`app not nested should add vite types to tsconfigs 1`] = `
"/// <reference types='vitest' />
import { defineConfig } from 'vite';
Expand Down Expand Up @@ -358,6 +392,66 @@ export function App() {
export default App;
"
`;

exports[`app should generate valid .babelrc JSON config for CSS-in-JS solutions 1`] = `
"import styled from 'styled-components';
import NxWelcome from "./nx-welcome";
const StyledApp = styled.div\`
// Your style here
\`;
export function App() {
return (
<StyledApp>
<NxWelcome title="my-app"/>
</StyledApp>
);
}
export default App;
"
`;

exports[`app should generate valid .babelrc JSON config for CSS-in-JS solutions 2`] = `
"import NxWelcome from "./nx-welcome";
export function App() {
return (
<div>
<style jsx>{\`/** your style here **/\`}</style>
<NxWelcome title="my-app"/>
</div>
);
}
export default App;
"
`;
exports[`app should generate valid .babelrc JSON config for CSS-in-JS solutions 3`] = `
"import styled from '@emotion/styled';
import NxWelcome from "./nx-welcome";
const StyledApp = styled.div\`
// Your style here
\`;
export function App() {
return (
<StyledApp>
<NxWelcome title="my-app"/>
</StyledApp>
);
}
export default App;
"
`;
Expand Down
18 changes: 18 additions & 0 deletions packages/react/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,17 @@ describe('app', () => {
expect(() => {
readJson(appTree, `my-app/.babelrc`);
}).not.toThrow();
const content = appTree.read('my-app/src/app/app.tsx').toString();
expect(content).toMatchSnapshot();
}
);

describe('--style scss', () => {
it('should generate scss styles', async () => {
await applicationGenerator(appTree, { ...schema, style: 'scss' });
expect(appTree.exists('my-app/src/app/app.module.scss')).toEqual(true);
const content = appTree.read('my-app/src/app/app.tsx').toString();
expect(content).toMatchSnapshot();
});
});

Expand All @@ -509,6 +513,20 @@ describe('app', () => {
"
`);
});

it('should not generate any styles files', async () => {
await applicationGenerator(appTree, { ...schema, style: 'tailwind' });

expect(appTree.exists('my-app/src/app/app.tsx')).toBeTruthy();
expect(appTree.exists('my-app/src/app/app.spec.tsx')).toBeTruthy();
expect(appTree.exists('my-app/src/app/app.css')).toBeFalsy();
expect(appTree.exists('my-app/src/app/app.scss')).toBeFalsy();
expect(appTree.exists('my-app/src/app/app.module.css')).toBeFalsy();
expect(appTree.exists('my-app/src/app/app.module.scss')).toBeFalsy();

const content = appTree.read('my-app/src/app/app.tsx').toString();
expect(content).toMatchSnapshot();
});
});

it('should setup jest with tsx support', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`component --style @emotion/styled should use @emotion/styled as the styled API library 1`] = `
"import styled from '@emotion/styled';
const StyledHello = styled.div\`
color: pink;
\`;
export function Hello() {
return (
<StyledHello>
<h1>Welcome to Hello!</h1>
</StyledHello>
);
}
export default Hello;
"
`;

exports[`component --style none should generate component files without styles 1`] = `
"export function Hello() {
return (
<div>
<h1>Welcome to Hello!</h1>
</div>
);
}
export default Hello;
"
`;

exports[`component --style styled-components should use styled-components as the styled API library 1`] = `
"import styled from 'styled-components';
const StyledHello = styled.div\`
color: pink;
\`;
export function Hello() {
return (
<StyledHello>
<h1>Welcome to Hello!</h1>
</StyledHello>
);
}
export default Hello;
"
`;

exports[`component --style styled-jsx should use styled-jsx as the styled API library 1`] = `
"export function Hello() {
return (
<div>
<style jsx>{\`
div {
color: pink;
}
\`}</style>{' '}
<h1>Welcome to Hello!</h1>
</div>
);
}
export default Hello;
"
`;
exports[`component --style tailwind should not generate any style in component 1`] = `
"export function Hello() {
return (
<div className={styles['container']}>
<h1>Welcome to Hello!</h1>
</div>
);
}
export default Hello;
"
`;
Loading

0 comments on commit cfcd4d1

Please sign in to comment.