Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

ynwd
ynwd

Posted on • Edited on

     

How to integrate tailwind, react and webpack

Step by step how to use tailwind together with react and webpack. It will be used when create shared components.

.├── babel.config.js├── package.json├── postcss.config.js├── src│   ├── App.js│   ├── index.css│   ├── index.html│   └── index.js├── tailwind.config.js└── webpack.config.js
Enter fullscreen modeExit fullscreen mode

Clone our previous branch:https://github.com/ynwd/postcss/tree/tailwind

Install react

npm i react react-dom
Enter fullscreen modeExit fullscreen mode

Install babel

npm i @babel/core @babel/preset-env @babel/preset-react -D
Enter fullscreen modeExit fullscreen mode

Create babel config

// babel.config.jsmodule.exports={presets:[["@babel/preset-react",{targets:{node:"current",},},],["@babel/preset-env",{"useBuiltIns":"entry"},]],}
Enter fullscreen modeExit fullscreen mode

Setup webpack

Install plugins and babel loader

npm i html-webpack-plugin webpack-dev-server babel-loader -D
Enter fullscreen modeExit fullscreen mode

Update webpack config

constMiniCssExtractPlugin=require("mini-css-extract-plugin")constHtmlWebpackPlugin=require('html-webpack-plugin')module.exports={mode:"production",module:{rules:[{test:/\.(js|jsx)$/,exclude:/nodeModules/,use:{loader:'babel-loader'}},{test:/\.css$/,use:[MiniCssExtractPlugin.loader,"css-loader","postcss-loader",],},],},plugins:[newMiniCssExtractPlugin(),newHtmlWebpackPlugin({template:'./src/index.html'})],}
Enter fullscreen modeExit fullscreen mode

Setup react app

Create tailwind entry point

/* src/index.css */@tailwindbase;@tailwindcomponents;@tailwindutilities;
Enter fullscreen modeExit fullscreen mode

Update react entry point

// src/index.jsimportReactfrom"react"importReactDomfrom"react-dom"importAppfrom"./App"import"./index.css"ReactDom.render(<App/>,document.getElementById('app'))
Enter fullscreen modeExit fullscreen mode

Create react app

// src/App.jsimportReactfrom"react"functionApp(){return<divclassName="bg-white mx-auto max-w-sm shadow-lg rounded-lg overflow-hidden"><divclassName="sm:flex sm:items-center px-6 py-4"><imgclassName="block h-16 sm:h-24 rounded-full mx-auto mb-4 sm:mb-0 sm:mr-4 sm:ml-0"src="https://avatars.githubusercontent.com/u/10122431?s=400&v=4"alt=""/><divclassName="text-center sm:text-left sm:flex-grow"><divclassName="mb-4"><pclassName="text-xl leading-tight">JohnDoe</p><pclassName="text-sm leading-tight text-grey-dark">Contributoratsomerepo</p></div><div><buttonclassName="text-xs font-semibold rounded-full px-4 py-1 leading-normal bg-white border border-purple text-purple hover:bg-purple hover:text-white">Message</button></div></div></div></div>}exportdefaultApp
Enter fullscreen modeExit fullscreen mode

Create index html

<!-- src/index.html --><!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>React Webpack App</title></head><body><divid="app"></div></body></html>
Enter fullscreen modeExit fullscreen mode

Run webpack

npx webpack serve
Enter fullscreen modeExit fullscreen mode

Update package to purge tailwind for production. This will removing unused CSS from your production builds for maximum performance.

{..."scripts":{"serve":"webpack serve","build":"NODE_ENV=production webpack"}...}
Enter fullscreen modeExit fullscreen mode

Build

npm run build
Enter fullscreen modeExit fullscreen mode

You can see the final source code on this branch:https://github.com/ynwd/postcss/tree/react

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
rajvirsingh1313 profile image
Rajvir Singh
Hello there! I am Rajvir Singh from Punjab, India. Always Learning Something New.
  • Location
    India
  • Work
    On My Own
  • Joined

Thanks for such help, I think it would be quite helpful if you clear out some few things about cloning the repo.

CollapseExpand
 
codewithjohnson profile image
Muyiwa Johnson
Tall
  • Pronouns
    He/Him
  • Joined

will this help avoid white flash on reload on CRA?

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

TypeScript & Serverless
  • Joined

More fromynwd

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp