Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
N

ng-tailwindcss

> 前端框架
Open source

A CLI tool for integrating Tailwind CSS into Angular-CLI projects

200 stars0 likes0 views
WebsiteGitHub

About

A CLI tool for integrating Tailwind CSS into Angular-CLI projects

ng-tailwindcss

A CLI tool for integrating Tailwind CSS into Angular-CLI projects with as little pain as possible

Read this article to learn more about how and why this works. You can also use it with other frameworks like React and Next.js!

Core Features:

  • PurgeCSS v1.4.2, ready to rock "out of the box", but also fully configurable

  • Sass support with optional dependency on node-sass or dart-sass

  • Default configurations reflect Tailwind v1 file naming conventions (such as tailwind.config.js)

  • Angular Workspaces (and other monorepo structures) Support (ngtw v2.2.0+)

  • Watch related files using watchRelatedFiles property of ng-tailwind.js config. This array of paths/globs will be watched for changes and trigger rebuilding of tailwind files.

  • "Hot reloading" of all watched files, including ng-tailwind.js. Any file change or renaming or reconfiguring will automatically be picked up by ngtw, no need to kill or restart your dev server!

Why Is This Necessary?

If you haven't used Tailwind CSS yet, you really should! However, if you are trying to use Tailwind in an Angular project, you will quickly realize that the best features of tailwind are found in the build process, which is conveniently automated using (mostly) postCSS plugins. Unfortunately, Angular currently does not offer developers access to the webpack configuration being used 'under the hood', so you're out of luck. Unless...

You use ng eject! (not available in v6+)

If you are ok with losing all of the benefits of angular-cli (not advisable given the improvements continually being made to it), you can reference this excellent SO answer or YouTube video and learn how to get at the postCSS controls. Using this method, you will certainly enjoy faster development speed when dealing with styles. But when you reach for ng generate, ng add, or ng upgrade etc., you may find that you'll have some regrets, because running ng eject is PERMANENT. I hope you enjoy creating components 'by hand.'

However, if you're into having cake and eating it too, ng-tailwindcss is what you've been looking for! With a few straightforward CLI commands, you can retain the development advantage of angular-cli and enjoy all the benefits of the best CSS utility framework on the web. Oh, and did we mention PurgeCSS???

Let's get down to business:

Quick and Dirty Setup

(Recommended for new projects only, see Configuration section for safe handling of existing projects)

After starting your new angular-cli project run these commands:

bash
npm i ng-tailwindcss -g
npm i tailwindcss -D
npx tailwind init # use --full, if you want to see all the defaults in your tailwind.config.js
ngtw configure
touch src/tailwind.css

Put all your tailwind imports in src/tailwind.css and run:

bash
ngtw scripts

Run npm start and let the wind fill your wings!


Full Installation and Usage Guide

  1. Install globally: npm i ng-tailwindcss -g

  2. If you don't already have an angular project up and running, start your angular project (assumes angular cli is already installed globally): ng new angular-meets-tailwind

  3. Follow Steps 1-3 from the Tailwind Installation Instructions:

    • Install Tailwind (npm i tailwindcss -D)
    • initialize (npx tailwind init)
    • Use tailwind in your source css files.

    A recommendation for new projects (no changes to global stylesheet yet) is to touch src/tailwind.css and use that file for all global styles and component classes. See Configuration section for existing projects.

  4. Configure your tailwind source/destination/config files by running:

    bash
    ngtw configure --config ./path/to/whatever-you-named-tailwind-config.js --source ./path/to/your-tailwind-source.css --output ./path/to/outputted-global-styles.css

    This will result in an ng-tailwind.js file at your project's root:

    javascript
    module.exports = {
      // Tailwind Paths
      configJS: '/Absolute/path/to/whatever-you-named-tailwind-config.js',
      sourceCSS: '/Absolute/path/to/your-tailwind-source.css',
      outputCSS: '/Absolute/path/to/outputted-global-styles.css',
      watchRelatedFiles: [],
      // Sass
      sass: false,
      // PurgeCSS Settings
      ...
    }

    Please note that as of version 1.0.3, these paths will be absolute when created using the cli tool, though they can be manually edited to be relative paths with no adverse consequences.

    For those curious, under the hood, these properties correspond to the paths used in the tailwind build command like so:

    bash
    ./node_modules/.bin/tailwind build {sourceCSS} -c {configJS} -o {outputCSS}
    # npx is not assumed by this project, to avoid worrying about it as a dependency

    See Configuration section for more details and implications for existing Angular projects

  5. Add or adjust these scripts in your package.json:

    json
    scripts: {
      "prestart": "ngtw build",
      "start": "ng serve & ngtw watch",
      "build": "ngtw build && ng build"
    }

    or simply run ngtw scripts to have these adjustments made automatically in your package.json.

    Now using npm start for your development server will ensure your tailwind files are being watched and built with your project, and you can still rely on the angular-cli for everything else (no ng eject! yay!).

  6. When you're ready to filter out your unused CSS, reference the documentation below for the various ways you can implement and adjust PurgeCSS. (Quick Tip: To include PurgeCSS in your build script, simply adjust the ngtw build command like so: ngtw build --purge.)

  7. Keep calm and angular on.


Configuration

The ng-tailwind.js file can be directly manipulated (in keeping with the tailwind way of doing things) after the initial configuration command has been run. Conversely, if you prefer the command line, running ngtw configure a second time will overwrite only the properties specified by the flags you include (e.g. ngtw configure -c ./new-tailwind-config.js will only change the configJS property, and retain the original values for sourceCSS and outputCSS).

Important: The default config (running ngtw configure with no arguments) will assume a configuration of:

javascript
{
  // Tailwind Paths
  configJS: './tailwind.config.js',
  sourceCSS: './src/tailwind.css',
  outputCSS: './src/styles.css',
  watchRelatedFiles: [],
  // Sass
  sass: false,
  // PurgeCSS Settings
  ...
}

Also important: these paths will actually be coerced to absolute paths. If you find this confusing, please open an issue, so the docs can be as clear as necessary.

It should be noted that such a configuration will set up your project to overwrite angular's default styles.css during each build, so if you desire to use the defaults in your existing project (recommended), you should remove any css from this file and place it in sourceCSS (the default being src/tailwind.css). If you are using styles.css as a source file (not really recommended), don't forget to edit your angular.json styles array to reflect your new global stylesheet (probably your outputCSS, but more complicated scenarios are certainly possible--be safe out there!).

Resetting to defaults

The --default flag can be included with the configure command at any time to overwrite all Tailwind Paths to the defaults (see below; PurgeCSS Settings will not change), with the exception of any other included flags when the command is run.

Example:

ng-tailwind.js (changed the file structure, needs an update)

javascript
module.exports = {
  // Tailwind Paths
  configJS: './some-tailwind-config.js',
  sourceCSS: './random/path/you/chose/tailwind.css',
  outputCSS: './way/different/location/of/styles.css'
  watchRelatedFiles: [],
  // Sass
  sass: false,
  // PurgeCSS Settings
  ...
}

bash script (this should fix it):

bash
ngtw configure --default -o ./src/my-groovy-styles.css

ng-tailwind.js (updated)

javascript
module.exports = {
  // Tailwind Paths
  configJS: './tailwind.config.js', // default config value
  sourceCSS: './src/tailwind.css', // default source value
  outputCSS: './src/my-groovy-styles.css' // -o (--output) overrides default
  watchRelatedFiles: [],
  // Sass
  sass: false,
  // PurgeCSS Settings
  ...
}

PurgeCSS Implementation Guide

How it Works

It is important to note that PurgeCSS will manipulate the output CSS file itself, directly.

For example:

  • ngtw build produces =>
  • styles.css file of ~300kb (all possible selectors; results may vary) =>
  • ngtw purge takes in that stylesheet and =>
  • rewrites styles.css file of 6kb (same file location, but with only a fraction of the original selectors, and no comments)

"Ah, but how does PurgeCSS know what selectors I'm using?" you ask.

ng-tailwindcss uses a custom extractor that is run against all .html and .ts files in the /src directory. You can edit your PurgeCSS configuration in the ng-tailwind.js file. Read the PurgeCSS Docs to see what is possible and how to maximize the configuration for your project.

How To Use It

When including PurgeCSS in your Angular/Tailwind magnum opus, there are 3 ways to execute the script:

  1. Lock It in at the Configuration Level

    This strategy ensures that PurgeCSS will clean up your Tailwind-generated global stylesheet every time Tailwind builds.

    Usage: ngtw configure --purge

    This configure flag sets {purge: true} in your ng-tailwind.js file. This property defaults to false, but, when true, it will not be overridden by any other CLI commands that initiate a build. **It is important to note that any PurgeCSS configuration options set to true in the ng-tailwind.js file will not be overridden by a CLI command.

    However, you can set {purge: false} at any time by manually editing the file (of course), or running ngtw c --unset-purge.

    Example: PurgeCSS All The Things

    bash
    ngtw configure --default --purge
    ngtw scripts
    
    # Even though the scripts command does not create any PurgeCSS scripts,
    # the configuration of {purge: true} in ng- will cause PurgeCSS
    # to run after every successful Tailwind build.
  2. Run It with the ngtw Build Command

    This is a slightly more manual approach, where you are telling PurgeCSS to run with a flag on the build command.

    Usage: ngtw build --purge.

    Obviously, this tells tailwind to rebuild your stylesheet, then PurgeCSS is immediately excecuted on the resulting output file (outputCSS in ng-tailwind.js) using the settings specified in ng-tailwind.js.

    Example: Production Build Script

    javascript
    "scripts": {
      "b-dev": "ngtw build && ng build", // dont purge

Issues· 12 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScriptangularclidevelopment-advantageng-tailwindcss

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架