我有一个使用RequireJS 2.1.8的Backbone应用程序(即我的所有Backbone视图都使用define()来指定它们的依赖关系)。一切都很好,现在我正在尝试使用r.js(通过NPM安装)将我的所有JavaScript连接/缩小为一个文件。
如何设置r.js配置,该配置会排除以某些路径开头的依赖项?
我在下面包含了我的main.js文件。在这种情况下,我希望“构建”输出文件排除第三方库(即jquery,骨干等)。此外,我想排除任何以“webapp /”开头的依赖项(例如,“webapp / dynamic_cfg”,这会导致请求被发送到我的Djang应用程序以获取动态生成的JavaScript文件/模块)。
文件夹结构:
|--static/
|--main.js
|--myapp/
|--views/
|-- MyView.js
|-- ...
|--lib
|--backbone-1.0/
|--underscore-1.5.1/
|-- ...
index.html(Django模板):
<script src="{% static 'lib/requirejs-2.1.8/require.min.js' %}" data-main="{% static 'main.js' %}" ></script>
main.js:
requirejs.config({
paths: {
"jquery": 'lib/jquery-1.10.2/jquery.min',
"text_loader": 'lib/requirejs-text-2.0.10/requirejs-text',
"fuelux": 'lib/fuelux-2.3.1',
"backbone": 'lib/backbone-1.0/backbone.min',
"underscore": 'lib/underscore-1.5.1/underscore.min',
// Any module that requires 'webapp/*' will result Require.js
// making a request to the server webapp.
"webapp": '..'
},
shim: {
'backbone': {
deps: ['underscore', 'jquery'], // Load these dependencies first
exports: 'Backbone' // Create global var with this name for the module
},
'underscore': {
exports: '_'
}
}
});
// Startup
require(['webapp/dynamic_cfg', 'myapp/util/logger', 'myapp/view/AppView', 'myapp/AppRouter', 'fuelux/all'],
// Dependencies are loaded and passed to this function
function(cfg, logger, AppView, AppRouter, fuelUx) {
logger.info("Starting up with config:", cfg);
var appView = new AppView();
var appRouter = new AppRouter();
}
);
答案 0 :(得分:30)
在我的r.js配置中将路径设置为“empty:”。例如:
// This is a RequireJS config file
(function(){
return {
// Name of input file (without the .js extention)
"name": "main",
// Directory containing input file
"baseUrl": "static/",
// Look in this file for the require.config() call and extract it
"mainConfigFile": "static/main.js",
"paths": {
// Don't attempt to include dependencies whose path begins with webapp/
"webapp": "empty:",
// Ditto for the following 3rd-party libraries
"jquery": "empty:",
"fuelux": "empty:",
"backbone": "empty:",
"underscore": "empty:"
},
"optimize": "uglify2",
};
})()