componentjs/builder2.js — pull request #75 by bogdan-dumitru (2014-09-01 10:10:46+00:00)
Title: Add support for nested local dependencies

We want to be able to require local components that are nested inside of the defined paths.

So, for example, instead of having the base ``component.json`` paths be ``['actions', 'stores']``.
And then (because the components would live in the same namespace), have the components:
- ``modal_actions`` (which lives in ``actions/modal_actions``
- ``modal_store`` (which lives in ``stores/modal_store``)

And do: ``require('modal_actions')`` or ``require('modal_stores')``.

---
We want to be able to have the ``component.json`` path be ``['./']`` (example).
The components would be namespaced by the folder so:
- ``actions/modal`` (which lives in ``actions/modal``)
- ``stores/modal`` (which lives in ``stores/modal`` )

And then do: ``require('actions/modal')`` or ``require('stores/modal`)``

> PS: there's also a new fixture in the pull-request, called js-nested-locals, which may help in understanding where we're coming from.

---

Currently that's not possible because of [lookup for local dependencies](https://github.com/componentjs/builder2.js/blob/master/lib/builders/scripts.js#L398):
```javascript
if (branch.locals[target]) return branch.locals[target].canonical + tail;
```
Which actually ends up doing something weird. At the beginning of the function it splits the target:
```javascript
var frags = target.split('/');
var reference = frags[0];
var tail = frags.length > 1
? ('/' + frags.slice(1).join('/'))
: ''
```

And then it looks up the locals by ``target`` but it still adds the ``tail``.
So when running our example without our fix, requiring something like ``nested/boot`` ended up being rewritten as ``nested/boot/boot``.

So even without our fix, it feels like the lookup should happen by ``reference`` and the tail would be added to that.