admin管理员组文章数量:1277876
When should I use paths
versus packages
in RequireJS? Is there a best practice for this or are there particular times when I should consider using one over the other?
I've followed the docs and I came up with this:
// main.js
requirejs.config({
enforceDefine: true,
urlArgs: "bust=" + (new Date()).getTime(),
baseUrl: "./js",
waitSeconds: 7,
paths: {
"jquery": [
'jquery'
],
"underscore": [
'underscore'
],
"backbone": [
'backbone'
],
"handlebars": [
'handlebars'
]
},
shim: {
"underscore": {
deps: [],
exports: "_"
},
"backbone": {
deps: ["jquery", "underscore"],
exports: "Backbone"
},
"handlebars": {
deps: [],
exports: "Handlebars"
}
} // End shim
}); // End config
// List all files; use 'define()' and not 'require()' because of shim
define([
'jquery',
'underscore',
'backbone',
'handlebars'
], function ($, _, Backbone, Handlebars)
{
console.log("$: " + typeof $);
console.log("_: " + typeof _);
console.log("Backbone: " + typeof Backbone);
console.log("Handlebars: " + typeof Handlebars);
}
); // End define
However, I viewed a video from Jesse Warden () and he seems to use this style for most of his code:
// main.js
requirejs.config({
urlArgs: "bust=" + (new Date()).getTime(),
baseUrl: "./js",
waitSeconds: 7,
packages: [
'main',
{
name: 'jquery',
location: 'libs/jquery',
main: 'jquery'
},
{
name: 'underscore',
location: 'libs/underscore',
main: 'underscore'
},
{
name: 'backbone',
location: 'libs/backbone',
main: 'backbone'
},
{
name: 'handlebars',
location: 'libs/handlebars',
main: 'handlebars'
}
]
}); // End config
So which is the proper way? Should I use paths
or packages
? Also, there is a modules
config. When do I use modules
?
When should I use paths
versus packages
in RequireJS? Is there a best practice for this or are there particular times when I should consider using one over the other?
I've followed the docs and I came up with this:
// main.js
requirejs.config({
enforceDefine: true,
urlArgs: "bust=" + (new Date()).getTime(),
baseUrl: "./js",
waitSeconds: 7,
paths: {
"jquery": [
'jquery'
],
"underscore": [
'underscore'
],
"backbone": [
'backbone'
],
"handlebars": [
'handlebars'
]
},
shim: {
"underscore": {
deps: [],
exports: "_"
},
"backbone": {
deps: ["jquery", "underscore"],
exports: "Backbone"
},
"handlebars": {
deps: [],
exports: "Handlebars"
}
} // End shim
}); // End config
// List all files; use 'define()' and not 'require()' because of shim
define([
'jquery',
'underscore',
'backbone',
'handlebars'
], function ($, _, Backbone, Handlebars)
{
console.log("$: " + typeof $);
console.log("_: " + typeof _);
console.log("Backbone: " + typeof Backbone);
console.log("Handlebars: " + typeof Handlebars);
}
); // End define
However, I viewed a video from Jesse Warden (http://css.dzone./articles/video-basics-requirejs) and he seems to use this style for most of his code:
// main.js
requirejs.config({
urlArgs: "bust=" + (new Date()).getTime(),
baseUrl: "./js",
waitSeconds: 7,
packages: [
'main',
{
name: 'jquery',
location: 'libs/jquery',
main: 'jquery'
},
{
name: 'underscore',
location: 'libs/underscore',
main: 'underscore'
},
{
name: 'backbone',
location: 'libs/backbone',
main: 'backbone'
},
{
name: 'handlebars',
location: 'libs/handlebars',
main: 'handlebars'
}
]
}); // End config
So which is the proper way? Should I use paths
or packages
? Also, there is a modules
config. When do I use modules
?
- From my limited experience you must use packages with dev jquery or it fails to load core.js and other dependencies from the baseUrl/ and not jquery/. – Corey Alix Commented Mar 20, 2015 at 18:35
1 Answer
Reset to default 10The word packages
refers to the standard CommonJS, because requirejs supports loading modules that are in a CommonJS Packages directory structure and the modules themselves should be in a module format that RequireJS can understand.
The paths config could be for a directory and for files (.js, requirejs modules ) also. Is a little confusing because as you stated you can use packages to load non standard CommonJS packages.
When do I use modules?
everything in requirejs declared within : define('name', callback);
is a module
Hope this answer helps.
本文标签: javascriptRequireJS when to use 39paths39 versus 39packages39Stack Overflow
版权声明:本文标题:javascript - RequireJS: when to use 'paths' versus 'packages' - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741251025a2365789.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论