TailwindCSS - Applied styles removed in production - css

[UPDATE 28-9-2021]
What I also did to eliminate other causes: Update OS packages (Centos 8), rebuilt Apache / NGINX, rebuilt NPM modules, updated and rebuilt TailwindCSS, tried default config files / json files, but all these didn't give me any result :-( I'm really stuck here
[/UPDATE]
Maybe I'm overlooking something here.
I added some basic styling to my style.css for headings:
#tailwind base;
#tailwind components;
#tailwind utilities;
#layer base {
p {
#apply text-theme-gray-lighter pb-4;
}
h1 {
#apply text-3xl py-2;
}
h2 {
#apply text-2xl py-2 border-b mb-2;
}
h3 {
#apply text-xl py-1 font-semibold text-theme-blue;
}
h4 {
#apply text-lg font-semibold;
}
h3.price {
#apply text-theme-gray;
}
.boks-bold {
#apply font-semibold p-4 px-6 text-lg m-4 my-6 mt-2 border shadow-md rounded;
}
}
This file is located at the root of my project.
This is my tailwind.config.js:
module.exports = {
variants: {
extend: {
grayscale: ['hover', 'focus'],
backgroundColor: ['checked'],
borderColor: ['checked'],
borderColor: ['focus-within'],
}
},
darkMode: 'media',
purge: ["*.jsx", "*.js", "*.php"],
plugins: [
// require('daisyui'),
// require('#tailwindcss/forms'),
require('#tailwindcss/custom-forms'),
],
daisyui: {
// styled: false,
// themes: false,
// rtl: false,
},
theme: {
customForms: theme => ({
default: {
'input, textarea, multiselect, checkbox, radio': {
borderdColor: theme('colors.gray.200'),
},
},
}),
container: {
padding: '1rem',
center: true,
},
extend: {
width: {
'full-150': '150vw',
'full-200': '200vw',
},
height: {
'18': '4.5rem',
'40rem': '40rem',
'730px': '730px',
'650px': '650px',
'1191px': '1191px',
'901px': '901px',
'722px': '722px',
'110perc': '110%',
},
maxHeight: {
'110perc': '110%',
},
zIndex: {
'-10': '-10',
},
borderWidth: {
'12': '12px',
},
colors: {
'theme-brown': '#BF8A23',
'theme-yellow': '#FAA71B',
'theme-brown-brighter': '#DDB15B',
'theme-gray': '#333333',
'theme-gray-lighter': '#444444',
'theme-blue': '#547A82',
},
minWidth: {
'0': '0',
'1/4': '25%',
'1/2': '50%',
'3/4': '75%',
'full': '100%',
},
animation: {
'scroll-rtl-slow': 'scrollbrand scroll 40s linear infinite',
},
keyframes: {
scrollbrand: {
'0%': { transform: 'translateX(0)' },
'100%': { transform: 'translateX(calc(-250px * 7))',
}
}
}
}
}
And this is my package.json:
{
"dependencies": {
"#tailwindcss/forms": "^0.3.3",
"#tailwindcss/typography": "^0.4.1",
"autoprefixer": "^10.3.1",
"daisyui": "^1.10.0",
"node-cron": "2.0.3",
"postcss": "^8.3.6",
"postcss-cli": "^8.3.1",
"tailwindcss": "^2.2.6",
"vue-tailwind": "^2.4.2"
},
"scripts": {
"build-css": "tailwindcss build -i style.css -o css/style.css",
"build": "tailwindcss style.css -o css/style.css",
"production": "cross-env NODE_ENV=production tailwindcss css/style.css -o css/style.min.css"
},
"name": "templateh.sceneryworkshop.com",
"version": "1.0.0",
"main": "tailwind.config.js",
"devDependencies": {
"#tailwindcss/custom-forms": "^0.2.1",
"cross-env": "^7.0.3",
"webpack": "^5.52.1",
"webpack-cli": "^4.8.0"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": ""
}
To get the #apply working, I had to add -i to the command:
*"build-css": "tailwindcss build -i style.css -o css/style.css"*
But when I do the same when building production:
"production": "cross-env NODE_ENV=production tailwindcss -i css/style.css -o css/style.min.css"
The file doesn't get purged at all, so it keeps it's total size.
Anyone who has an idea what I'm doing wrong here?

Make sure to add path to your config file in your command:
"production": "cross-env NODE_ENV=production tailwindcss -c ./tailwind.config.js -i css/style.css -o css/style.min.css"

Related

What do I put in the content section of module exports in "tailwind.config.js"?

I can't get Tailwind to work and I think it's because my configuration file is wrong?
/** #type {import('tailwindcss').Config} */
module.exports = {
content: ["./public/index.html", "./src/**/*.{vue,js,ts,jsx,tsx}"],
theme: {
extend: {},
},
plugins: [],
};
I installed Tailwind and used init:
https://imgur.com/nGWspSt
I filled in the "tailwind.config.js" like this:
tailwind.config.js
/** #type {import('tailwindcss').Config} */
module.exports = {
content: ["./src/components/DropdownVue/DropdownVue.vue"], <---INCLUDED DROPDOWN FILE
theme: {
extend: {},
},
plugins: [],
};
I created an "index.css" and imported the Tailwind modules in it like this:
src/index.css
#tailwind base;
#tailwind components; <-----INCLUDED IMPORTS
#tailwind utilities;
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: "Noto Serif", serif, Ariel, Helvetica, sans-serif;
background: #d7d7d7;
}
img {
width: 100%;
object-fit: contain;
}
#import url("https://fonts.googleapis.com/css2?family=Noto+Serif&display=swap");
I started the build process and got an "output.css" file
https://i.imgur.com/mOKDwuc.png
that I included in my "index.html" file like this:
public/index.html
<link href="/dist/output.css" rel="stylesheet">
At this point it should have been working but it wasn't. I realized my config file was wrong so I changed it to:
/** #type {import('tailwindcss').Config} */
module.exports = {
content: ["./public/index.html", "./src/**/*.{vue,js,ts,jsx,tsx}"],
theme: {
extend: {},
},
plugins: [],
};
but it's still not working. Do I have to run the build process again because I changed the config file?
What am I doing wrong?
EDIT:
package.json
{
"name": "stripe-payment-vue3",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
"dependencies": {
"#fortawesome/fontawesome-svg-core": "^6.2.1",
"#fortawesome/free-brands-svg-icons": "^6.2.1",
"#fortawesome/free-regular-svg-icons": "^6.2.1",
"#fortawesome/free-solid-svg-icons": "^6.2.1",
"#fortawesome/vue-fontawesome": "^3.0.2",
"#headlessui/vue": "^1.7.9",
"#heroicons/vue": "^2.0.14",
"core-js": "^3.8.3",
"vue": "^3.2.13",
"vue-router": "^4.1.6",
"vuex": "^4.0.2"
},
"devDependencies": {
"#babel/core": "^7.12.16",
"#babel/eslint-parser": "^7.12.16",
"#vue/cli-plugin-babel": "~5.0.0",
"#vue/cli-plugin-eslint": "~5.0.0",
"#vue/cli-service": "~5.0.0",
"autoprefixer": "^10.4.13",
"eslint": "^7.32.0",
"eslint-plugin-vue": "^8.0.3",
"postcss": "^8.4.21",
"tailwindcss": "^3.2.4"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/vue3-essential",
"eslint:recommended"
],
"parserOptions": {
"parser": "#babel/eslint-parser"
},
"rules": {}
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead",
"not ie 11"
]
}

Tailwind: modify classes dynamically for Vue app

I have a Vue3 application with Tailwinds configured in tailwind.config.js
Is it possible to dynamically change the value of a preconfigured class from tailwind.config.js ?
For example:
tailwind.config.js:
const defaultTheme = require("tailwindcss/defaultTheme");
module.exports = {
purge: ["./index.html", "./src/**/*.{vue,js,ts,jsx,tsx}"],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {
colors: {
base: {
DEFAULT: "#6e0147",
},
},
fontFamily: {
sans: ["Interstate", "Inter var", ...defaultTheme.fontFamily.sans],
},
},
},
variants: {
extend: {
ringWidth: ["hover", "active"],
ringColor: ["hover", "active"],
ringOpacity: ["hover", "active"],
ringOffsetWidth: ["hover", "active"],
ringOffsetColor: ["hover", "active"],
},
},
plugins: [],
};
VueComponent.vue :
<template>
<div class="text-base">text here</div>
</template>
<script>
export default {
.....
mounted(){
tailwind.config.colors.base = "#00000" // change tailwind color of class somehow
}
}
</script>
In your tailwind.css add a CSS variable called --text-color-base (you could add multiple) in the base theme and also in theme-1:
#tailwind base;
#tailwind components;
#tailwind utilities;
#layer base{
:root{
--text-color-base:#6e0147;
}
.theme-1{
--text-color-base:#000000;
}
}
in tailwind.config.js extend the textColor field in the theme option with skin key which will contain the different variable for your text color :
theme: {
extend: {
textColor:{
skin:{
base:"var(--text-color-base)"
}
},
colors: {
base: {
DEFAULT: "#6e0147",
},
},
fontFamily: {
sans: ["Interstate", "Inter var", ...defaultTheme.fontFamily.sans],
},
},
then you could use it like class="text-skin-base", to apply the theme-1 add the class theme-1 to the root element like :
<div class="theme-1">
<h1 class="text-skin-base">text here</h1>
...
</div>
then in your script you could bind the root class to a property and you update in the script :
<div :class="myTheme">
<h1 class="text-skin-base">text here</h1>
...
</div>
<script>
export default {
.....
data(){
return{
myTheme:''
}
},
mounted(){
this.myTheme="theme-1"
}
}
</script>

GRUNT: grunt-postcss not executing cssnano when used together with sass - if scss file long

I am using both the sass parser and gruntpostcss with cssnano on watch. cssnano` is parsing the file just fine if its a short file, but it my scss/css file gets long css file, the it is not parsing the file. Anyone knows why that is and how to fix it?
Here the package.json, Gruntfile.js and both a short-css-file.css which works and a long-css-file.css where the cssnano is not parsing it.
package.json
{
"name": "testpage",
"version": "1.0.0",
"description": "testpage grunt",
"main": "Gruntfile.js",
"dependencies": {},
"devDependencies": {
"cssnano": "^3.10.0",
"grunt": "^1.0.3",
"grunt-contrib-watch": "^1.0.0",
"grunt-postcss": "^0.8.0",
"load-grunt-tasks": "^3.5.2"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
watch: {
css: {
files: 'wp-content/themes/mytheme/assets/css/*.scss',
tasks: ['sass', 'postcss']
},
},
sass: {
options: {
sourceMap: true
},
dist: {
files: {
'wp-content/themes/mytheme/assets/css/default.css': 'wp-content/themes/mytheme/assets/css/default.scss'
}
}
},
postcss: {
options: {
map: {
inline: false, // save all sourcemaps as separate files...
sourcesContent: true,
},
processors: [
require('autoprefixer')({browsers: ['last 2 versions', 'ie 8', 'ie 9']}), // add vendor prefixes
require('cssnano')({zindex: false}) // minify the result
]
},
dist: {
files: {
'wp-content/themes/mytheme/assets/css/default.css': 'wp-content/themes/mytheme/assets/css/default.css'
}
}
},
});
require('load-grunt-tasks')(grunt);
grunt.registerTask('default', ['watch', 'sass', 'postcss']);
};
short-css-file.css
// Test
.css_nano, .css_nano + p, [class*="css_nano"], .css_nano {
/* cssnano will remove this comment */
display: flex;
font-weight: normal;
margin-top: 1rem;
margin-bottom: 2rem;
margin-left: 1.5rem;
margin-right: 2.5rem;
font-weight: normal;
padding: 1.75rem;
width: calc(50rem - (2 * 1.75rem));
}
long-css-file.css see in the jsfiddle
https://jsfiddle.net/12p3Lxcn/

Nested syntax with PostCSS for :hover and :focus

I'm using TailwindCSS and PostCSS and I have this css code:
.btn {
#apply py-1;
#apply px-4;
#apply border;
#apply rounded;
}
.btn:hover {
#apply bg-white text-black;
}
.btn:focus {
#apply bg-black text-white;
}
Is there a native way in PostCSS (or with a plugin) to write this code like the below?
.btn {
#apply py-1;
#apply px-4;
#apply border;
#apply rounded;
&:hover {
#apply bg-white text-black;
}
&:focus {
#apply bg-black text-white;
}
}
Use postcss-preset-env.
First install, npm install postcss-preset-env --save-dev.
Then enable nesting-rules in your postcss.config.js file,
module.exports = {
plugins: [
"tailwindcss",
[
"postcss-preset-env",
{
stage: 3,
features: {
"nesting-rules": true,
},
},
],
],
};
Here you can find the list of features ID that can be enabled
You could also use postcss-nested plugin.
In your package.json:
{
"dependencies": {
"postcss": "^8.2.9",
"tailwindcss": "^2.0.4",
"postcss-nested": "^5.0.5"
}
}
In your postcss.config.js:
module.exports = {
plugins: [
require('postcss-nested'),
require('tailwindcss'),
]
}
Works too, following the object notation Reference
module.exports = {
plugins: {
'postcss-import': {},
tailwindcss: {},
autoprefixer: {},
'postcss-preset-env': { stage: 2 },
},
}

React antdesign #media query not working

I'm working with react ant-design and I'm using #media queries for styling but it keeps throwing the error given below
#btn {
^
Media definitions require block statements after any features
in /home/osh/WebstormProjects/ant-design-kickstart/src/styles/styles.less (line 69, column 8)
# ./src/styles/styles.less 2:14-142 21:1-42:3 22:19-147
# ./src/index.js
# multi (webpack)-dev-server/client?http://localhost:8081 (webpack)/hot/dev-server.js react-hot-loader/patch ./src/index.js
Failed to compile.
I have given the related files below
------------styles.less file---------------
#media only screen and (max-width: 768px) {
#btn {
width: 50%;
margin-top: 200px;
align-self: center;
}
Button {
width: 1000%;
marginBottom: '25px'
}
Input {
align-self: center;
width: 250px;
}
h4 {
color: #5400ff;
}
.appDashCard {
background: #fff;
padding: 10%;
/*//minHeight: 280px;*/
align-content: center;
}
}
#media only screen and (min-width: 768px) {
#btn {
width: inherit;
margin-right: 30px;
margin-left: 400px;
margin-top: 30px;
}
h4 {
color: #ff2600;
}
.appDashCard {
background: #fff;
padding: 24px;
minHeight: 280px;
}
.selectBox {
width: 100%;
}
}
----------package.json----------
{
"name": "ant-design-kickstart",
"version": "1.0.0",
"description": "An opinionated starter-kit for Ant Design",
"scripts": {
"start": "webpack-dev-server --config ./webpack.dev.js --mode development",
"build": "webpack --config ./webpack.prod.js --mode production",
"precommit": "pretty-quick --staged"
},
"author": "Madusha Prasanjith",
"license": "MIT",
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.4",
"babel-plugin-import": "^1.6.7",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"css-loader": "^0.28.11",
"file-loader": "^1.1.11",
"husky": "^0.14.3",
"less": "^3.0.1",
"less-loader": "^4.1.0",
"less-vars-to-js": "^1.2.1",
"prettier": "1.11.1",
"pretty-quick": "^1.4.1",
"react-hot-loader": "^4.0.0",
"style-loader": "^0.20.3",
"url-loader": "^1.0.1",
"webpack": "^4.3.0",
"webpack-cli": "^2.0.13",
"webpack-dev-server": "^3.1.5"
},
"dependencies": {
"antd": "^3.3.3",
"axios": "^0.18.0",
"media-queries": "^1.0.5",
"moment": "^2.21.0",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-router-dom": "^4.2.2",
"style-loader": "^0.20.3"
}
}
-----------webpack.dev.js----------
const webpack = require("webpack");
const path = require("path");
const fs = require("fs");
const lessToJs = require("less-vars-to-js");
const themeVariables = lessToJs(
// fs.readFileSync(path.join(__dirname, "./src/styles/ant-default-vars.less"), "utf8")
fs.readFileSync(path.join(__dirname, "./src/styles/styles.less"), "utf8")
);
module.exports = {
entry: ["react-hot-loader/patch", "./src/index.js"],
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ["babel-loader"]
},
{
test: /\.less$/,
use: [
{
loader: "style-loader"
},
{
loader: "css-loader",
options: {
sourceMap: true
}
},
{
loader: "less-loader",
options: {
sourceMap: true,
modifyVars: themeVariables,
javascriptEnabled: true
}
}
]
},
{
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
loader: "url-loader",
options: {
limit: 10000
}
}
]
},
resolve: {
extensions: ["*", ".js", ".jsx"]
},
output: {
path: __dirname + "/dist",
publicPath: "/",
filename: "bundle.js"
},
plugins: [new webpack.HotModuleReplacementPlugin()],
devServer: {
contentBase: "./dist",
hot: true
}
};
This often indicates a syntax error. Please take a look at 'Button'. It has a property called 'marginBottom', rename it to margin-bottom, please also add a semicolon after the statement (although that shouldn't make a difference). Also, you should rename 'Button' to 'button' and 'Input' to 'input'.

Resources