Permalink
Browse files

Fallback to module/index, if necessary. Aligning more to node's requi…

…re()

For example, if you have the structure

app/
  views/
    test/
      index.coffee

You can require('app/views/test') and it will load 'app/views/test/index'.

If you did have an 'app/views/test.coffee' file, it wouldn't look for the index.
  • Loading branch information...
mattmccray committed Feb 11, 2011
1 parent 714a465 commit 52e862c590e1bc5ad001865d3a94f7db4694e72d
Showing with 21 additions and 4 deletions.
  1. +3 −3 lib/stitch.js
  2. +5 −0 src/stitch.coffee
  3. +2 −0 test/fixtures/default/foo/buz/index.coffee
  4. +11 −1 test/test_stitch.coffee
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -44,6 +44,11 @@ exports.Package = class Package
fn(module.exports, require, module);
exportCache[name] = module.exports;
return module.exports;
} else if (fn = modules[name +"/index"]) {
module = { id: name, exports: {} };
fn(module.exports, require, module);
exportCache[name] = module.exports;
return module.exports;
} else {
throw 'module \\'' + name + '\\' not found';
}
@@ -0,0 +1,2 @@
exports.buz= "BUZ"
View
@@ -5,7 +5,7 @@ stitch = require "stitch"
fixtureRoot = __dirname + "/fixtures"
fixtures = fixtureRoot + "/default"
altFixtures = fixtureRoot + "/alternate"
fixtureCount = 7
fixtureCount = 8
defaultOptions =
identifier: "testRequire"
@@ -157,3 +157,13 @@ module.exports =
module.x = "foo"
test.same "foo", testRequire("module").x
test.done()
"look for module index if necessary": (test) ->
test.expect 2
defaultPackage.compile (err, sources) ->
test.ok !err
eval sources
buz= testRequire("foo/buz").buz
test.same buz, "BUZ"
test.done()

0 comments on commit 52e862c

Please sign in to comment.