K
K
Konstantin Chuykov2015-05-30 02:26:19
Angular
Konstantin Chuykov, 2015-05-30 02:26:19

How to preserve the order of js files when concatenating with gulp?

Hello! I have the following working order of scripts:

<script src="js/jquery-2.1.3.min.js"></script>
        <script src="js/bootstrap.min.js"></script>
        <script src="js/socket.io.min.js"></script>
        <script src="js/angular.min.js"></script>
        <script src="js/angular/angular-route.min.js"></script>
        <script src="js/angular/angular-resource.min.js"></script>
        <script src="app.js"></script>
        <script src="factory/ng-socket-io.js"></script>
        <script src="controllers/MainController.js"></script>
        <script src="controllers/AuthController.js"></script>

I want to compress and glue them with gulp, I do it like this:
var assets = {
    js : [
        'public/js/vendor/*.js',
        'public/js/angular/*.js', 
        'public/js/app.js', 
        'public/factory/*.js',
        'public/controllers/*.js'
    ]
};

gulp.task('js', function(){
    gulp.src(assets.js)
        .pipe(concat('build.js'))
        .pipe(uglify())
        .pipe(gulp.dest('public/'))
});

But as you understand, complaints began about the lack of jquery, then the lack of the angular module, etc. Please tell me how can I keep the order of concatenation and not lose the effect of automating the loading of new scripts?
I found the answer that it can be divided into 2 streams (files), in one vendor, in another angular, but I would like to get by with one file. Thank you!
And one more question, if there are already min files in the folder, should they be deleted and the non-minified ones added, or does it not affect in any way, can the minified ones be processed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Zuev, 2015-05-30
@chuikoffru

To preserve the order, you can use gulp-order .
Minified files can be processed, but if you suddenly want to exclude them from minimization, you can use the gulp-if module . For example

var gulpif = require('gulp-if');
var uglify = require('gulp-uglify');

var notMinified = function (file) { 
    return !/\.min\.js/.test(file.path); 
};

gulp.task('task', function() {
  gulp.src('./src/*.js')
    .pipe(gulpif(notMinified , uglify()))
    .pipe(gulp.dest('./dist/'));
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question