문제

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?

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top