michaelficarra / commonjs-everywhere

:rainbow: minimal CommonJS browser bundler with aliasing, extensibility, and source maps
BSD 3-Clause "New" or "Revised" License
158 stars 21 forks source link

Better handling of `root` or a `core_modules` for unqualified requires. #106

Open AshHeskes opened 9 years ago

AshHeskes commented 9 years ago

Currently if you specify a root it makes all paths relative to it. IMO it would be better if unqualified requires were not made part of the relative entry point path. If the intention is to mimic the node environment node's core modules and anything in node_modules are the only requires that can have unqualified paths.

Since I expected cjs everywhere to use the root option for looking up unqualified requires only, if say for example you're using bower for dependency management I would want to specify my components dir as the source for unqualified requires. However I still want my entry point to be relative to the entry point dir.

for example in my typical directory structure....

+ js
    + components        // unqualified requires come from here, just like node_modules.
        + backbone
            index.js
    + libs
        dummy-lib.js
    + models
        foo-model.js
    + controllers
        foo-controller.js
    + app.js            // all requires outside of `components` need to use relative paths just like node. 

Where app.js would look like

var backbone       = require ( 'backbone' ),
    foo_model      = require ( './models/foo-model' ),
    foo_controller = require ( './controllers/foo-controller' );

module. exports = function (  ) { // do stuff maybe..... };

currently relative paths in app.js are being converted to be relative to root.

It would be great if cjs-everywhere could be configured this way. I find it a much more flexible approach to using common js in the browser where the package manager is not always npm, yet retains the same require semantics as node.

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/6622579-better-handling-of-root-or-a-core_modules-for-unqualified-requires?utm_campaign=plugin&utm_content=tracker%2F297897&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F297897&utm_medium=issues&utm_source=github).