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?

Share Improve this question edited Aug 6, 2013 at 5:38 JsusSalv asked Aug 6, 2013 at 1:21 JsusSalvJsusSalv 5171 gold badge8 silver badges22 bronze badges 1
  • 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
Add a ment  | 

1 Answer 1

Reset to default 10

The 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