Here is a simple guide on installing and configuring Gulp 4 for those who design a static web page which doesn’t require backend code. Gulp is a cross-platform, streaming task runner that lets developers automate many development tasks. So we will learn how to install Gulp 4 on your machine.
Gulp reads files as streams and pipes the streams to different tasks. These tasks are code-based and use plugins. The tasks modify the files, building source files into production files.
node -v npm -v
If they are not installed then please use below command
npm install npm@latest -g npm install node@latest -g
npm i gulp-cli -g
Now we are going to create project
Open you command prompt and create folder name : gulp then run next command npm init
This will guide you through giving your project a name, version, description, etc. You will see some text, and the CMD will offer you to add some information about your project. Fill out the fields or press Enter to leave them blank
Press Enter and you will see that the package.json has been created in the root of your project folder. Go to your gulp folder and run the following command
npm i gulp -D
Once above command complete , a folder named node_modules and a file named package-lock.json should appear in the root of your project folder
Run below command
npm i -D gulp-sass node sass gulp-autoprefixer gulp-cssnano gulp-concat gulp-uglify gulp-rename gulp-imagemin gulp-changed gulp-clean browser-sync
It will install the following plugins locally in your project:
const { src, dest, parallel, series, watch } = require('gulp'); // Load plugins const uglify = require('gulp-uglify'); const rename = require('gulp-rename'); const sass = require('gulp-sass'); const autoprefixer = require('gulp-autoprefixer'); const cssnano = require('gulp-cssnano'); const concat = require('gulp-concat'); const clean = require('gulp-clean'); const imagemin = require('gulp-imagemin'); const changed = require('gulp-changed'); const browsersync = require('browser-sync').create(); // Clean assets function clear() { return src('./assets/*', { read: false }) .pipe(clean()); } // JS function function js() { const source = './src/js/*.js'; return src(source) .pipe(changed(source)) .pipe(concat('bundle.js')) .pipe(uglify()) .pipe(rename({ extname: '.min.js' })) .pipe(dest('./assets/js/')) .pipe(browsersync.stream()); } // CSS function function css() { const source = './src/scss/main.scss'; return src(source) .pipe(changed(source)) .pipe(sass()) .pipe(autoprefixer({ overrideBrowserslist: ['last 2 versions'], cascade: false })) .pipe(rename({ extname: '.min.css' })) .pipe(cssnano()) .pipe(dest('./assets/css/')) .pipe(browsersync.stream()); } // Optimize images function img() { return src('./src/img/*') .pipe(imagemin()) .pipe(dest('./assets/img')); } // Watch files function watchFiles() { watch('./src/scss/*', css); watch('./src/js/*', js); watch('./src/img/*', img); } // BrowserSync function browserSync() { browsersync.init({ server: { baseDir: './' }, port: 3000 }); } // Tasks to define the execution of the functions simultaneously or in series exports.watch = parallel(watchFiles, browserSync); exports.default = series(clear, parallel(js, css, img));
The commands you will have to use the most often in the CMD:
This is the basic setup and installation of gulp. I think this will help you to start. Do you have any question you can feel free contact us.
Introduction Git tags are an essential feature of version control systems, offering a simple way…
Introduction The methods that browsers employ to store data on a user's device are referred…
Introduction A well-known open-source VPN technology, OpenVPN provides strong protection for both people and businesses.…
Introduction Integrating Sentry into a Node.js, Express.js, and MongoDB backend project significantly enhances error tracking…
Introduction In the world of JavaScript development, efficiently managing asynchronous operations is essential. Asynchronous programming…
Introduction Let's Encrypt is a Certificate Authority (CA) that makes it simple to obtain and…