Updated February 17, 2023
Introduction to Gulp Concat
The gulp provides a different kind of feature to the developer; the concat is one of the features provided by the gulp. A gulp is an open-source tool used to automate web tasks as per the developer, but sometimes the developer needs to combine the different JavaScripts files; at that time, we can gulp concat to merge all files as per our requirement. Pipe concept here, we need to implement the concat function and pass different parameters to the newly created file; for example, we consider the file’s name one of the arguments that need to pass.
What is Gulp Concat?
By and large, a site or a JavaScript application contains a few JavaScript documents. Moreover, the program should get those records from the server to stack the page. Downloading more records takes additional time as the program needs to send more HTTP demands.
We attempt to convey an ideal experience whenever we make sites. This incorporates having our website pages show quickly. Some time ago, this implied having every one of our styles in one CSS document. Instead, all Gulp setup goes in a record called gulpfile.js situated at the foundation of the undertaking. The example for composing assignments is that you first burden a module you will utilize and characterize a project that depends on that module afterward.
Gulp is module-driven; you want to know which module to utilize to achieve something. Usually, a solitary module has a solitary reason, and all the modules are customary JavaScript. While this made our site pages load quicker, it made keeping up with the CSS document a night-female. Nowadays, we can utilize it numerously.
CSS documents for better association and afterward concat (significance consolidation or join) the records into one enormous record. Let’s expect we have two separate CSS documents in our construct/CSS envelope. We will utilize a gulp module to concat all our CSS documents in the style envelope. Gulp contains a few fundamental errands, yet the force of gulp is the customization you can bring into your assemble cycle by utilizing modules.
How to Use Gulp Concat?
Let’s see how we can use gulp concat as follows:
Code:
var concatf = require('gulp-concat');
gulp.task('scrcon, function() {
return gulp.src('./css/*.js')
.pipe(concat('all.js'))
.pipe(gulp.dest('./distf/'));
});
Explanation:
- In the above code, we can contact the file on our machine, and it uses the base directory from the very first file and sends it through the process.
- After that, the concatenation process will start in specified order inside the gulp.src function for example/css/file1.js, ./css/file2 and so on. So in this order, we can use the following code as follows.
Code:
var concatf = require('gulp-concat');
gulp.task('scrcon, function() {
return gulp.src(['./css/file2.js', './css/file3.js', './css/file1.js'])
.pipe(concat('all.js'))
.pipe(gulp.dest('./dist/'));
});
If we want to change a new line, we need to add the following line inside the code as follows:
Code:
.pipe(concat('main.js', {newLine: ';'}))
Gulp Concat Install
Let’s see how we can install gulp concat as follows:
But, first, we need to confirm all setup and installation of gulp with the help of the following command.
Code:
node -v
Explanation:
- Using the above command, we can see the installed version of Node.js; after execution, we can see the result in the following command.
Output:
In the command line prompt, enter the accompanying order to show the variant of npm (Node.js bundle chief), which is utilized to introduce modules.
It will show the introduced Node.js form with the help of the below command.
Code:
npm -v
Explanation:
- After executing the above command, we can see the currently installed version of npm on our machine, as shown in the screenshot below.
Output:
Now we successfully installed the Node.js; now, we need to install the gulp by using the below command as follows:
Code:
npm install gulp -g
Explanation:
- In the above command, we use g for a flag that ensures gulp is globally available for all projects.
- After entering the above command, we get the following screen, as shown in the below screenshot.
Output:
For verification of gulp, we need to run the below command as follows:
Code:
gulp –v
Explanation:
- After executing the above command, we get the currently installed version of a gulp, as shown in the screenshot below.
Output:
Now we have it all set up; we need to install the Concat function with the help of the following statement as follows:
Code:
npm install --save-dev gulp-concat
Explanation:
- Using the above command, we can add the package and the development dependencies of concat.
Example of Gulp Concat
Given below is the example of Gulp Concat:
We have two different files, as shown in the following code.
Samplecss1.css
Smaplecss1.css
Explanation:
- We use two different css files from the source folder, and we need to combine that files by using concat().
After creating a new file and giving it a name per our requirement with the .js extension.
Code:
var gulpc = require('gulp');
var concat = require('gulp-concat');
gulp.task('concatf', [], function() {
console.log("concat all css file and placed inside the style folder");
gulp.src("https://cdn.educba.com/Gulpdemo/styles/**.css")
.pipe(concat('final.css'))
.pipe(gulp.dest('dest/styles'));
});
Explanation:
- In the above code, we can see a couple of changes we made. First, we decided to name this concat. Depending on the developer, we could call it anything we need; however, concat conveys how the module treats those perusing our fabricated script.
- In the following steps, we added another task In the middle between the src and the pipe (gulp.dest…) steps, as well as added pipe (concat(…)). This is because gulp works by streaming documents from one cycle to the next. This permits us to make complex form errands out of little, straightforward advances.
- Now run the gulp as shown below screenshot with the result of concat.
Output:
Conclusion
With the help of the above article, we saw about the Gulp concat. From this article, we know basic things about the Gulp concat, and we also saw the integration of the Gulp concat and how we use it in the Gulp concat.
Recommended Articles
This is a guide to Gulp Concat. Here we discuss the introduction and how to use and Install gulp concat with examples, respectively. You may also have a look at the following articles to learn more –