javascript RequireJS:如何在构建到单个文件时排除某些路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18468803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RequireJS: How to exclude certain paths when building to single file?
提问by Clint Harris
I have a Backbone app which uses RequireJS 2.1.8 (i.e., all my Backbone views use define() to specify their dependencies). Everything works great and now I'm trying to use r.js (installed via NPM) to concat/minify all of my JavaScript into a single file.
我有一个使用 RequireJS 2.1.8 的 Backbone 应用程序(即,我所有的 Backbone 视图都使用 define() 来指定它们的依赖项)。一切正常,现在我正在尝试使用 r.js(通过 NPM 安装)将我的所有 JavaScript 合并/缩小到一个文件中。
How can I set up a r.js config which excludes dependencies that begin with certain paths?
如何设置排除以某些路径开头的依赖项的 r.js 配置?
I've included my main.js file below. In this case, I want the "built" output file to exclude the 3rd-party libraries (i.e., jquery, backbone, etc.). Also, I want to exclude any dependency that starts with "webapp/" (e.g., "webapp/dynamic_cfg", which results in a request being sent to my Djang app for a dynamically-generated JavaScript file/module).
我在下面包含了我的 main.js 文件。在这种情况下,我希望“构建”输出文件排除第 3 方库(即 jquery、backbone 等)。此外,我想排除任何以“webapp/”开头的依赖项(例如,“webapp/dynamic_cfg”,这会导致请求被发送到我的 Djang 应用程序以获取动态生成的 JavaScript 文件/模块)。
Folder structure:
文件夹结构:
|--static/
|--main.js
|--myapp/
|--views/
|-- MyView.js
|-- ...
|--lib
|--backbone-1.0/
|--underscore-1.5.1/
|-- ...
index.html (a Django template):
index.html(Django 模板):
<script src="{% static 'lib/requirejs-2.1.8/require.min.js' %}" data-main="{% static 'main.js' %}" ></script>
main.js:
主要.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();
}
);
回答by Clint Harris
Setting the paths to "empty:" in my r.js config worked. Example:
在我的 r.js 配置中将路径设置为“空:”有效。例子:
// 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",
};
})()