Migrating an Angular project (with Sass) to Tailwind CSS v4
The Tailwind CSS v4 release is a big one, with a lot of changes. Unfortunately, the automated upgrade tool doesn’t work with Sass (i.e. .scss files). If you have an Angular project with Sass (e.g. one that uses Angular Material) you can’t directly use the upgrade tool.
In this post, I show you how to manually migrate an Angular project with Sass to Tailwind v4, whilst still using the automated upgrade tool to do most of the work.
When you run npx @tailwindcss/upgrade@next — the upgrade tool — in your Angular (with Sass) project you’ll see output like the following:
≈ tailwindcss v4.0.2
│ Searching for CSS files in the current directory and its subdirectories…
│ ↳ Cannot find any CSS files that reference Tailwind CSS.│ Before your project can be upgraded you need to create a CSS file that imports Tailwind│ CSS or uses `@tailwind`.
│ ↳ No PostCSS config found, skipping migration.
│ Updating dependencies…
│ ↳ Updated package: `tailwindcss`
│ Verify the changes and commit them to your repository.You can see here that the upgrade tool can’t find any CSS files that reference Tailwind CSS, as we’re using Sass (.scss). It also doesn’t detect a PostCSS config, as the Angular CLI took care of this for you for Tailwind v3.
To “force” the upgrade tool to work we’ll:
- Create a PostCSS config file in the root — we’ll need this anyway for Tailwind v4 so it’ll stay.
- We’ll create a temporary CSS file in the root that contains only the Tailwind specific bits from our
src/styles.scssfile. This is only temporary and for the upgrade tool to do its magic. We’ll inspect the changes made and apply them to our main styles file and delete this temporary one.
The manual migration process
Section titled “The manual migration process”-
Make sure you start with a clean working directory and commit any changes you have, as we’ll be using
git diffto see what changes the upgrade tool makes — so we know what changes to apply manually to oursrc/styles.scssfile, and how templates etc. have been updated. -
Create a
.postcssrc.jsonfile in the root of your project with the following:{"plugins": {"tailwindcss": {},"autoprefixer": {}}} -
Create a
tailwind.tmp.cssfile in the root of your project and copy over only the Tailwind specific bits from your mainsrc/styles.scssfile. For example:@import 'tailwindcss/base';@import 'tailwindcss/components';@import 'tailwindcss/utilities'; -
Run the upgrade tool:
Terminal window npx @tailwindcss/upgrade@next --forceYou should see output like:
Terminal window ≈ tailwindcss v4.0.2│ Searching for CSS files in the current directory and its subdirectories…│ ↳ Linked `./tailwind.config.js` to `./tailwind.tmp.css`│ Migrating JavaScript configuration files…│ ↳ Migrated configuration file: `./tailwind.config.js`│ Migrating templates…│ ↳ Migrated templates for configuration file: `./tailwind.config.js`│ Migrating stylesheets…│ ↳ Migrated stylesheet: `./tailwind.tmp.css`│ ↳ Installed package: `@tailwindcss/postcss`│ ↳ Removed package: `autoprefixer`│ Updating dependencies…│ ↳ Updated package: `tailwindcss`│ Verify the changes and commit them to your repository. -
Inspect your
tailwind.tmp.cssfile and compare it to yoursrc/styles.scssfile.You can use
git diffto see the changes made by the upgrade tool:Terminal window git diff tailwind.tmp.cssYou’ll need to apply these changes to your main
src/styles.scssfile manually to complete this migration. -
Inspect any changes the upgrade tool may have made to template files, and run
ng serveto test that your app is working as expected. -
Once you’re happy with the changes, you can delete the
tailwind.tmp.cssfile and commit your changes 🚀