requirejs / almond

A minimal AMD API implementation for use after optimized builds
Other
2.42k stars 169 forks source link

Added require.defined() to replicate require.js #81

Closed nicjansma closed 10 years ago

nicjansma commented 10 years ago

I use the (undocumented) RequireJS require.defined() function in my web app to dynamically check if the current view has any code to run. For example, I have AMD modules that are named like views/foo, and my app.js checks to see if views/foo exists. If so, it calls .init() on that module.

For example:

if (require.defined('views/' + viewName)) {
    require('views/' + viewName).init();
}

The require.defined() function isn't exposed in the almond wrapper, just require._defined (a map of views).

This PR adds a simple require.defined() function to match requirejs's.

ooxi commented 10 years ago

While almond is aiming at providing a minimal AMD implementation I think the patch is both small and is useful enough to justify the addition.

nicjansma commented 10 years ago

Any other concerns? Very simple change :)

jrburke commented 10 years ago

This is not quite enough: the one in requirejs allows using relative IDs, require.defined('./something'). For that to work, it is best to do this work in makeRequire, and likely using makeMap internally to the defined call to get the absolute ID. Relevant pointer from require.js code

If you want to look at updating to that pathway, we can re-evaluate. I am still a bit on the fence though. As @ooxi mentioned, this is API provider is supposed to be small for "just bundle up some modules to use as a unit" sort of thing, where I would not expect require.defined() to be useful. Each one of these things that are added increases the download size of almond for everyone.

jrburke commented 10 years ago

Closing for now given previous comments.