简体   繁体   English

茉莉花和角质的$ provider注射剂减少

[英]Minified $provider injection with jasmine and angular

We have a project (angular) and some unittests for it (jasmine+sinon), which when minified creates some issues. 我们有一个项目(角度),并为此进行了一些单元测试(茉莉花和西农),将其缩小时会产生一些问题。 For the actual code, we've solved these problems by injecting using the staticly typed string array, eg ['locationService', 'etcService']. 对于实际的代码,我们已经通过使用静态类型的字符串数组(例如['locationService','etcService'])进行注入来解决了这些问题。 Unfortunately for the unittests, the minification has some more problems to solve. 不幸的是,对于单元测试,缩小还有更多问题需要解决。 As an example: 举个例子:

module(function($provide){
    $provide.service('etc..',...);
}

Code above immediately becomes unusuable since the provider variable gets renamed to something like 'a'. 上面的代码立即变得不可用,因为provider变量被重命名为“ a”之类的东西。 I've tried to tweak it a bit wrapping the function with something like below: 我试图对其进行一些调整,如下所示:

function injectTest($provide){
    // do the same stuff
}
injectTest.$inject = ['$provide'];

which was a recommended solution in some other online posts. 在其他一些在线帖子中,这是推荐的解决方案。 The problem is with modules this really doesn't work. 问题在于模块确实不起作用。 I've tried both: 我都尝试过:

module(angular.injector().invoke(injectTest)); // which results in 'Unknown provider: $provideProvider <- $provide

and

module(injectTest); // which results in 'Unknown provider: nProvider <- n'

Is there any way to inject the $provider into a module without breaking on minification? 有什么方法可以将$ provider注入模块而不会破坏最小化吗?

Inline injection : 在线注入:

var myFN = ['$provide', function($provide){
   // do stuff
}]

Now if you want to bind a function to a 3rd party library where you need service let's say in my sample your function need the service CRUDService and receive a params objects from the 3rd party : 现在,如果要将函数绑定到需要服务的第三方库,在我的示例中,您的函数需要服务CRUDService并从第三方接收params对象:

var myFN = ['CRUDService', function(CRUDService){
   // do some init stuff
   // you can either make it a singleton by sotrng the function and return the reference or either return new function on each call
   return function(params){
       // do stuff
   };
}] ;

// now to bind it to your 3rd party
objectFor3rdParty = {fn:$injector.invoke(myFN)};

I use only inline injection instead of $inject, matter of taste i guess. 我只使用内联注入而不是$ inject,我想是味道问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM