Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up gulp to bundle several files into one?

This seems like a very simple question, but spent the last 3 hours researching it, discovering it can be slow on every save on a new file if not using watchify.

This is my directory tree:

gulpfile.js
package.json

www/
  default.htm
     <script src="toBundleJsHere/file123.js"></script>

  toBundletheseJs/
    componentX/
       file1.js
    componentY/
       file2.js
    componentZ/
      file3.js

  toPutBundledJsHere/
      file123.js

Requirements. On every creation or save of a file within the folder toBundleTheseJs/ I want this file to be rebundled into toBundleJsHere/

What do I need to include in my package.json file?

And whats the minimum I need to write into my gulp file?

This should be as fast as possible so think I should be using browserify and watchify. I want to understand the minimum steps so using package manager like jspm is overkill a this point.

thanks

like image 923
Steve Tomlin Avatar asked Mar 05 '16 11:03

Steve Tomlin


People also ask

Can gulp concatenate JavaScript files?

Gulp plugin for concatenating javascript with sourcemaps. This gulp plugin will concatenate all source files together with some magic, so that it still works to require the other files. You only have to ensure you always use "./" when requiring local modules.

How do I bundle JavaScript files?

You can bundle your JavaScript using the CLI command by providing an entry file and output path. Webpack will automatically resolve all dependencies from import and require and bundle them into a single output together with your app's script. But that's just the bare minimum it can do.

Is gulp a module bundler?

Webpack is a JavaScript module bundler. Gulp is a build system that automates tasks. They are both open-source software.


Video Answer


1 Answers

var gulp = require('gulp');
var concat = require('gulp-concat');
gulp.task('js', function (done) {
    // array of all the js paths you want to bundle.
    var scriptSources = ['./node_modules/idb/lib/idb.js', 'js/**/*.js'];
    gulp.src(scriptSources)
        // name of the new file all your js files are to be bundled to.
        .pipe(concat('all.js'))
        // the destination where the new bundled file is going to be saved to.
        .pipe(gulp.dest('dist/js'));
    done();
});
like image 87
Willy Avatar answered Oct 17 '22 04:10

Willy