Compile JavaScripts with Gulp and Resolve Dependen

2019-05-18 21:37发布

问题:

I want to compile JavaScript files with Gulp.

I have a src directory where all scripts are present with .js extension. I want all scripts to be compiled separately and placed into a destination directory (dist) with the same filename as the original.

Consider this example:

src/jquery.js:

/**
 * @require ../../vendor/jquery/dist/jquery.js
 */

src/application.js:

/**
 * @require ../../vendor/angular/angular.js
 * @require ../../vendor/ngprogress-lite/ngprogress-lite.js
 * @require ../../vendor/restangular/dist/restangular.js
 * @require ../../vendor/lodash/dist/lodash.underscore.js
 * @require ../../vendor/angular-input-locker/dist/angular-input-locker.js
 * @require ../../vendor/angular-route/angular-route.js
 */

(function(document, angular) {

    'use strict';

    var moduleName = 'waApp';

    angular.module(moduleName, [
        // Some more code here.
    ;

    // Bootstrapping application when DOM is ready.
    angular.element(document).ready(function() {
        angular.bootstrap(document, [moduleName]);
    });

})(document, angular);

I'm using gulp-resolve-dependencies to resolve dependencies specified in the header of each source JavaScript file.

My gulpfile.js is looking like this:

//==============//
// Dependencies //
//==============//

var gulp = require('gulp');
var pathModule = require('path');
var resolveDependencies = require('gulp-resolve-dependencies');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');

//=======//
// TASKS //
//=======//

gulp.task('build:scripts', function(callback) {

    return gulp.src('scripts/*.js')
        .pipe(resolveDependencies({
            pattern: /\* @require [\s-]*(.*?\.js)/g,
            log: true
        }))
        .pipe(concat('all.js'))
        .pipe(uglify())
        .pipe(gulp.dest('js/'))
    ;
});

In order to merge scripts resolved by resolveDependencies I have to use concat, but concat requires a filename and merges not only original file and dependencies resolved for it, but all JavaScript files specified via glob pattern.

So, How do I get individual JavaScript files as the output? Like this:

dist/jquery.js:
    src/jquery.js
    vendor/jquery.js

dist/application.js:
    src/application.js
    vendor/angular.js
    vendor/ngprogress-lite.js
    ...

I have this workaround for now:

gulp.task('build:scripts', function(callback) {

    var compileScript = function(stream, filename) {
        return stream
            .pipe(resolveDependencies({
                pattern: /\* @require [\s-]*(.*?\.js)/g,
                log: true
            }))
            .pipe(concat(filename))
            .pipe(uglify())
            .pipe(gulp.dest('dist/'))
        ;
    };

    var scripts = getListOfFiles('src/', 'js');
    for (key in scripts) {
        var filename = scripts[key];
        var stream = gulp.src(pathModule.join('src/', filename));
        compileScript(stream, filename);
    }

    callback(null);
});

//===================//
// FUNCTIONS & UTILS //
//===================//

/**
 * Returns list of files in the specified directory
 * with the specified extension.
 *
 * @param {string} path
 * @param {string} extension
 * @returns {string[]}
 */
function getListOfFiles(path, extension) {

    var list = [];
    var files = fs.readdirSync(path);
    var pattern = new RegExp('.' + extension + '$');

    for (var key in files) {
        var filename = files[key];
        if (filename.match(pattern)) {
            list.push(filename);
        }
    }

    return list;
}

But it looks hackish and I can't find a good way to make it work with gulp-watch.

Is there a better and simpler way to solve this problem and achieve desired result?

回答1:

How do I get individual JavaScript files as the output?

Check an answer I gave to a similar problem here: Pass random value to gulp pipe template

Using this gulp plugin: https://github.com/adam-lynch/glob-to-vinyl

You can have access to single files.

This is how (assuming the use of this plugin):

function compileScript(file) {
  gulp
    .src('file')
    .pipe(resolveDependencies({
      pattern: /\* @require [\s-]*(.*?\.js)/g,
      log: true
    }))
    .pipe(concat())
    .pipe(uglify())
    .pipe(gulp.dest('dist/'))
  ;
};

gulp.task('build:scripts', function() {
  globToVinyl('src/**/*.js', function(err, files){
    for (var file in files) {
      compileScript(files[file].path);
    }
  });
});


回答2:

Here's the result of rewriting my task using solution specified by @avcajaraville. It's a complete, tested and working code.

var targetDir = 'web';
var sourceDir = 'assets';

var gulp = require('gulp');
var pathModule = require('path');
var resolveDependencies = require('gulp-resolve-dependencies');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var globToVinyl = require('glob-to-vinyl');

gulp.task('build:scripts', function(callback) {

    var compileScript = function(filePath, destinationDirectory) {

        // Extracting filename from absolute path (required by concat).
        var filename = pathModule.basename(filePath);

        return gulp
            .src(filePath)
            .pipe(resolveDependencies({
                pattern: /\* @require [\s-]*(.*?\.js)/g,
                log: true
            }))
            .pipe(concat(filename))
            .pipe(uglify())
            .pipe(gulp.dest(destinationDirectory))
        ;
    };

    var sourceGlob = pathModule.join(sourceDir, '/scripts/*.js');
    var destinationDirectory = pathModule.join(targetDir, '/js/');

    globToVinyl(sourceGlob, function(errors, files) {
        for (var file in files) {
            compileScript(files[file].path, destinationDirectory);
        }
    });

    callback(null);
});