• Stars
    star
    261
  • Rank 156,630 (Top 4 %)
  • Language
    JavaScript
  • License
    Other
  • Created over 13 years ago
  • Updated almost 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A RequireJS+jQuery sample project

RequireJS + jQuery

This project shows how jQuery can be used with RequireJS. It includes a sample project that you can use as a template to get started.

See the Use with jQuery page on the RequireJS site for more background on the sample project. The RequireJS site also has a link to download this sample project.

The sample project uses a require-jquery.js file which is just a simple combination of RequireJS and jQuery.

This project will be kept relatively up to date with the latest jQuery and RequireJS files as they are released.

Better integrations

With RequireJS, scripts can load in a different order than the order they are specified. This can cause problems for jQuery plugins that assume jQuery is already loaded. Using the combined RequireJS + jQuery file makes sure jQuery is in the page before any jQuery plugins load.

However, the ideal use of jQuery with RequireJS is to load it as a module, since it registers as an AMD module.

Here are some alternate ways to use RequireJS and jQuery together in a way that recognizes jQuery plugin constraints.

Wrap all scripts with define()

Ideally, all the scripts you use would wrap themselves in a define() call so that RequireJS can understand what dependencies need to be loaded before defining the script's functionality.

For scripts that just need jQuery, you can wrap the code in a wrapper like this:

    define(['jquery'], function ($) {
        //Script contents go here.
    });

For more information on using define() to declare the codes as Asynchronous Module Definition (AMD) modules, see the RequireJS API page.

If you do not control the code in question, you can ask the code's author if they could optionally call define() in their code. As of jQuery 1.7, jQuery itself optionally calls define() if it is available. The umdjs project has some resources and examples to help code authors update their code to this pattern.

Use shim config

In RequireJS 2.0, there is a shim config option that allows specifying the dependencies for a script without having to modify the script. For simple jQuery plugins, it is enough to just specify an array that mentions jQuery for the shim value:

requirejs.config({
    shim: {
        'plugin.one': ['jquery'],
        'plugin.two': ['jquery']
    }
});

Be sure to read the build notes for shim. In particular, if using "shim" config you must build in jquery.js into your built file.

Use step plugin

Using the shim config is the recommended way to use non-module code with RequireJS and jQuery. However, there are some situations in which the step plugin may be useful. See the step plugin README for more information.

More Repositories

1

requirejs

A file and module loader for JavaScript
JavaScript
12,930
star
2

r.js

Runs RequireJS in Node and Rhino, and used to run the RequireJS optimizer
JavaScript
2,568
star
3

almond

A minimal AMD API implementation for use after optimized builds
JavaScript
2,414
star
4

text

An AMD loader plugin for loading text resources
JavaScript
932
star
5

example-multipage

Example RequireJS-based project that has multiple pages that share a common set of modules.
JavaScript
840
star
6

example-multipage-shim

Example RequireJS-based project that has multiple pages that share a common set of modules with shim config
JavaScript
382
star
7

require-cs

An AMD loader plugin for CoffeeScript
JavaScript
335
star
8

alameda

AMD loader, like requirejs, but with promises and for modern browsers
JavaScript
195
star
9

example-jquery-shim

Example project that uses jQuery and jQuery plugins via shim config
JavaScript
190
star
10

domReady

An AMD loader plugin for detecting DOM ready
JavaScript
182
star
11

example-jquery-cdn

Example project that uses jQuery and jQuery plugins wrapped as modules
JavaScript
151
star
12

cajon

JavaScript module loader for the browser that can load CommonJS/node and AMD modules
JavaScript
117
star
13

i18n

An AMD loader plugin for loading internationalization/localization string resources
JavaScript
98
star
14

xrayquire

An auxillary script that gives extra info on the use of requirejs in a web page.
JavaScript
74
star
15

example-libglobal

Use AMD modules to develop a JS library that builds to a lib that can be used by "globals only" scripts or as an AMD module
JavaScript
61
star
16

element

An AMD loader plugin for custom elements
JavaScript
56
star
17

requirejs-bower

Repo hold assets for publishing to the bower package manager.
JavaScript
34
star
18

requirejs-npm

Wrappers to allow using RequireJS-related scripts
JavaScript
22
star
19

step

AMD loader plugin that takes a step config to know how to sequential load some files
JavaScript
20
star
20

prim

Promise lib for use inside requirejs-related projects
JavaScript
7
star
21

requirejs-branding

Branding/images/design for RequireJS web site
CSS
6
star
22

browsertests

Standalone browser capability tests to help inform what is possible in a loader
PHP
3
star
23

alameda-prim

The alameda AMD loader that includes private promise shim
JavaScript
3
star
24

requirejs.github.io

requirejs.org site
HTML
2
star
25

requirejs-nuget

Artifact used for pushing to NuGet
JavaScript
2
star