How to set up Tailwind responsive variants for production build? - create-react-app

I set up responsive variants in my create-react-app app after setting up Craco according to Tailwind Docs. These work perfectly in development build but no images load in production build. What am I missing here?
I have determined the issue is most likely in my production configuration. When I "hard-code" any of the background-image URLs directly into my App.css file as a test, they show up correctly in production build.
index.css
#tailwind base;
#tailwind components;
#tailwind utilities;
#layer utilities {
#variants responsive {
.test-XS {
background-image: url('https://res.cloudinary.com/.../test-XS.png');
}
.test-SM {
background-image: url('https://res.cloudinary.com/.../test-SM.png');
}
.test-MD {
background-image: url('https://res.cloudinary.com/.../test-MD.png');
}
.test-LG {
background-image: url('https://res.cloudinary.com/.../test-LG.png');
}
.test-XL {
background-image: url('https://res.cloudinary.com/.../test-XL.png');
}
}
/* There are about 20 more sets of these for different images */
}
package.json :
{
"homepage": "http://xxx.github.io/yyy",
"name": "color-portfolio",
"version": "0.1.0",
"private": true,
"dependencies": {
"#craco/craco": "^6.1.1",
"#tailwindcss/postcss7-compat": "^2.0.2",
"#testing-library/jest-dom": "^5.11.9",
"#testing-library/react": "^11.2.5",
"#testing-library/user-event": "^12.6.3",
"autoprefixer": "^9.8.6",
"postcss": "^7.0.35",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.2",
"tailwindcss": "npm:#tailwindcss/postcss7-compat#^2.0.2",
"web-vitals": "^1.1.0"
},
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"start": "craco start",
"build": "craco build",
"test": "craco test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"gh-pages": "^3.1.0"
}
}
craco.config.js :
module.exports = {
style: {
postcss: {
plugins: [
require('tailwindcss'),
require('autoprefixer'),
],
},
},
}
tailwind.config.js :
module.exports = {
purge: ['./src/**/*.{js,jsx,ts,tsx,css}', './public/index.html'],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {},
},
variants: {
extend: {},
},
plugins: [],
}

The issue was in the concatenation I was using in my classNames.
It is covered in Tailwind's documentation here.
"Don't use string concatenation to create class names"
An example of what I was doing incorrectly:
<div className={`card bg-contain ${props.image}-XS sm:${props.image}-SM md:${props.image}-MD lg:${props.image}-LG xl:${props.image}-XL`}>
My workaround to get my project deployed for now: Changing it so that Tailwind does not "tree-shake" the utilities layer by specifying which layers to purge in config (more info in Tailwind Docs here):
tailwind.config.js :
module.exports = {
purge: {
layers: ['base', 'components'],
content: ['./src/**/*.{js,ts,tsx}', './public/index.html']
},
darkMode: false, // or 'media' or 'class'
theme: {
extend: {},
},
variants: {
extend: {},
},
plugins: [],
}
I am sure that this makes my build size unnecessarily large, but at least my project is deployed until I figured out a better way to incorporate these dynamic class variants.

Related

How to configure eslint and prettier with nuxt 3?

I have installed these dependencies
Package.json:
{
"devDependencies": {
"#intlify/nuxt3": "^0.1.6",
"#nuxtjs/eslint-config": "^7.0.0",
"#nuxtjs/eslint-module": "^3.0.2",
"eslint": "^8.1.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-nuxt": "^3.0.0",
"eslint-plugin-vue": "^7.20.0",
"nuxt3": "latest",
"prettier": "2.4.1",
"sass": "^1.43.3",
"vite-plugin-eslint": "^1.3.0"
}
}
At .eslintrc.js
extends: [
'eslint:recommended',
'plugin:nuxt/recommended',
'prettier'
],
At nuxt.config.ts
import eslintPlugin from 'vite-plugin-eslint';
export default defineNuxtConfig({
...
vite: {
plugins: [eslintPlugin()]
},
buildModules: ['#intlify/nuxt3', '#nuxtjs/eslint-module',],
})
And none of these options are working with nuxt 3.
A simple ESLint + Prettier + TypeScript + Nuxt 3 (or Bridge) setup would look like this:
yarn add --dev eslint prettier eslint-config-prettier eslint-plugin-prettier #nuxtjs/eslint-config-typescript
.eslintrc.js
module.exports = {
root: true,
extends: ['#nuxtjs/eslint-config-typescript', 'plugin:prettier/recommended'],
}
package.json
{
"scripts": {
"lint": "eslint --ext .ts,.js,.vue ."
}
}
Here's a config i found here : https://github.com/nuxt/framework/discussions/2815#discussioncomment-2050408
// .eslintrc.json
{
"env": {
"browser": true,
"es2021": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:vue/vue3-recommended",
"plugin:#typescript-eslint/recommended",
"#nuxtjs/eslint-config-typescript"
],
"parserOptions": {
"ecmaVersion": "latest",
"parser": "#typescript-eslint/parser",
"sourceType": "module"
},
"plugins": [
"vue",
"#typescript-eslint"
],
"rules": {}
}
If you really want to use prettier (imo eslint already does the job, using both can be very annoying to configure) you could add eslint-plugin-prettier library, then add "plugin:prettier/recommended" to eslint extends.
Idk what IDE you're using but if it's vscode, I advise you to use the linting on save instead of relying on formatters (Volar, prettier, eslint-prettier).
Mostly because it forces all devs to have the same format and rules

FUNCTION_INVOCATION_FAILED error on Vercel and Nuxt deployment

Imported a Nuxt project from GitHub with vercel.json config:
{
"version": 2,
"builds": [
{
"src": "nuxt.config.js",
"use": "#nuxtjs/vercel-builder"
}
]
}
My package.json:
{
"name": "test-app-v2",
"version": "1.0.0",
"private": true,
"scripts": {
"dev": "nuxt",
"build": "nuxt build",
"start": "nuxt start",
"generate": "nuxt generate",
"lint:js": "eslint --ext \".js,.vue\" --ignore-path .gitignore .",
"lint": "yarn lint:js"
},
"lint-staged": {
"*.{js,vue}": "eslint"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"dependencies": {
"#nuxtjs/composition-api": "^0.24.0",
"#nuxtjs/pwa": "^3.3.5",
"core-js": "^3.9.1",
"nuxt": "^2.15.3"
},
"devDependencies": {
"#nuxt/types": "^2.15.3",
"#nuxt/typescript-build": "^2.1.0",
"#nuxtjs/eslint-config-typescript": "^6.0.0",
"#nuxtjs/eslint-module": "^3.0.2",
"#nuxtjs/vuetify": "^1.11.3",
"babel-eslint": "^10.1.0",
"eslint": "^7.22.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-nuxt": "^2.0.0",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-vue": "^7.7.0",
"husky": "^4.3.8",
"lint-staged": "^10.5.4",
"prettier": "^2.2.1"
}
}
and nuxt.config.js
import colors from 'vuetify/es5/util/colors'
export default {
// Global page headers: https://go.nuxtjs.dev/config-head
head: {
titleTemplate: '%s - test-app-v2',
title: 'test-app-v2',
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ hid: 'description', name: 'description', content: '' },
],
link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }],
},
// Global CSS: https://go.nuxtjs.dev/config-css
css: [],
// Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
plugins: [],
// Auto import components: https://go.nuxtjs.dev/config-components
components: true,
// Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
buildModules: [
// https://go.nuxtjs.dev/typescript
'#nuxt/typescript-build',
// https://go.nuxtjs.dev/vuetify
'#nuxtjs/vuetify',
'#nuxtjs/composition-api/module',
],
// Modules: https://go.nuxtjs.dev/config-modules
modules: [
// https://go.nuxtjs.dev/pwa
'#nuxtjs/pwa',
],
// PWA module configuration: https://go.nuxtjs.dev/pwa
pwa: {
manifest: {
lang: 'en',
},
},
// Vuetify module configuration: https://go.nuxtjs.dev/config-vuetify
vuetify: {
customVariables: ['~/assets/variables.scss'],
theme: {
dark: false,
themes: {
dark: {
primary: colors.blue.darken2,
accent: colors.grey.darken3,
secondary: colors.amber.darken3,
info: colors.teal.lighten1,
warning: colors.amber.base,
error: colors.deepOrange.accent4,
success: colors.green.accent3,
},
},
},
},
// Build Configuration: https://go.nuxtjs.dev/config-build
build: {},
}
I have no API yet and this is the error log in function tab:
Wasn't the OPs aim to deploy it using SSR? Using build command yarn generate will just generate a static site.
EDIT: updating the build command fixed OP's issue.
Do you even need the vercel.json here?
I've tried to host one of my repo there and it's working fine with those settings only.
Hosted there: https://nuxt-tailwind-typography-darkmode-boilerplate.vercel.app/
Vercel pretty much self-detected that it was a Nuxt project and almost aced it itself.

Error: Module parse failed when using babel with webpack

When I'm trying to install babel to use it webpack and make the configuration, I face this error. I tried different versions from babel and webpack and I tried so many configurations, non of them worked and they keeping throw the same error.
Notice: I already have another plugins like webpack-server and webpack-html but I'm pretty sure they doesn't affect babel.
ERROR in ./node_modules/core-js/modules/es6.regexp.exec.js 1:3
Module parse failed: Unexpected character ' ' (1:3)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)
# ./node_modules/core-js/es6/index.js 102:0-37
# ./node_modules/#babel/polyfill/lib/noConflict.js
# ./node_modules/#babel/polyfill/lib/index.js
# multi #babel/polyfill ./src/js/index.js
.babelrc file:
// projectname/.babelrc
{
"presets": [
[ "#babel/preset-env", {
"modules": false,
"targets": {
"browsers": [
"last 2 Chrome versions",
"last 2 Firefox versions",
"last 2 Safari versions",
"last 2 iOS versions",
"last 1 Android version",
"last 1 ChromeAndroid version",
"ie 11"
]
}
} ]
]
}
webpack.config.js file:
// webpack.config.js
const path = require( 'path' );
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
context: __dirname,
entry: ['#babel/polyfill', './src/js/index.js'],
output: {
path: path.resolve( __dirname, 'dist' ),
filename: 'js/bundle.js',
},
devServer: {
contentBase: './dist'
},
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
template: './src/index.html'
})
],
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader'
}
]
}
};
package.json file:
{
"name": "forkify",
"version": "1.0.0",
"description": "forkify project",
"main": "index.js",
"scripts": {
"dev": "webpack --mode development",
"build": "webpack --mode production",
"start": "webpack-dev-server --mode development --open"
},
"author": "Ahmed Hossam",
"license": "ISC",
"devDependencies": {
"#babel/cli": "^7.6.4",
"#babel/core": "^7.6.4",
"#babel/preset-env": "^7.6.3",
"babel-loader": "^8.0.6",
"html-webpack-plugin": "^3.2.0",
"path": "^0.12.7",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.9",
"webpack-dev-server": "^3.9.0"
},
"dependencies": {
"#babel/polyfill": "^7.6.0"
}
}

Issue using extract-text-webpack-plugin

I am trying to use the Extract Text Webpack Plugin, but I have not been able to generate a new css file for the compiled sass files.
webpack.config.js
var ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
entry : './src/app.js',
output : {
filename : './dist/bundle.js'
},
module: {
rules: [
{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader','sass-loader']
})
}
]
},
plugins: [
new ExtractTextPlugin({
filename : 'app.bundle.css'
}),
]
}
package.json:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "webpack.config.js",
"scripts": {
"dev": "webpack-dev-server"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"css-loader": "^0.28.9",
"extract-text-webpack-plugin": "^3.0.2",
"node-sass": "^4.7.2",
"sass-loader": "^6.0.6",
"style-loader": "^0.20.1",
"webpack": "^3.11.0",
"webpack-dev-server": "^2.11.1"
}
}
app.js:
var css = require('../css/app.scss');
console.log("This is a test Hi from webpack !!!");
I able to generate a new css file for the compiled sass files
please go through repo for your reference working example
your configuration looks good only ,my be you miss the path or forgot load css file in index.html or may be you did not create any scss files
live example

ES6 Export with Babel

Clearly I'm missing something incredibly simple here, so I apologize in advance for the dumb question. I have no errors so it's difficult to Google.
I'm trying to export something, anything, from an npm package written in ES6, compiled with babel and webpack.
I followed this http://jamesknelson.com/using-es6-in-the-browser-with-babel-6-and-webpack/ for my webpack setup, leaving it mostly-identical, but find it below for reference. I made a test export repo just to make sure it wasn't anything in the code of the module I was trying to export; find that below as well. Any help would be greatly appreciated; at this point I feel like I'm taking crazy pills.
src/index.js
const test = "test";
export default test;
webpack.config.js
var path = require("path");
var webpack = require("webpack");
module.exports = {
entry: [
"babel-polyfill",
"./src/index"
],
output: {
//path: path.join(__dirname, "lib"),
//filename: "[name].js"
filename: "./lib/index.js"
},
// import bare, .js, and .jsx files
resolve: {
extensions: ["", ".js", ".jsx"]
},
devtool: "source-map",
module: {
loaders: [
{
loader: "babel-loader",
// only load src
include: [
path.resolve(__dirname, "src")
],
// only compile .js and .jsx files
test: /\.jsx?$/,
query: {
plugins: ["transform-runtime", "transform-decorators-legacy"],
//plugins: ["transform-decorators-legacy"],
presets: ["es2015", "stage-0", "react"]
}
},
]
},
debug: true
};
package.json
{
"name": "test-package",
"version": "0.0.1",
"description": "test",
"main": "lib/index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/xxx/xxx.git"
},
"scripts": {
"start": "webpack-dev-server"
},
"keywords": [
"es6"
],
"author": "me",
"license": "MIT",
"bugs": {
"url": "https://github.com/xxx/xxx/issues"
},
"homepage": "https://github.com/xxx/xxx#readme",
"dependencies": {
"babel-polyfill": "^6.5.0",
"babel-runtime": "^6.5.0"
},
"devDependencies": {
"babel-core": "^6.5.2",
"babel-loader": "^6.2.3",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-runtime": "^6.5.2",
"babel-preset-es2015": "^6.5.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "^6.5.0",
"webpack": "^1.12.14",
"webpack-dev-server": "^1.14.1"
}
}
webpack -p
other project
npm i ../test-package
(verify actually installed, search for "test" in lib/index.js and find what should be the export)
import test from "test-package";
console.log(test);
console.log(Object.keys(test));
output: empty object, empty array
Why not trying
import * as test from "test-package";
And then
console.log(test);