Backbone 应用程序经过预优化后运行良好。使用 r.js(与杏仁)后,我成功生成了一个输出文件,该文件似乎总是在紧跟骨干的模块中抛出“TypeError:Backbone is undefined”错误。
例如,当 r.js 的输出如下所示时:
/consumer/public/javascripts/vendor/almond.js
/consumer/public/javascripts/vendor/jquery-1.7.2.js
/consumer/public/javascripts/vendor/underscore.js
/consumer/public/javascripts/vendor/bootstrap.js
/consumer/public/javascripts/vendor/backbone.js
/consumer/public/javascripts/rjs/src/mnp/prop_m.js
/consumer/public/javascripts/rjs/src/mnp/prop_c.js
/consumer/public/javascripts/vendor/play-mustache.js
...
/consumer/public/javascripts/main.js
在浏览器中加载优化文件后,在引用 Backbone 时会在 'prop_m.js' 模块中抛出上述错误。使用 require 2.0.5、Backbone 0.9.2 和 almond 0.1.4。提前感谢您的帮助。
PS:build.js 看起来像这样:
({
baseUrl: "./",
mainConfigFile: "main.js",
paths: {
'almond': 'vendor/almond',
// need a path to the cs-compiled .js file
'app' : '../../target/scala-2.9.1/resource_managed/main/public/javascripts/rjs/src/app',
'mnp/router' : '../../target/scala-2.9.1/resource_managed/main/public/javascripts/rjs/src/mnp/router',
'mnp/search_form_v' : '../../target/scala-2.9.1/resource_managed/main/public/javascripts/rjs/src/mnp/search_form_v',
'mnp/prop_c' : '../../target/scala-2.9.1/resource_managed/main/public/javascripts/rjs/src/mnp/prop_c',
'mnp/prop_m' : '../../target/scala-2.9.1/resource_managed/main/public/javascripts/rjs/src/mnp/prop_m',
'mnp/prop_list_v' : '../../target/scala-2.9.1/resource_managed/main/public/javascripts/rjs/src/mnp/prop_list_v',
'mnp/prop_item_v' : '../../target/scala-2.9.1/resource_managed/main/public/javascripts/rjs/src/mnp/prop_item_v'
},
/*not compatible with modules*/
name: 'vendor/almond',
include: ["main"],
out: "dist/app.js",
/*not compatible with modules*/
keepBuildDir: false,
preserveLicenseComments: false,
optimize: "none",
wrap: {
start: "(function(global, define) {\n"+
// check for amd loader on global namespace
" var globalDefine = global.define;\n",
end: " var library = require('main');\n"+
" if(typeof module !== 'undefined' && module.exports) {\n"+
// export library for node
" module.exports = library;\n"+
" } else if(globalDefine) {\n"+
// define library for global amd loader that is already present
" (function (define) {\n"+
" define(function () { return library; });\n"+
" }(globalDefine));\n"+
" } else {\n"+
// define library on global namespace for inline script loading
" global['main'] = library;\n"+
" }\n"+
"}(this));\n"
}
})`