$喷油器:modulerr后r.js优化,而不是之前 [英] $injector:modulerr after r.js optimization, but not before
问题描述
我用 require.js
与 r.js
优化。 pre-优化,我有:
// MainCtrl.js
定义(函数(){
返回[$范围功能($范围){执行console.log($范围内); }];
});// main.js
require.config({
垫片:{
角度:{
出口:角,
},
},
路径:{
角:LIB / angular.min
},
});
需要([角,MainCtrl],功能(NG,MainCtrl){
的console.log(纳克);
VAR应用= ng.module(MyApp的,[]);
app.controller(MainCtrl,MainCtrl);
});
我的HTML是非常简单的;它只是 NG-应用=的myapp </ code>在
&LT; HTML&GT;
和 NG-控制器= MainCtrl
在&LT;车身方式&gt;
当我运行这样的应用程序,一切似乎工作。我得到的的console.log
为纳克
这似乎是角
对象。然后,我得到的的console.log
为 $范围
从 MainCtrl
。没有问题。
运行后 r.js
,并使用缩小的 main.js
,我得到的错误和东西不再起作用。我跑 r.js -o build.js
。
// build.js
({
APPDIR:公,
的baseUrl:JS
导演:DIST
fileExlusionRegExp:/^(r|build\\.js)$/,
优化:uglify2
optimizeCss:标准,
模块:[{
名称:主
}],
路径:{
角:LIB / angular.min
},
})
的现在的,当我访问 index.html的
我得到的错误:
未捕获的错误:[$喷油器:modulerr]
未定义
未捕获类型错误:无法调用未定义的方法模块
块引用>
- 似乎表明,角度不正确加载或东西。它不能注入的东西
的myapp </ code>,但它没有依赖关系。没有
ngRoute
或任何纳克
是,即使它是正确的,现在不确定的角
之前。这也导致了第二个错误- 的
的console.log
在MainCtrl
不叫。不是一个巨大的奇怪,因为存在对previous线错误。的然而的,如果我改变纳克
到角
的未定义
不和app.controller
被调用,但的console.log
从出现误差MainCtrl
是还是的不叫。我唯一的猜测是,角被缩小的两次(我已经使用angular.min.js然后
r.js
minifies一遍)。我可以做任何事情来解决这个问题?解决方案在
垫片
文档,它表明:
您应该使用mainConfigFile构建选项来指定文件在哪里可以找到垫片配置。否则,优化器将不知道垫片配置的。另一种选择是复制在生成配置文件中的垫片配置。
块引用>这可能是造成
角
的垫片是从内置文件不存在的。I'm using
require.js
withr.js
to optimize. Pre-optimization, I have:// MainCtrl.js define(function () { return ["$scope", function ($scope) { console.log($scope); }]; }); // main.js require.config({ shim: { angular: { exports: "angular", }, }, paths: { angular: "lib/angular.min", }, }); require(["angular", "MainCtrl"], function (ng, MainCtrl) { console.log(ng); var app = ng.module("myapp", []); app.controller("MainCtrl", MainCtrl); });
My HTML is very simple; it just has
ng-app=myapp
in<html>
andng-controller=MainCtrl
on<body>
.When I run the app like this, everything seems to work. I get the
console.log
forng
which appears to be theangular
object. Then, I get theconsole.log
for$scope
fromMainCtrl
. No problems.After running
r.js
and using the minifiedmain.js
, I get errors and things no longer work. I runr.js -o build.js
.// build.js ({ appDir: "public", baseUrl: "js", dir: "dist", fileExlusionRegExp: /^(r|build\.js)$/, optimize: "uglify2", optimizeCss: "standard", modules: [{ name: "main" }], paths: { angular: "lib/angular.min", }, })
Now when I visit
index.html
I get errors:Uncaught Error: [$injector:modulerr] undefined Uncaught TypeError: Cannot call method 'module' of undefined
- Seems to indicate that angular is not loading properly or something. It can't inject something to
myapp
, but it has no dependencies. NotngRoute
or anythingng
is now undefined even though it was correctlyangular
before. This also causes the second error- The
console.log
inMainCtrl
is not called. Not a huge surprise since there is an error on the previous line. However, if I changeng
toangular
theundefined
error does not occur andapp.controller
gets called, but theconsole.log
fromMainCtrl
is still not called.My only guess is that angular is being minified twice (I'm already using angular.min.js and then
r.js
minifies it again). Can I do anything to fix this issue?解决方案In the
shim
documentation, it is suggests:You should use the mainConfigFile build option to specify the file where to find the shim config. Otherwise the optimizer will not know of the shim config. The other option is to duplicate the shim config in the build profile.
This is probably causing
angular
's shim to be absent from the built file.这篇关于$喷油器:modulerr后r.js优化,而不是之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!