What is the best way to minify and concatenate multiple CSS files only after converting SCSS to CSS with Gulp?

When attempting to convert my scss files to css files using gulp, I encountered an issue. After writing the scss code, it is easily converted to css. However, I wanted to minify this css and save it in a different folder called 'min'. Within the 'min' folder, there are other css files as well. I then attempted to concatenate these css files into a single file and save it in the 'dist' folder. Everything seemed to be functioning correctly during the gulp run. However, when I used the concatenated css file on my html page, not all the css styles were rendering. Upon checking the compiled css from scss, I noticed that the latest written code was present there but not in the minified and concatenated file. It turns out that the minification and concatenation processes were being performed first, while the scss compiling was done last. This led to inconsistencies in the final css code. Is there a way to correct this issue?

This is an example of my gulp code:

var gulp = require("gulp");
var sass = require("gulp-sass");
const cleanCSS = require('gulp-clean-css');
var concat = require('gulp-concat');
var browserSync = require('browser-sync').create();

gulp.task('sass', function () {
    return gulp.src('./app/sass/style.scss')
        .pipe(sass().on('error', sass.logError))
        .pipe(gulp.dest('./app/css'));
});

gulp.task('minify-css', () => {
  return gulp.src('./app/css/*.css')
    .pipe(cleanCSS({compatibility: 'ie9'}))
    .pipe(gulp.dest('./app/css/min'));
});

gulp.task('cssConcat', function() {
  return gulp.src('./app/css/min/*.css')
    .pipe(concat('all-style.min.css'))
    .pipe(gulp.dest('./app/dist/css'));
});
gulp.task('jsConcat', function() {
  return gulp.src(['./app/js/library/jQuery-3.4.1.min.js', './app/js/*.js', './app/js/script/script.js'])
    .pipe(concat('all-js.min.js'))
    .pipe(gulp.dest('./app/dist/js'));
});
gulp.task('browser-sync', function() {
    browserSync.init({
        server: {
            baseDir: "./app"
        }
    });
});

gulp.task('run', gulp.parallel('sass', 'minify-css', 'cssConcat', 'jsConcat', 'browser-sync'));

gulp.task('watch', function () {
    gulp.watch('./app/sass/*.scss', gulp.series('sass'));
    gulp.watch('./app/css/*.css', gulp.series('minify-css'));
    gulp.watch('.app/css/min/*.css', gulp.series('cssConcat'));
    gulp.watch('./app/js/*.js', gulp.series('jsConcat'));
    gulp.watch('./app', gulp.series('browser-sync'));

});

gulp.task('default', gulp.parallel('run', 'watch'));

I have also attached a screenshot of the gulp running and finishing process.

https://i.stack.imgur.com/nGoZb.jpg

Answer №1

It is advisable not to run your css task (sass, minify-css and cssConcat) in parallel. The same goes for JS tasks.

You should consider making the following changes:

gulp.task('cssTask', gulp.series('sass', 'minify-css', 'cssConcat'));
gulp.task('run', gulp.parallel(cssTask, 'jsConcat', 'browser-sync'));

gulp.task('watch', function () {
    gulp.watch('./app/sass/*.scss', gulp.series('cssTask'));
    gulp.watch('./app/js/*.js', gulp.series('jsConcat'));
    gulp.watch('./app', gulp.series('browser-sync'));

});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The application experiences crashes when the tablet is rotated, happening while in the development stage

For my web-based Android application project, I am utilizing PhoneGap and Eclipse IDE on a Windows platform. My focus is specifically on Tablet development, more precisely on the Samsung Galaxy Tab 10.1. To develop, I use Eclipse and test the app on the ...

What is the best way to set an array as the value for a state variable?

Looking at my function, I have the following situation: execute(e) { let { items } = this.context; let array: number[] = []; for (var i = 0; i < 2; i++) array = [...array, i]; this.setState( { items: array, } ) ...

HTML/CSS - Troubleshooting Animation Issues

I am currently experimenting with different website animations and implementing responsive design. However, I encountered an issue when trying to apply these changes to my header, which also affects the .heading element. It seems that there is a problem w ...

Tips for including assisting text in the date field with Material UI

Behold, my component definition: <DateField name={formElement.name} type="date" label={formElement.name} onChange={(date) => formik.setFieldValue(formElement.name, date)} value={formik.values[formElement.name] ...

Automate web page interactions using Python Selenium to duplicate a class

Is there a method to duplicate a class? view image description here Upon examining the data-sitekey, I aim to replicate everything following it, namely 6Ld_LMAUAAAAAOIqLSy5XY9-DUKLkAgiDpqtTJ9b. Is this feasible? ...

CSS animations are not running as expected

Currently, I've been experimenting with a basic CSS animation. The objective is to make the application logo move 200px to the right and then smoothly return to its original position. Below is the code snippet I've used for these animations: /* ...

Div not centered after translation by 50% on the Y-axis

My HTML and body have a height of 100vh. Inside my body, I have a square that is 100px by 100px. When I apply top: 50% to the div, it moves down 50%. But when I apply translateY(50%) to the div, it does not move down 50%. Why is this? Please note that y ...

Modify the array dynamically within the Factory

I am currently working on a simple app where I want to display embed videos with the click of a button. It was quite challenging for me to dynamically bind my embed players, but I managed to do it successfully. I created a factory that contains my data in ...

JavaScript: Exporting and Utilizing a Function within a Model.js File

Coming from a background in PHP OOP development, I am aware that there are various methods to create classes in JavaScript. I require assistance from a JavaScript developer to resolve this particular issue. Here is the situation: I am building an AWS lamb ...

Is it possible to dynamically render css using Express.js?

I have a need to dynamically generate CSS with each request. For example, in my Express.js application, I've set up a route like this: /clients/:clientId/style.css When a matching request is received, I want to retrieve the Client from the reposito ...

There appears to be an issue with the functionality of the Bootstrap toggle tab

Currently, I am facing a toggle issue and having trouble pinpointing the root cause as there are no errors appearing in the console. The problem involves two tabs that can be toggled between - "pay" and "method". Initially, the default tab displayed is "pa ...

Removing connected entries with pre middleware on mongoose

I currently have 3 different schemas: Building const BuildingSchema = mongoose.Schema({ address: { type: String, required: true }, numberOfFloors: { type: Number, default: 0 }, }); Apartment const RoomSchema = mongoose.Schema({ roomNumber: { type: ...

What is causing this npm error to occur, and what steps can I take to resolve it?

I am currently in the process of integrating a PayPal system into my HTML website. To do this, I am following a tutorial that can be found here: https://www.youtube.com/watch?v=DNM9FdFrI1k&list=PL4eNesgScjZb3oktJK1786zI4nSET_tas&index=4 At 9:10 in ...

Tips for overlaying text onto a canvas background image

I'm curious about how to overlay text on top of an image that is within a canvas element. The image is a crucial part of my game (Breakout), so it must remain in the canvas. I've tried adding text, but it always ends up behind the image, which is ...

Python Web Scraping Automation Tool - Inconsistent Encountering of 511 Error Code

My latest project involves opening Firefox with Selenium, extracting HAR files using BrowserMobProxy, and accessing JSON data from those files at timed intervals. Occasionally, however, I encounter a 511 Error: <!DOCTYPE html><html><head> ...

How can I use Vue to close the side Navbar by targeting the body tag and clicking anywhere on the screen?

I am looking to make a change in the Navbar position by moving it right: -500px (to close the navbar) when clicking anywhere on the window. In HTML and JS, I can achieve this by targeting the body tag. How can I accomplish this in Vue? I already have funct ...

Create a new array by dynamically generating a key while comparing two existing arrays

One of the features in my app involves retrieving data from an API and storing it in $scope.newz. The previous user activity is loaded from LocalStorage as bookmarkData. I am facing a challenge with comparing the contentId values in arrays $scope.newz an ...

AngularJS substitution with regular expressions

Looking to replace specific words in HTML content within <div> and <p> tags upon page load. Utilizing angularJS to achieve this task. This is my current function: var elementsList = e.find('p'); var regex = ('[^<>\& ...

Steps for Disabling Autocomplete in a JSP Page

How can I prevent the browser from remembering the username and password on my JSP page after submitting the form? I've already tried using autocomplete=off in the JSP code. <form name="indexFrm" id="indexFrm" autocomplete="off" method="post"> ...

Creating a Drop-down Menu Item using React Material UI

I am experiencing an issue with displaying a MenuItem in my Dialog2 within my React app. Despite setting the zIndex: 1900 to a higher value than the two dialogs, the MenuItem remains hidden behind the dialogs instead of appearing at the front. Please revi ...