Question

The latest jQuery v1 (as of now: v1.10.2) finally uses CommonJS. This allows to use it as a node module, e.g. with webmake.

What is the preferred way of writing plugins for jQuery in a CommonJS compatible way?

The following seems logical to me:

  1. If CommonJS is not available fall back to other techniques (AMD, global objects)
  2. Require jQuery like:

    var $ = require('jQuery');
    
  3. Define new functions/objects (jQuery can be used with $)

  4. Export those new functions/objects by assigning them to module.exports, e.g.:

    module.exports.newFunc = newFunc;
    

Should I do it that way? Are there good alternatives?

Was it helpful?

Solution

Take a look at https://github.com/umdjs/umd - this is a wrapper for both AMD and CommonJS.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top