Integrate Tailwind CSS into Theme (SSR-safe with Webpack)
As themes increase in size and complexity, the challenges associated with maintaining and scaling styles become more challenging. The management of multiple separate Less files may hinder development iterations and complicate the implementation of user interface modifications. Although the current configuration—utilizing Less and CSS compiled with Webpack loaders such as less-loader
, css-loader
, and MiniCssExtractPlugin
—initially functions effectively with global rules specified in files like base.global.less
and individual Less files for distinct components, this methodology exhibits limitations as projects progress.
To ease these challenges, it is advisable to integrate a utility-first CSS framework, such as Tailwind CSS or Shed.css, into your theme.
This page outlines the process of incorporating Tailwind CSS into your Fynd theme, detailing the setup alongside your existing Less and CSS architecture while ensuring compatibility with your current Webpack configuration.
The adoption of Tailwind CSS enhances consistency, accelerates development, and simplifies responsive design by offering an extensive collection of utility classes directly within your markup. This hybrid strategy facilitates a gradual transition to Tailwind, allowing you to retain your existing styles, thereby making the process more seamless and sustainable. For additional information, please consult the Tailwind CSS documentation.
Prerequisites
- Existing theme using Webpack with
less-loader
,css-loader
, andMiniCssExtractPlugin
. Refer to Get Started to create a theme.
1. Install Dependencies
-
Make sure you are inside your theme directory.
-
Run the following command in your terminal to install the following dependencies:
npm install -D tailwindcss@3 postcss@8 postcss-loader@7 autoprefixer@10 @tailwindcss/forms @tailwindcss/typography @tailwindcss/aspect-ratio
- tailwindcss@3: utility classes generator
- postcss and postcss-loader: run Tailwind in Webpack builds
- autoprefixer: vendor prefixes
- Plugins (optional): forms, typography and aspect-ratio
2. Create PostCSS Config File
-
Create a postcss.config.js file in the root directory of your theme and add the following:
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};This configuration instructs PostCSS to process your stylesheets with both Tailwind CSS and Autoprefixer during the build process. Tailwind will generate utility classes based on your configuration and project files, while Autoprefixer will automatically add vendor prefixes to ensure cross-browser compatibility.
3. Create Tailwind Config File
-
Create a
tailwind.config.js
file in the root directory of your theme./** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./theme/**/*.{js,jsx,ts,tsx,html}"], // Files Tailwind will scan for class names
corePlugins: { preflight: false }, // Disables Tailwind's base reset to preserve your Less styles
theme: { extend: {} }, // Extend the default theme as needed
plugins: [
require('@tailwindcss/forms'),
require('@tailwindcss/typography'),
require('@tailwindcss/aspect-ratio'),
],
};This configuration file tells Tailwind how to scan your project for class names, disables Tailwind's default CSS reset (so your existing Less base styles remain unaffected), and enables useful plugins for forms, typography, and aspect ratios.
4. Create an Entry Stylesheet
-
Create a tailwind.css file in the path: theme/styles/.
@tailwind base;
@tailwind components;
@tailwind utilities;This file serves as the main entry point for Tailwind's styles. The three
@tailwind
directives tell Tailwind to inject its base styles, component classes, and utility classes into your CSS bundle. By including this file in your project, you ensure that all Tailwind CSS features are available for use throughout your theme.
5. Import Tailwind Globally
-
Add the following in index.jsx file available in the path: theme/ below the existing
base.global.less
to import Tailwind after your base Less:import "./styles/tailwind.css";
6. Update Webpack Configuration for SSR and Tailwind
To ensure Tailwind CSS works correctly with server-side rendering (SSR), you need to update your webpack.config.js file.
-
Locate all rules in your Webpack config that extract CSS (these typically use both
MiniCssExtractPlugin.loader
andcss-loader
). You may have multiple such rules for different file types. -
Insert
postcss-loader
immediately aftercss-loader
in each of these rules. This enables Tailwind and Autoprefixer to process your CSS. -
Keep
less-loader
only for.less
files—do not addpostcss-loader
to Less rules unless you specifically want to process Less output with PostCSS.[
MiniCssExtractPlugin.loader, // Already exists
{
loader: "css-loader",
options: {
modules: false
}
}, // Already exists
{
loader: "postcss-loader"
}, // Need to be added
]
7. Start the Local Development Server
-
To preview your theme with Tailwind CSS enabled, run the following command from your theme directory:
fdk theme serve
8. Verify if Tailwind CSS Integration Works
Visual Test
To verify that Tailwind CSS is integrated and working correctly, you can perform a simple visual test in your home.jsx
file in the path: theme/pages/. Add the following code snippet to your homepage component:
<div className="bg-emerald-500 text-white p-2 text-sm text-center">
Tailwind OK
</div>
Output
CLI Test (optional)
npx tailwindcss -i theme/styles/tailwind.css -o /tmp/tw.css --content './theme/**/*.{js,jsx,ts,tsx,html}'
grep -q '.bg-emerald-500' /tmp/tw.css && echo 'Tailwind OK' || echo 'Not found'
Response
Tailwind OK
This checks that Tailwind generated the .bg-emerald-500
utility.
Your theme is running with SSR, alongside your existing Less. This hybrid approach lets you adopt Tailwind incrementally, speed up UI delivery, and keep long‑term maintainability.