Skip to content

Instantly share code, notes, and snippets.

@bradtraversy
Last active July 25, 2026 14:39
Show Gist options
  • Select an option

  • Save bradtraversy/1c93938c1fe4f10d1e5b0532ae22e16a to your computer and use it in GitHub Desktop.

Select an option

Save bradtraversy/1c93938c1fe4f10d1e5b0532ae22e16a to your computer and use it in GitHub Desktop.
Setup Webpack with Tailwind CSS

Webpack & Tailwind CSS Setup

Create your package.json

npm init -y

Create your src folder

Create a folder called src and add an empty index.js file. The code that webpack compiles goes in here including any Javascript modules and the main Tailwind file.

Install Tailwind

npm i -D tailwindcss

Install Webpack & Loaders

npm i -D webpack webpack-cli webpack-dev-server style-loader css-loader postcss postcss-loader postcss-preset-env

Create webpack.config.js in the root and add this to it...

const path = require('path')

module.exports = {
  mode: 'development',
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
  },
  devServer: {
    static: {
      directory: path.resolve(__dirname, 'dist'),
    },
    port: 3000,
    open: true,
    hot: true,
    compress: true,
    historyApiFallback: true,
  },
  module: {
    rules: [
      {
        test: /\.css$/i,
        include: path.resolve(__dirname, 'src'),
        use: ['style-loader', 'css-loader', 'postcss-loader'],
      },
    ],
  },
}

Add Tailwind Directives

Create a style.css in your src folder and add these 3 lines

@tailwind base;
@tailwind components;
@tailwind utilities;

Tailwind Config File

run the following command to generate your tailwind.config.js file and add this to it

module.exports = {
  content: ['./dist/*.html'],
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
}

PostCCSS Config File

Create a file in the root called postcss.config.js and add this

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

Add this line to your src/index.js

import './style.css';

Create a dist/index.html file and add this

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Webpack App</title>
  </head>
  <body>
    <h1 class="text-4xl text-blue-700">My Webpack + Tailwind App</h1>
    <script src="bundle.js"></script>
  </body>
</html>

Add scripts to package.json

Add the following to your package.json file

"scripts": {
    "dev": "webpack serve",
    "build": "webpack"
  },

Running your app

To build once and create your dist/bundle.js file, run

npm run build

To run your webpack server

npm run dev

You now have Webpack setup along with Tailwind CSS

@yotamcuralife

Copy link
Copy Markdown

Please!

@charanjit-singh

charanjit-singh commented Mar 7, 2025

Copy link
Copy Markdown

Guys,
I know it's open source work. I'd appreciate if you'd follow me here: https://x.com/cjsingg

So here's tailwind V4 solution:

pnpm add tailwindcss @tailwindcss/postcss postcss postcss-loader -D

Note: -D at end

Now go to your package.json and add:

// in root object
 "postcss": {
    "plugins": {
      "@tailwindcss/postcss": {},
      "autoprefixer": {}
    }
  },

In app.tsx:

import './App.css';

In global.css or App.css:

@import "tailwindcss";

In webpack config

 {
        test: /\.s?css$/,
        use: ['style-loader', 'css-loader', 'postcss-loader'], // add "postcss-loader" to CSS loaders
        exclude: /\.module\.s?(c|a)ss$/,
      },
      
      

Done!

Thank me here: https://x.com/cjsingg

@koraysels

koraysels commented Mar 9, 2025

Copy link
Copy Markdown

Nice! this works fine for development.. But what if you want to do a production build? Sometimes you dont want css through javascript on inlined.. . especially when deploying a multipage application to production.

@yotamon

yotamon commented Mar 10, 2025

Copy link
Copy Markdown

Thanks @charanjit-singh, I used Cursor Agent to fix my issues :)

@jodiedoubleday

Copy link
Copy Markdown

Anybody got a good solution for production builds?
https://webpack.js.org/loaders/style-loader/#recommend

Style-loader recommends using mini-css-extract-plugin but for the life of me i can;t get this working properly (tailwind 3 or 4)

@maheshj01

Copy link
Copy Markdown

Thanks @charanjit-singh This worked

@touhid-rahman

Copy link
Copy Markdown

Anyone has webpack starter code with Tailwind 4?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment