Search code examples
javascriptgulprequirejsrequire

Let Gulp merge/resolve require js files


I have a main.js file with some code and a couple of require('some_other_file.js'); lines in them (which in turn might require some other files).

The Requirement: Using Gulp 4.0, I would like to merge all these files together into one file by just supplying the main.js file as source and let some Gulp plugin figure out which other files need to be included as well (so not just simply using a combine on all the files!).

Using requirejs in gulp simply adds the require(...); lines to the output file.

gulp.task('compile_js', function(cb) {

    var config = {
        baseUrl: './src/js/',
        include: ['main'],
        out: './dist/main.js',
    };

    rjs.optimize(config, function(buildResponse){
        console.log('build response', buildResponse);
        cb();
    }, cb);
});

Am I doing it wrong with requirejs or is there a Gulp plugin that can handle this?


Solution

  • Found it! Browserify seems to do the trick.

    Browserify: http://browserify.org/

    And an implementation in Gulp: https://github.com/gulpjs/gulp/blob/master/docs/recipes/browserify-uglify-sourcemap.md