A CLI tool for integrating Tailwind CSS into Angular-CLI projects
A CLI tool for integrating Tailwind CSS into Angular-CLI projects
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
watchRelatedFilesproperty ofng-tailwind.jsconfig. 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 byngtw, no need to kill or restart your dev server!
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:
After starting your new angular-cli project run these commands:
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.cssPut all your tailwind imports in src/tailwind.css and run:
ngtw scriptsRun npm start and let the wind fill your wings!
Install globally:
npm i ng-tailwindcss -g
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
Follow Steps 1-3 from the Tailwind Installation Instructions:
npm i tailwindcss -D)npx tailwind init) 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.
Configure your tailwind source/destination/config files by running:
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:
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:
./node_modules/.bin/tailwind build {sourceCSS} -c {configJS} -o {outputCSS}
# npx is not assumed by this project, to avoid worrying about it as a dependencySee Configuration section for more details and implications for existing Angular projects
Add or adjust these scripts in your package.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!).
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.)
Keep calm and angular on.
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:
{
// 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!).
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)
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):
ngtw configure --default -o ./src/my-groovy-styles.cssng-tailwind.js (updated)
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
...
}It is important to note that PurgeCSS will manipulate the output CSS file itself, directly.
For example:
ngtw build produces =>ngtw purge takes in that stylesheet and =>"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.
When including PurgeCSS in your Angular/Tailwind magnum opus, there are 3 ways to execute the script:
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
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.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
"scripts": {
"b-dev": "ngtw build && ng build", // dont purgeNo open issues yet, or sync has not completed.