Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AMD using require sugar syntax #111

Open
dagoss opened this issue Jun 16, 2017 · 0 comments
Open

AMD using require sugar syntax #111

dagoss opened this issue Jun 16, 2017 · 0 comments

Comments

@dagoss
Copy link

dagoss commented Jun 16, 2017

RequireJS allows some syntatic sugar so that instead of passing an array of dependencies, then declaring names for those dependencies in the factory in (hopefully) the correct order, you just class require, and then can use more familiar (to CommonJS people) require() calls:

define(function(require){
  var $ = require('jquery')
  var utilities = require('../app/utilities')
  // insert magic words here that make browser do things
})

To do this in a UMD friendly way, I think you'd do something like this:

(function (root, factory) {
  if (typeof define === 'function' && define.amd) {
    define(factory)
  } else {
    // Browser
    root.returnExports = factory()
  }
}(this, function (require) {
  if (typeof require === 'function' && define.amd){
    var $ = require('jquery')
  }
  // insert more magic words here
  return {}
}));

I don't know how prevalent this type is, but I find it much easier to write and read. Maybe this example could be covered?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant