A streamlined Tailwind CSS template for Create React App in TypeScript

  • By Sung M. Kim
  • Last update: Dec 26, 2022
  • Comments: 11

cra-template-tailwindcss-typescript

All Contributors

A streamlined Tailwind CSS v2.0 template for Create React App in TypeScript.

Usage

npx create-react-app my-app --template tailwindcss-typescript

Experimental JIT mode

You can enable Just-in-Time "JIT" mode by uncommenting mode: "jit" in tailwind.config.js.

IMPORTANT: Do NOT comment purge when you enable JIT mode, otherwise your CSS will be empty!

module.exports = {
  // Uncomment the line below to enable the experimental Just-in-Time ("JIT") mode.
  // 👇👇👇👇👇
  // mode: "jit",
  theme: {
    extend: {},
  },
  // other options...
};

Credits

This project was made possible thanks to the following projects.

  1. GeoffSelby/cra-template-tailwindcss - A streamlined Tailwind CSS template for Create React App (in JavaScript).
  2. cra-template-typescript - An official TypeScript template for create-react-app.

License

MIT © Sung M. Kim

Contributors

Thanks goes to these wonderful people (emoji key):


Ivo Angelov

💻 🚧

Roger Chi

🐛 💻

Jag Reehal

💻

Michael

🐛

Mustafa YILDIZ

⚠️ 📆 🐛

This project follows the all-contributors specification. Contributions of any kind welcome!

Github

https://github.com/dance2die/cra-template-tailwindcss-typescript

Comments(11)

  • 1

    "sleep" does not work on Windows

    Hello

    "react-scripts:start": "sleep 5 && react-scripts start", does not work as "sleep" is not present on win.

    I'm doing "react-scripts:start": "timeout /t 1 /nobreak > NUL && react-scripts start",

    wanted to point it out, maybe it's something you would like to fix

  • 2

    Replace autoprefixer with postcss preset env

    This is a good template but to make it great (😉) I'd replace autoprefixer with postcss-preset-env which includes autoprefixer and additional polyfills

  • 3

    Powershell: npm start fails with 'sleep' is not recognized as an internal or external command

    First of all, great project. Secondly, using bash on Windows successfully open localhost:3000. However, the command fails when using Powershell. I'm not sure whether this is a Powershell-specific error or not.

    Here's the whole error: image

  • 4

    Dependencies not added when using NPX

    Hi. I am trying this template out with Node + Yarn on Windows. First I run this command:

    npx create-react-app my-tailwind-app  --template tailwindcss-typescript
    

    But I found out that the generated project can't be run right away.

    I ended up adding these packages, either to global or to the project's dependencies:

    • npm-run-all
    • delay-cli
    • postcss
    • postcss-cli
    • autoprefixer
    • typescript
    • @types/react
    • @types/react-dom
    • @types/jest
    • @testing-library/react
    • @testing-library/jest-dom

    Should these be described in README section for getting started?

    Or better, should most of these be added to (dev) dependencies in the template from the start?

    UPDATE

    After testing further, it seems to be the issue with npx create-react-app that does not add the dependencies to package.json. Using yarn create react-app did it as in this comment

  • 5

    Ignore Tailwind-processed CSS file and remove it from template

    I don't think it's necessary to keep the processed CSS under version control. The file is generated automatically when the development server starts. Also, since GitHub determines repository language stats using file size, repositories using this template display a distorted CSS percentage figure.

  • 6

    Update Tailwind CSS to the latest version (1.4.4)

    Version 1.4.0 adds built-in support for PurgeCSS: https://github.com/tailwindcss/tailwindcss/releases/tag/v1.4.0

    The @fullhuman/postcss-purgecss and purgecss dependencies aren't needed anymore, so I removed them.

  • 7

    build script doesn't actually get NODE_ENV=production into postcss

    When running npm run build, the run-s command executes the "env:prod" script in a separate command than the "build:css" command, so NODE_ENV=production is not persisted into the postcss call. I suggest adding "cross-env NODE_ENV=production" directly into the build:css script, e.g.:

      "scripts": {
        "start": "run-p watch:css react-scripts:start",
        "build": "run-s build:css react-scripts:build",
        "test": "react-scripts test",
        "eject": "react-scripts eject",
        "build:css": "cross-env NODE_ENV=production postcss src/styles/tailwind.css -o src/styles/index.css",
        "watch:css": "cross-env NODE_ENV=development postcss src/styles/tailwind.css -o src/styles/index.css --watch",
        "react-scripts:start": "sleep 5 && react-scripts start",
        "react-scripts:build": "react-scripts build"
      },
    

    This is the file size change after making this modification to package.json for a vanilla project using cra-template-tailwindcss-typescript:

    Creating an optimized production build...
    Compiled successfully.
    
    File sizes after gzip:
    
      39.39 KB            build/static/js/2.f2e7b3c8.chunk.js
      2.45 KB             build/static/js/main.559c7be3.chunk.js
      1.69 KB (-97.8 KB)  build/static/css/main.808035e1.chunk.css
      783 B               build/static/js/runtime-main.3cf3366c.js
    
    The project was built assuming it is hosted at /.
    You can control this with the homepage field in your package.json.
    
    The build folder is ready to be deployed.
    You may serve it with a static server:
    
      yarn global add serve
      serve -s build
    
    Find out more about deployment here:
    
      bit.ly/CRA-deploy
    
    Done in 13.71s.
    
  • 8

    more detailed documentation

    Hi,

    I manage to add your template with the npx command. But I have some questions

    (1) you say we enable JIT by changing tailwind.config.js to add "mode: "jit"," but what about postcss.config.js that still looks the following? Shouldn't that be changed as well "tailwindcss" to "tailwindcss/hit" ?

    module.exports = {
      plugins: [
        require('tailwindcss'),
        require('postcss-preset-env'),
      ],
    };
    

    (2) IMPORTANT: Do NOT comment purge when you enable JIT mode, otherwise your CSS will be empty!

    • But can we comment out at all? Can I comment out purge after JIT mode is enabled?

    (3) As this is a custom build tailwind project how do we use it? When to run the following scripts:

    build:css": "cross-env TAILWIND_MODE=build NODE_ENV=production postcss src/styles/tailwind.css -o src/styles/index.css", "watch:css": "cross-env TAILWIND_MODE=watch NODE_ENV=development postcss src/styles/tailwind.css -o src/styles/index.css --watch", "react-scripts:start": "sleep 5 && react-scripts start",

  • 9

    Bump lodash from 4.17.20 to 4.17.21

    Bumps lodash from 4.17.20 to 4.17.21.

    Commits
    • f299b52 Bump to v4.17.21
    • c4847eb Improve performance of toNumber, trim and trimEnd on large input strings
    • 3469357 Prevent command injection through _.template's variable option
    • See full diff in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

  • 10

    Bump ini from 1.3.5 to 1.3.8

    Bumps ini from 1.3.5 to 1.3.8.

    Commits
    • a2c5da8 1.3.8
    • af5c6bb Do not use Object.create(null)
    • 8b648a1 don't test where our devdeps don't even work
    • c74c8af 1.3.7
    • 024b8b5 update deps, add linting
    • 032fbaf Use Object.create(null) to avoid default object property hazards
    • 2da9039 1.3.6
    • cfea636 better git push script, before publish instead of after
    • 56d2805 do not allow invalid hazardous string as section name
    • See full diff in compare view
    Maintainer changes

    This version was pushed to npm by isaacs, a new releaser for ini since your current version.


    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

  • 11

    Replace CRACO with Macro

    Context: https://www.reddit.com/r/reactjs/comments/flebuh/how_to_add_tailwind_to_unejected_createreactapp/

    KCD's code: https://github.com/kentcdodds/bookshelf/commit/b984b34239aee9f57fb7bf8868b7e78dfe475ae8

    CRA Custom Templates: https://create-react-app.dev/docs/custom-templates/