Utilizing chokidar with JavaScript, I created a solution to automatically build my tailwind files whenever changes are made since Angular (up to version 6.0.3) does not currently provide access to postcss plugins in CLI projects.
Here is the snippet from chokidar.js located at the top-level next to package.json:
const chokidar = require('chokidar')
const child = require('child_process')
const tailwind = chokidar.watch(['tailwind.js', './src/tailwind.css'])
tailwind.on('change', (event, path) => {
child.exec('npm run build-tailwind')
console.log('Reprocessing Tailwind Files')
})
Snippet from package.json scripts:
"scripts": {
"ng": "ng",
"build-tailwind": "./node_modules/.bin/tailwind build ./src/tailwind.css -c ./tailwind.js -o ./src/styles.css",
"prestart": "npm run build-tailwind",
"start": "ng serve & node chokidar.js",
"build": "npm run prestart && ng build"
}
In the build-tailwind
script, I placed tailwind.css
in the src/
folder alongside styles.css
for all custom CSS implementations within the tailwind project setup.
The contents of tailwind.css:
@tailwind preflight;
/* custom components */
@tailwind utilities;
/* custom utilities */
This workaround can be beneficial until Angular integrates direct support for post-css functionality.
UPDATE:
To simplify this process for others looking to leverage tailwind capabilities in Angular projects, I developed a CLI tool available on npm:
https://www.npmjs.com/package/ng-tailwindcss