我是一个大口新手。我正在使用VS 2015 Update 3来完成我的项目。对于该项目,我有几个使用TypeScript编写的服务和控制器类。我使用gulp对由.ts文件转换的那些角度js文件进行排序,连接,uglify。这里服务和控制器文件的顺序很重要,所以我使用" gulp-angular-filesort"连接这些文件之前的配方。我尝试了2个版本。
对于版本1(请参阅下面的代码。任务命名为" min:appcompExp" ),我在gulpfile.js文件中按顺序列出js文件,并且结果缩小(最小)文件正在运行,当我运行我的项目时没有问题。
对于版本2(请参阅下面的代码。任务名称为" min:appcompExp2" ),我使用通配符列出SOURCE js文件名,以便在gulpfile.js。但是,生成的minified(min)文件已创建,但在运行项目时它无法正常工作。错误看起来像依赖注入问题:虽然我看到此版本的结果min文件包含其中的所有服务和控制器类,但控制器找不到相应的服务DI。我使用了ngularFilesort = require(" gulp-angular-filesort"),但它对版本2没有帮助。
使用gulp版本2的一个特定DI错误:
Error: $injector:unpr
Unknown Provider
Unknown provider: ContactSrvcProvider <- ContactSrvc <- ContactCtrl
有人可以解释一下为什么使用通配符来获取SOURCE js文件名并不适用于gulp?
以下是我的gulpfile.js,它列出了两个版本的concat和uglify服务和控制器类:
/*
This file in the main entry point for defining Gulp tasks and using Gulp plugins.
Click here to learn more. http://go.microsoft.com/fwlink/?LinkId=518007
*/
var gulp = require("gulp"),
inject = require("gulp-inject"),
concat = require("gulp-concat"),
print = require("gulp-print"),
angularFilesort = require("gulp-angular-filesort"),
uglify = require("gulp-uglify"),
rimraf = require("rimraf");
// concatenated/bundled files
// vendor js scripts
var pathsCssSrc = "./bower_components/bootstrap/dist/css/*.min.css";
var pathsNglibSrc = [
"./bower_components/angular/angular.min.js",
"./bower_components/angular-route/angular-route.min.js",
"./bower_components/jquery/dist/jquery.min.js", // must before "bootstrap.min.js"
"./bower_components/bootstrap/dist/js/bootstrap.min.js"
];
var pathsAppmoduleSrc = [
"./app/app.js",
"./app/Constants/Constants.js",
"./app/routes.js",
"./app/models/*.js"
];
// destinations
var pathsDest = { build: ".build/", subspa : "spa/", indexfile : "./index.html" };
pathsDest.cssDest = pathsDest.build + "css/appStyles.min.css";
pathsDest.nglibDest = pathsDest.build + "nglib/nglib.min.js";
pathsDest.appModuleDest = pathsDest.build + pathsDest.subspa + "app.min.js";
// tasks
....
////// services, controllers
gulp.task("min:appcompExp", function () {
// I explictly list ALL of js file names and their orders of js file names are important.
// The resulted min js file working when I run my project.
var target = gulp.src(pathsDest.indexfile); // important: root folder must be specified before file name "index.html"
var comp = gulp.src([ "app/services/SessionSrvc.js",
"app/services/UtilSrvc.js",
"app/services/BaseSrvc.js",
"app/services/ContactSrvc.js",
"app/services/AvatarSrvc.js",
"app/services/LoginSrvc.js",
"app/services/RegisterSrvc.js",
"app/services/presidentsSrvc.js",
"app/controllers/BaseCtrl.js",
"app/controllers/LoginCtrl.js",
"app/controllers/LogoutCtrl.js",
"app/controllers/RegisterCtrl.js",
"app/controllers/PresidentCtrl.js",
"app/controllers/PresidentDetailCtrl.js",
"app/controllers/ContactCtrl.js",
"app/controllers/AvatarCtrl.js"]);
var dest = ".build/spa/appcomp.min.js";
return target
.pipe(inject(comp
.pipe(print())
.pipe(concat(dest))
.pipe(angularFilesort())
.pipe(uglify()) // Minifies the concatenated js file.
.pipe(gulp.dest(".")) // important: otherwise there is no files stored
, { name: "appcomp" }))
.pipe(gulp.dest("./")); // important: root folder must be specified like so
});
////////////////////////////
gulp.task("min:appcompExp2", function () {
// using wild cards for SOURCE js codes for shorting codes
// I got runtime error when running my project: DI problem - controllers cannot find their service classes.
var target = gulp.src(pathsDest.indexfile); // important: root folder must be specified before file name "index.html"
var comp = gulp.src(["app/services/*.js",
"app/controllers/*.js"]);
var dest = ".build/spa/appcomp.min.js";
return target
.pipe(inject(comp
.pipe(print())
.pipe(concat(dest))
.pipe(angularFilesort()) // this does not help though
.pipe(uglify()) // Minifies the concatenated js file.
.pipe(gulp.dest(".")) // important: otherwise there is no files stored
, { name: "appcomp" }))
.pipe(gulp.dest("./")); // important: root folder must be specified like so
});
答案 0 :(得分:0)
当您在gulp.src()
中使用通配符,即["app/services/*.js", "app/controllers/*.js]
时,gulp会按排序顺序读取app/services/
和app/controllers/
目录中的所有js文件。
当您在gulp.src()
明确列出源文件,即["app/services/SessionSrvc.js", "app/services/UtilSrvc.js", app/services/BaseSrvc.js", etc.]
时,gulp会按特定顺序读取这些js文件。
你的"min:appcompExp2"
gulp任务无效的原因是因为在gulp.src()
中使用通配符会使gulp处理的js文件的顺序变得混乱。而"min:appcompExp"
gulp任务正在运行,因为您按照应该处理的顺序明确列出了js文件。
我敢打赌,如果您在gulp.src()
gulp任务中修改了"min:appcompExp"
的参数,以便js文件按排序顺序排列,它将失败的方式与"min:appcompExp2"
gulp任务相同做的:
[
"app/services/AvatarSrvc.js",
"app/services/BaseSrvc.js",
"app/services/ContactSrvc.js",
"app/services/LoginSrvc.js",
"app/services/SessionSrvc.js",
"app/services/UtilSrvc.js",
"app/services/presidentsSrvc.js",
"app/services/RegisterSrvc.js",
"app/controllers/AvatarCtrl.js"
"app/controllers/BaseCtrl.js",
"app/controllers/ContactCtrl.js",
"app/controllers/LoginCtrl.js",
"app/controllers/LogoutCtrl.js",
"app/controllers/PresidentCtrl.js",
"app/controllers/PresidentDetailCtrl.js",
"app/controllers/RegisterCtrl.js",
]
我假设它是presidentsSrvc.js
的不区分大小写的排序顺序。