Search code examples
javascriptgulp-concat

Gulp-concat can't concat file


Here is my directory structure:

- javascript
  |- file2.js
- output
- file1.js
- gulpfile.js

And I want to merge file1.js and file2.js. Here is my gulpfile.js content:

var gulp = require('gulp');
var concat = require('gulp-concat');

gulp.task('javascript', function () {
    return gulp.src('./javascript/*.js')
        .pipe(concat('file1.js'))
        .pipe(gulp.dest('./output/'));
});

however, the concat result is confusing me, the file in output directory is called file1.js, and the content in the file is only from origin file2, so I have some questions:

  • How can I decide the ouput file name?
  • Why the content don't merged? How can I make them merged?

Solution

  • The concat pipe takes as a parameter the name of the output file. The following should work:

    var gulp = require('gulp');
    var concat = require('gulp-concat');
    
    gulp.task('javascript', function () {
        return gulp.src(['./javascript/file2.js', './file1.js'])
            .pipe(concat('concatenatedFile.js'))
            .pipe(gulp.dest('./output/'));
    });
    

    EDIT: I updated the answer so that I explicitly show how to have the two files you want as the source files.