Skip to content
/ wrapjs Public

RequireJS plugin for wrapping regular scripts as AMD modules

License

Notifications You must be signed in to change notification settings

geddski/wrapjs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DEPRECATED

This plugin's wildest dreams came true and its functionality was added to RequireJS 2.0
Please upgrade your RequireJS and use its new shim config instead of wrap.js

wrap.js

version 0.2.2 A RequireJS plugin for wrapping regular scripts as AMD modules. This is useful for cases where regular scripts have dependencies, like jQuery plugins and Backbone. Depends on the text plugin

Usage

In your require.config use the wrapJS object to specify which scripts you want to wrap. Use the deps attribute to specify all the script's dependencies. Use the attach attribute to specify the global name of the library (think $, _, Backbone, etc) Use the path attribute to specify the location of the script to wrap. Alternatively use require's regular path config. The following example wraps backbone, declaring underscore and jQuery as dependencies:

require.config({
  wrapJS: {
    "backbone": {
      deps: ["underscore", "jquery"], //an array of the script's dependencies
      attach: "Backbone"
  }
});

Now use the wrap plugin to load the backbone (and its dependencies) like so:

require('wrap!backbone', function(Backbone){
  console.log(Backbone); //now returns Backbone, just as if Backbone were an AMD module
}

NOTE: As of RequireJS 1.0.5 you can reuse your app's main.js config as part of your build config, by using the new mainConfigFile setting: mainConfigFile:'path/to/main.js' So you only have to specify the wrapJS stuff once. Here's a sample app.build.js.

How It Works

During development, wrap.js basically does a nested require() for you, so that the script's dependencies are sure to load first. Then when you run the r.js build, an actual AMD module is generated based on your config. So say you were trying to wrap Backbone.js, and the Backbone.js script looks like this:

(function(){
  //stuff
}).call(this);

Now after the r.js build backbone looks like this:

define('wrap!backbone', ['jquery', 'underscore'], function(){
  (function(){
    //backbone stuff
  }).call(this);
  return (function () {
    var attach = Backbone;
    return (typeof attach === 'function') ? attach.apply(this) : attach;
  }());
});

NOTE: the generated AMD module contains an immediate function that resolves to and returns Backbone in the example above. Pure JS magic.

So there you have it. The wrap.js plugin turns any old script into an AMD module with just a little config.

More Examples

I've included several examples in the examples directory.

Use as Globals

By default the wrapped scripts are still available as globals. See as-globals.html.

Use as Modules

The wrapped scripts are also returned to your callback just like AMD modules. See as-modules.html.

Remove Globals

If you want to have the wrapped scripts no longer available as globals, you can remove the global—and do any other custom things—in a function you pass to the attach property. See as-modules-only.html.

Using the path option

Using the path attribute can help keep your config small. See use-path-option.html

Post-build example

To see an example of a generated module, see the build-example.js before and after.

About

RequireJS plugin for wrapping regular scripts as AMD modules

Resources

License

Stars

Watchers

Forks

Packages

No packages published