-
Notifications
You must be signed in to change notification settings - Fork 1
config-aliases-main #1
Comments
I think the solution here is not to use relative dependencies of the type './internal_dependency_name', but rather to be more explicit, as this is good Require.js practice anyway. Your example code would be // recipient/recipient.js relies on recipient/name.js
define( [ 'recipient/name' ], function( recipientName ) { |
You can change the config to:
Configure the path to point to the lib roots. That way you can use |
@necolas I like it, I like it. It allows relative dependencies. I've added |
+1 to @necolas' approach here. If you're going to have packages of code wherein you have multiple modules that all live in a common directory, it is not unreasonable to ask your users to configure a top-level path for the package of code. In fact, RequireJS does have a concept of setting up packages. http://requirejs.org/docs/api.html#packages has a bit more detail. Having used packages heavily with Dojo's AMD loader, this would be my choice with any AMD loader that supports it. Paths and packages are subtly different, but having it be formal packages instead of just path aliases feels nice and clean. |
Another approach I've started trying (which feels a bit more Node-like), is to set the requirejs.config({
baseUrl: 'bower_components',
paths: {
'app': 'custom_components',
}
}); This means you don't have to keep adding paths for every Bower component you have installed. |
@necolas I'm curious, why not use the component-path technique? |
Components can be more portable. Imagine you write your module to work on the server and client. You want your require statements to make sense in Node-land, e.g., I think the general pattern is to go from the library root. If you're using a lot of client-side modules, then setting the define(function(require, exports, module) {
var Greeter = require('greeter/greeter');
module.exports = recipient;
function recipient() {
// ...
}
}); …at least, that's what I'm thinking based on my experience so far. There's a lot I don't know about this stuff. |
My advice:
My larger answer: the package manager should be doing this work, and lay out files to avoid big config blocks. Big config blocks are an antipattern. ES6 modules will have similar baseUrl + moduleID + '.js' default convention lookup for browsers that AMD systems do, so supporting working with that convention within the package manager would be future facing. What does that mean for the package manager when it installs a package:
Where the package manager creates bower_components/recipient.js with this content: define(['recipient/recipient'], function (main) { return main; }); Then, I also suggest using the base config that @necolas mentions: set baseUrl to the packages area and have one paths entry to the app code, and that app code uses relative IDs to refer to its code. An example of that type of project layout (not using bower, just plain requirejs). This way, there is no need to touch the config again for finding modules. The above approach for installing packages is what volo does. Bower could support an amd and baseUrl config as volo does, which would instruct it how to do this work. Bower supports something like baseUrl via the bowerrc 'directory', but I suggest folding that into the bower.json metadata file for the project instead of another file. The Later, when es6 comes out, maybe support an An easy intermediate approach for bower could be to just always do the path 2 approach of writing an adapter module for every package even if it is just one JS module. Longer term though I suggest trying to support just single file installs in the baseUrl to avoid the extra module hop, as I expect the bulk of basic dependencies to be single module file packages. |
@jrburke Thanks for much for going into detail about this. Your expertise is much appreciated. |
The config-aliases-main example is broken, but if you know a way resolve it, and/or you believe it to be the best way to implement the example, please chime in.
The text was updated successfully, but these errors were encountered: