-
Notifications
You must be signed in to change notification settings - Fork 183
ModuleLoader
Features that we want (in priority order):
- ability to load in core modules
- ability to load in modules from other extensions
- ability to shim browser modules (like jslint)
- Support for node_modules loading
- Possibility of customizing the object that is returned (to support passing extension context along for automatic unloading)
I have taken a look at three CommonJS module loaders:
A useful note: Link.js provides more robust parsing for dependencies (and also exports function names and such from its parse). This may be useful. Of note, the addition of Link.js to Inject fixed a bug in which a minifier renamed "require" to "a". Link.js can handle that. We may be able to use exported functions to feed JavaScriptCodeHints.
Cajon/RequireJS and Inject are both extensible. They are both not perfect for our needs. Neither one:
- would properly handle node_modules resolution. For example, consider the following scenario with files in node_modules:
- blarg/index.js
- blarg/node_modules/forble/index.js
- forble/index.js
- If an extension does
require("blarg"); require("forble");
, I could not see a way to modify Cajon or Inject to load the correct forble for the module
- In both cases, when they're taking a module ID and figuring out what that maps to, they do it synchronously. We need to be able to do this asynchronously because we need to poke around on disk for things (today, at least). We could get around this by generating a list of Brackets and extension files that we can synchronously search for matches. That may even be good for performance.
Inject has a further problem:
- It does not look like
require("otherModuleInExtension")
will work. It appears that thatrequire
would go back to the root -
require("./otherModuleInExtension")
would be the needed syntax. This is backwards incompatible for us, but is better for node compatibility. - Inject is a good deal larger, but part of that is Link.js which looks to be a win anyhow.
Inject's extensibility API almost allows us to manipulate module resolution as we would need to. If it passed in the "relative to" value, we could use that to figure out which module is appropriate.
Searching in the current directory first is not very compatible with the Node behavior. It's cleaner to state explicitly that you want a local module with "./". Perhaps we deprecate the current directory behavior?
- Current directory (as in require)
- Core modules
- If it begins with "./", "/" or "../", use the same mechanism as node
- Look in node_modules for the extension
- Look for other extension packages
- Look in other node_modules directories (in Brackets itself?)
- require("command/CommandHandler")
- Look for src/command/CommandHandler in core modules
- Look for src/command/CommandHandler.js
- require("underscore")
- assume underscore is installed in src/node_modules
- Look for src/underscore
- Look for src/underscore.js
- Look for src/underscore directory
- Look for src/node_modules/underscore
- Look for src/node_modules/underscore.js
- Look for src/node_modules/underscore/package.json
- Look for main in package.json
- It's there, so load underscore.js per package.json
- require("JavaScriptCodeHints") // an extension, default in this case
- Look for src/JavaScriptCodeHints
- Look for src/JavaScriptCodeHints.js
- Look for src/node_modules/JavaScriptCodeHints
- Look for src/node_modules/JavaScriptCodeHints.js
- give up
- require("shim:jQuery!thirdparty/jquery.min.js")
- Look for src/thirdparty/jquery.min.js
- Adds a shim for jQuery (magic? how does jQuery.noConflict get called? See if link.js offers assistance here. I think Inject has support for this)
Assume a jstools extension that looks like this:
-
main.js
-
jsutils.js
-
jslint.js
-
require("command/CommandHandler")
- Look for jstools/command (this is the one we should deprecate)
- Look for src/command
- Look for src/command/CommandHandler
- Look for src/command/CommandHandler.js
-
require("jsutils")
- Look for jstools/jsutils
- Look for jstools/jsutils.js
-
require("shim:JSLINT!jslint")
- Look for jstools/jslint
- Look for jstools/jslint.js
- Look for src/jslint
- Look for src/jslint.js
- Add shim that exports JSLINT (this one is not so magical)
- Look into the Google module server