Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to order injected files using Gulp?

MyAngular application has the following structure

- src/app/main/
    |
    -main.js
    -main.controller.js
    -index.html

I'm using Gulp. After the build completes, the *.js files are injected in the wrong order in index.html. The file main.js depends on main.controller.js, so main.controller.js has to be injected before main.js.

<!-- build:js({.tmp/serve,.tmp/partials,src}) scripts/app.js -->
<!-- inject:js -->
<script src="app/main/main.js"></script>
<script src="app/main/main.controller.js"></script>
<script src="app/index.js"></script>
<!-- endinject -->

This is my gulpfile.js

'use strict';

var gulp = require('gulp');
var gutil = require('gulp-util');
var wrench = require('wrench');

var options = {
  src: 'src',
  dist: 'dist',
  tmp: '.tmp',
  e2e: 'e2e',
  errorHandler: function(title) {
    return function(err) {
      gutil.log(gutil.colors.red('[' + title + ']'), err.toString());
      this.emit('end');
    };
  },
  wiredep: {
    directory: 'bower_components',
    exclude: [/jquery/, /bootstrap-sass-official\/.*\.js/, /bootstrap\.css/]
  }
};

wrench.readdirSyncRecursive('./gulp').filter(function(file) {
  return (/\.(js|coffee)$/i).test(file);
}).map(function(file) {
  require('./gulp/' + file)(options);
});

gulp.task('default', ['clean'], function () {
    gulp.start('build');
});
like image 417
misco Avatar asked Jun 15 '15 09:06

misco


3 Answers

I fixed this problem by using the gulp-natural-sort and stream-series modules. gulp-natural-sort orders your files in a consistent order and stream-series takes multiple streams of files and places one before the other.

My code looks like this:

var series = require('stream-series');
var naturalSort = require('gulp-natural-sort');

gulp.task('inject', function() {
    log('Injecting custom scripts to index.html');

    var theseComeFirst = gulp.src(["somepath/first*.js", {read: false}).pipe(naturalSort());

    var theseComeSecond = gulp.src("somepath/second*.js", {read: false}).pipe(naturalSort());

    return gulp
        .src("*.html")
        .pipe($.plumber({errorHandler: handleError}))
        .pipe($.inject(series(theseComeFirst, theseComeSecond), {relative: true}))
        .pipe(gulp.dest('client/'));
});

You could just use the stream-series, as long as you don't care if the others are in order or not. Unfortunately inject() will put the files in a series group in somewhat random order every time it runs, which gives me the heebee jeebees.

like image 175
Ryan Shillington Avatar answered Nov 17 '22 20:11

Ryan Shillington


Use gulp-angular-filesort in your inject.js

https://www.npmjs.com/package/gulp-angular-filesort

To work correctly, each angular file needs to have a uniquely named module and setter syntax (with the brackets), i.e. angular.module('myModule', []).

Example inject.js:

module.exports = function(options) {
  gulp.task('inject', ['scripts', 'styles'], function () {
    var injectStyles = gulp.src([
      options.tmp + '/serve/app/**/*.css',
      '!' + options.tmp + '/serve/app/vendor.css'
    ], { read: false });

    var injectScripts = gulp.src([
      options.src + '/app/**/*.js',
      '!' + options.src + '/app/**/*.spec.js',
      '!' + options.src + '/app/**/*.mock.js'
    ])
    .pipe($.angularFilesort()).on('error', options.errorHandler('AngularFilesort'));

    var injectOptions = {
      ignorePath: [options.src, options.tmp + '/serve'],
      addRootSlash: false
    };

    return gulp.src(options.src + '/*.html')
      .pipe($.inject(injectStyles, injectOptions))
      .pipe($.inject(injectScripts, injectOptions))
      .pipe(wiredep(options.wiredep))
      .pipe(gulp.dest(options.tmp + '/serve'));

  });
};
like image 2
KaushikTD Avatar answered Nov 17 '22 20:11

KaushikTD


It works for me to first define the important js files in the Gulp source array:

var target = gulp.src('app/index.html');
var sources = gulp.src(['app/scripts/core/app.js','app/scripts/**/*.js'], {read: false});
target.pipe(inject(sources, {})).pipe(gulp.dest('app'));
like image 2
Michel Avatar answered Nov 17 '22 20:11

Michel