Completing Gulp - JS and Image Minification

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 403:- Completing Gulp - JS and Image Minification

To expand on the previous answer, in addition to CSS minification, Gulp can also be used to minify JavaScript (JS) files and optimize images. Here's how you can complete the Gulp setup for JS and image minification:

  1. Install Required Plugins: In your project directory, install the necessary Gulp plugins for JS minification and image optimization. Run the following command:

    cssCopy code

    npm install gulp-uglify gulp-imagemin --save-dev

    This installs the gulp-uglify plugin for JS minification and the gulp-imagemin plugin for image optimization.

  2. Update Gulpfile: Open your gulpfile.js in a code editor and modify it to include the tasks for JS and image minification. Below is an example:

    javascriptCopy code

    const gulp = require('gulp'); const cssmin = require('gulp-cssmin'); const rename = require('gulp-rename'); const uglify = require('gulp-uglify'); const imagemin = require('gulp-imagemin'); gulp.task('minify-css', function () {  return gulp.src('src/css/*.css')    .pipe(cssmin())    .pipe(rename({ suffix: '.min' }))    .pipe(gulp.dest('dist/css')); }); gulp.task('minify-js', function () {  return gulp.src('src/js/*.js')    .pipe(uglify())    .pipe(rename({ suffix: '.min' }))    .pipe(gulp.dest('dist/js')); }); gulp.task('optimize-images', function () {  return gulp.src('src/images/*')    .pipe(imagemin())    .pipe(gulp.dest('dist/images')); }); gulp.task('default', gulp.parallel('minify-css', 'minify-js', 'optimize-images'));

    In this updated example, the minify-js task takes JS files from the src/js/ folder, applies the uglify plugin to minify them, renames the output file with a .min suffix, and saves them in the dist/js/ folder.

    Similarly, the optimize-images task takes image files from the src/images/ folder, applies the imagemin plugin to optimize them, and saves the optimized images in the dist/images/ folder.

    The default task has been updated to include all three tasks: minify-css, minify-js, and optimize-images. When you run gulp in the command line, all these tasks will be executed simultaneously.

  3. Run Gulp Tasks: Open the command line in your project directory and run the following command:

    Copy code

    gulp

    This will execute all the tasks defined in the gulpfile.js, including CSS minification, JS minification, and image optimization.

    After running the tasks, Gulp will minify the CSS and JS files, optimize the images, and save the resulting files in their respective destination folders (dist/css/, dist/js/, dist/images/).

With these modifications, you have expanded your Gulp setup to include JS minification and image optimization. Gulp will now automatically perform these tasks when you run the gulp command in your project directory, streamlining your development workflow and optimizing your assets for production.

47. GULP - Getting Deployment Ready

Comments: 2

profile
@niteshguptav63
17-Nov-2024, 01:39 PM

I am not able to access videos from second class and further. I have already completed first class

profile
@niteshguptav63
16-Nov-2024, 10:56 AM

When will I get my course?

profile
@admin79
17-Nov-2024, 01:29 PM

Now, Your query was resolved.

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?