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');
});
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.
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'));
});
};
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'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With