Tailwind CLI's --watch not working properly - tailwind-css

Each time I add a new class to the index.html file, I need to rebuild the output.css file manually.
The package.json file:
{
"name": "tailwind-practice",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "npx tailwindcss --watch -i ./input.css -o ./output.css"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"tailwindcss": "^3.1.7"
}
}
The tailwind.config.js file:
/** #type {import('tailwindcss').Config} */
module.exports = {
screens: {
'sm': '480px',
'md': '768px',
'lg': '1440px',
},
content: ['./index.html'],
theme: {
extend: {},
},
plugins: [],
}
I am supposed to run npm run build once, and each time I saved the html file, tailwind is supposed to add my new classes to output.css. But it doesn't. I checked the file after saving index.html and I couldn't find my new classes there. But the CLI shamelessly said it rebuilt it in 20ms. I needed to run npm run build each time to successfully build my css file. Also, I deleted my previous nodejs installation and reinstalled the current version, updated VS Code, updated Google Chrome, and now, I am considering moving back to Windows from Manjaro.
Edit: A useful observation.
After saving index.html, the CLI said this:
Rebuilding...
Done in 27ms.
But when I stopped the process and reran npm run build, it said:
Rebuilding...
Done in 198ms.
There is a relatively huge time delay when it actually works.
Edit 2:
It works when I save index.html multiple times rapidly.
I moved from Manjaro to Ubuntu, and it still doesn't work!

You need to add these to your input.css to make the --watch work properly.
#tailwind base;
#tailwind components;
#tailwind utilities;
Without these, TW won't know from where it should pull info for your CSS classes and write a processed version in output.css
Let's say you wrote <h1 class="underline">Hello world! </h1> in index.html.
Tw needs to look for information on how it can parse + process the underline class and what should be written in output.css regarding this class.
Try it and let me know if it works.

try changing
from
npx tailwindcss --watch -i ./input.css -o ./output.css
to
npx tailwindcss -i ./input.css -o ./output.css --watch

Assuming this is how you think Tailwind CLI works:
This is your input.css:
.aaa {
color: blue;
}
This is your index.html:
...
<div class="aaa">AAA</div>
You execute npm run build
You go to index.html and add an HTML element with some Tailwind class
...
<div class="aaa">AAA</div>
<div class="font-bold">BBB</div>
You expect your output.css to be magically built into this:
.aaa {
color: blue;
}
.font-bold {
font-weight: 700;
}
This is how Tailwind CLI actually works:
In your input.css you have to first specify which Tailwind layers you think you will use in your index.html. Let's add a layer called utilities which enables you to use Tailwind classes such as font-bold, underline, etc.
#tailwind utilities;
.aaa {
color: blue;
}
You execute npm run build
You go to index.html and add an HTML element with some Tailwind class which belongs to the utilities layer
...
<div class="aaa">AAA</div>
<div class="font-bold">BBB</div>
You go to output.css et voila - the Tailwind class is there:
.font-bold {
font-weight: 700;
}
.aaa {
color: blue;
}
You add another Tailwind class from the utilities layer
...
<div class="aaa">AAA</div>
<div class="font-bold underline">BBB</div>
et voila:
.font-bold {
font-weight: 700;
}
.underline {
-webkit-text-decoration-line: underline;
text-decoration-line: underline;
}
.aaa {
color: blue;
}

Related

No utility classes were detected in your source files ,double-check the `content`

I am trying to setup tailwind 3 , but i got the next warning .
No utility classes were detected in your source files. If this is unexpected, double-check the `content` option in your Tailwind CSS configuration.
this is my project structure
|_public :
|_index.html ,
|_output.css // this css file generated after i run the command | npx tailwindcss -i ./src/input.css -o ./public/output.css --watch
|_src
|_input.css
tailwind.config.js
/** #type {import('tailwindcss').Config} */
module.exports = {
content: ["./src/**/*.{html,js}"],
theme: {
extend: {},
},
plugins: [],
}
According to your code, your html files are in the src folder (and sub-folders)
Put your html files in the public folder and properly link it with output.css
Then run npx tailwindcss -i ./src/input.css -o ./public/output.css --watch
You can also clone the Tailwind boilerplate I made using
git clone https://github.com/abrahamebij/tailwind-boilerplate
Then npm install and npm run css
This is worked for me. in tailwind.config.js, change your folder name "src" to "public" or you need to put your files to "src" folder
Why does this error occur ?
When specified content path in the tailwind.config.css doesn't have any html/js file
OR
if you are not using tailwind-css classes in your html file
module.exports = {
content: ["./src/**/*.{html,js}"], 👈 Your html and js files which is users of tailwind classes
theme: {
extend: {},
},
plugins: [],
};
This states I want to use the tailwind classes for html/js files under src directory. But src doesn't have any html/js file.
Solution:
Change content in tailwind.config.css to have right path
Have html/js files in the specified directory.
Extra : Proper approach to follow when using Tailwind-CLI
1. Know about your file structure. Use:
public
|_ tailwind_base.css
👆 File from which the output.css is produced
#tailwind base;
#tailwind components;
#tailwind utilities;
|_ output.css
src
|_ index.html
👆 Link with the output.css using
<link href="../public/output.css" rel="stylesheet" />
Watch it as
npx tailwindcss -i ./public/tailwind_base.css -o ./public/output.css --watch
Specify your html/js in tailwind.config.js
module.exports = {
content: ["./src/**/*.{html,js}"],
theme: {
extend: {},
},
plugins: [],
};
index.html file
<html lang="en">
<head>
<link href="../public/output.css" rel="stylesheet" />
</head>
<body class="text-8xl">
Hello
</body>
</html>
Use tailwindcss classes happily in your index.html file 😇

PostCSS or Parcel not compiling tailwind CSS

I am currently trying to create a web application, using TailwindCSS for styles, PostCSS as the processor and parcel-bundler as a bundler, based off this boilerplate from GitHub.
For some reason, every time I run yarn build, everything compiles right except for the CSS. The *.css file in dist/ is completely unchanged from the source file, and it seems like the file is not being processed at all...
My package.json
{
"license": "MIT",
"scripts": {
"dev": "parcel src/index.html",
"build": "parcel build --no-minify src/index.html"
},
"devDependencies": {
"#fullhuman/postcss-purgecss": "^4.0.3",
"autoprefixer": "^9",
"parcel-bundler": "^1.12.5",
"postcss": "^7",
"tailwindcss": "npm:#tailwindcss/postcss7-compat"
},
"dependencies": {
"#tailwindcss/forms": "^0.3.4",
}
}
main.css (CSS source file, *.css file in dist/ looks exactly like this)
/* purgecss start ignore */
#tailwind base;
#tailwind components;
#tailwind utilities;
/* purgecss end ignore */
.btn-orange {
#apply bg-gray-200 w-28 p-2 text-sm rounded hover:bg-yellow-500 transition-colors shadow-lg font-bold;
}
label {
#apply font-bold;
}
I have no idea what I'm doing wrong... I have used #tailwind instead of #import as suggested by many posts online, but for some reason the file is still not being processed. I am not sure if it is something wrong with PostCSS, or Parcel.

how to use sass using in vuejs3/vite

I starting with vite / vuejs 3
after installing sass with npm install -D sass I tried to add my _variables.js file this to vite.config.js
css: {preprocessorOptions: {scss: {additionalData: `#import" ./src/css/_variables.scss ";`,},},},
din't work!
it also worked in vue.config
css: {
loaderOptions: {
sass: {
sassOptions: {
prependData: `#import" # / css / _variables.scss ";`,
}
}
}
},
after this tried to import the file in main.js import "./css/_variables.scss"
Unfortunately, my components cannot find the variables, where is the error
Vite is a litte bit different
import { defineConfig } from 'vite'
export default defineConfig({
css: {
preprocessorOptions: {
scss: {
additionalData: `
#import "./src/styles/_animations.scss";
#import "./src/styles/_variables.scss";
#import "./src/styles/_mixins.scss";
#import "./src/styles/_helpers.scss";
`
}
}
}
})
here is my working setup, got it from the vite docs and with the command yarn add -D sass
// package.json
{
"dependencies": {
...
"vue": "^3.0.5"
},
"devDependencies": {
...
"sass": "^1.32.11",
"vite": "^2.2.3"
}
}
// vite.config.js
import { defineConfig } from 'vite'
import vue from '#vitejs/plugin-vue'
export default defineConfig({
plugins: [vue()]
})
// App.vue
<template>...</template>
<script>...</script>
<style lang="scss">
#import "./assets/style.scss";
</style>
// style.scss
[data-theme="dark"] {
--bg-color1: #121416;
--font-color: #f4f4f4;
}
[data-theme="light"] {
--bg-color1: #f4f4f4;
--font-color: #121416;
}
...
body {
background-color: var(--bg-color1);
color: var(--font-color);
}
and my vue.config.js is clean - nothing fancy there.
For Vue3, as stated in the documentation: https://vitejs.dev/guide/features.html#css-pre-processors
You only need to add
npm add -D sass
And in the script tag use
<style lang="scss">
// OR
<style lang="sass">
My problem was the following: I coudn't get scss variables within <style lang="scss"> and now this is my good config:
// vite.config.ts
export default defineConfig({
plugins: [
vue(),
...
],
...,
css: {
preprocessorOptions: {
scss: {
additionalData: ` // just variables loaded globally
#import "./src/assets/styles/setup/fonts";
#import "./src/assets/styles/setup/colors";
#import "./src/assets/styles/setup/mixins";
`
}
}
}
});
// App.vue
<style lang="scss"> // the main file that imports everything related with styles
#import "#/assets/styles/main.scss";
</style>
I had the same problem and simply solved it by installing sass as a Dev dependency.
npm i -D sass
Solutions:
"npm add -D sass" ---------- (-g flag will not work in vite now 16/02/2022).
Or,
"yarn add -D sass" - Run these comments in your command line.
In main.js file "import './style.scss' " will not work, so I added my sass file in html
If already install sass in your react.js app. Uninstall it.
Once Uninstall is done Check your packge.json file. It's will look like that.
"devDependencies": {
"#types/react": "^18.0.17",
"#types/react-dom": "^18.0.6",
"#vitejs/plugin-react": "^2.1.0",
"vite": "^3.1.0" }
Then Ren this command
npm cache clean --force or npm cache clean -f
Close your Vscode and Re-open it.
Now Reinstall sass in your react vite app.
# .scss and .sass
npm add -D sass
# .less
npm add -D less
# .styl and .stylus
npm add -D stylus
Once Installation done your json file look like that.
"devDependencies": {
"#types/react": "^18.0.17",
"#types/react-dom": "^18.0.6",
"#vitejs/plugin-react": "^2.1.0",
"sass": "^1.55.0",
"vite": "^3.1.0"
}

Add SCSS support to Vue project

In my packages.json file by default I get:
"postcss": {
"plugins": {
"autoprefixer": {}
}}
When I add <style lang='scss'> It doesn't compile like magic like it does for Typescript support. I know I will need to specify some NPM package as devDependencies and specify something above in the postcss section to get scss to compile, but I can't find any documentation outside of webpack so I am lost.
See https://vue-loader.vuejs.org/guide/pre-processors.html.
For example, to compile our <style> tag with SASS/SCSS:
npm install -D sass-loader node-sass
In your webpack config:
module.exports = {
module: {
rules: [
// ... other rules omitted
// this will apply to both plain `.scss` files
// AND `<style lang="scss">` blocks in `.vue` files
{
test: /\.scss$/,
use: [
'vue-style-loader',
'css-loader',
'sass-loader'
]
}
]
},
// plugin omitted
}
Now in addition to being able to import 'style.scss', we can use SCSS
in Vue components as well:
<style lang="scss"> /* write SCSS here */ </style>
Any content inside the block will be processed by webpack as if it's
inside a *.scss file.

How are `postcss-import` configured plugins applied

I've just started using PostCSS exclusively with Webpack. When using postcss-import to inline external stylesheets, I see it's options allow us to configure plugins and transformers to be applied on imported sources, but I'm a bit confused on how this fits in together with other options configured for the main PostCSS runner.
For instance, if I want to inline URLs, should I be adding the postcss-url plugin to postcss-import, the PostCSS runner or both (if my main stylesheet also has URL references)?
It's recommended to make postcss-import the first plugin in your list when you're defining the plugins for postcss in webpack. Since postcss-import just inlines the #import to the start of the file, any postcss plugin defined afterwards will be applied to it.
Example:
(For the example i'm gonna assume you use a postcss.config.js file, the same logic applies if you use an array for the plugins in the webpack 1 format)
// Header.css
#import 'button.css';
.foo {
font-size: 3rem;
transform:translateY(-10px);
}
// Button.css
.bar {
transform:translateX(20px);
}
If the import plugin is behind autoprefixer, it will first apply the autoprefixer plugin on the file and then afterwards import the #import file. So by the time the file is imported the prefixing will have already happened, the output will be:
// postcss.config.js
module.exports = {
plugins: {
'autoprefixer': {},
'postcss-import': {}
},
};
// output.css
.bar {
transform: translateX(20px); // Prefixing hasn't happened on the imported file
}
.foo {
font-size: 3rem;
transform:translateY(-10px);
-webkit-transform:translateY(-10px); // original file has been prefixed though
}
If you put the import first though, it will inline the imported file and then do the autoprefixing, this means both the imported and the original file will be autoprefixed:
// postcss.config.js
module.exports = {
plugins: {
'postcss-import': {},
'autoprefixer': {}
},
};
// output.css
.bar {
transform: translateX(20px);
-webkit-transform:translateX(20px); // Also prefixed now
}
.foo {
font-size: 3rem;
transform:translateY(-10px);
-webkit-transform:translateY(-10px);
}
So this means you don't actually have to add plugins again in the option of the postcss-import plugin.

Resources