• Stars
    star
    190
  • Rank 203,739 (Top 5 %)
  • Language
    JavaScript
  • License
    Other
  • Created over 11 years ago
  • Updated over 6 years ago

Reviews

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

Repository Details

Example project that uses jQuery and jQuery plugins via shim config

Example of using jQuery and require.js with a shim config.

This example shows one way to load jQuery and jQuery plugins with require.js. jQuery itself registers as an AMD module and can easily be loaded. Most plugins, however, do not register as AMD modules, and therefore, require.js doesn't know that the plugins need jQuery to be loaded.

If the plugins you use all call define() to declare their dependencies, then you may want to use the example that uses jQuery from a CDN instead.

The most important part of this example is the app.js file, which specifies the shim configuration for the plugins.

If you want IE6-8 support, clone this repo, but replace the jQuery file with the latest jQuery 1.x release. The jQuery 2 file used in this project does not work with those browsers.

Project structure

tools/

  • build.js
  • r.js

www/app.html

www/js/

  • app.js
  • lib/
    • jquery.js
    • jquery.alpha.js
    • jquery.beta.js
    • require.js
  • app/
    • main.js

How it's set up

The main file of this setup is www/js/app.js. It is loaded from app.html by this line:

<script data-main="js/app" src="js/lib/require.js"></script>

app.js is mainly about configuration. The shim configuration specifies jQuery as a dependency for jQuery.alpha and jQuery.beta. Finally, our main code is loaded at the bottom of the file:

requirejs.config({
    "baseUrl": "js/lib",
    "paths": {
      "app": "../app"
    },
    "shim": {
        "jquery.alpha": ["jquery"],
        "jquery.beta": ["jquery"]
    }
});

// Load the main app module to start the app
requirejs(["app/main"]);

App/main.js is where the app logic is:

define(["jquery", "jquery.alpha", "jquery.beta"], function($) {
    //the jquery.alpha.js and jquery.beta.js plugins have been loaded.
    $(function() {
        $('body').alpha().beta();
    });
});

How to see it in action

Just serve up the www/ folder using any web server you'd like. To get you set up quickly, we've included a node.js static file server in tools/. Start the server by typing node tools/server.js from the command line, and then go to localhost:8888/www/app.html in your browser.

How to optimize it using r.js

To use the optimizer, you need node.js or Java 6 installed. These instructions assume Node is being used. See the Optimization page for more information.

r.js and a build configuration is included in the tools/ folder. To build, navigate to tools/ and type node r.js -o build.js. You will find the built product in the www-build folder. If you serve that directory instead, you can see in the network panel of the web developer tools that the files aren't loaded separately any more.

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

require-jquery

A RequireJS+jQuery sample project
JavaScript
261
star
9

alameda

AMD loader, like requirejs, but with promises and for modern browsers
JavaScript
195
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