javascript – 如何使用Gulp订购注入的文件?

前端之家收集整理的这篇文章主要介绍了javascript – 如何使用Gulp订购注入的文件?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
MyAngular应用程序具有以下结构
- src/app/main/
    |
    -main.js
    -main.controller.js
    -index.html

我使用Gulp.构建完成后,* .js文件在index.html中以错误的顺序注入.文件main.js取决于main.controller.js,所以main.controller.js必须在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 -->

这是我的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');
});

解决方法

在你的inject.js中使用gulp-angular-filesort

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’,[]).

示例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'));

  });
};
原文链接:https://www.f2er.com/js/154645.html

猜你在找的JavaScript相关文章