Permalink
Browse files

Modules can be defined at runtime, and multiple bundles may live in t…

…he same package
  • Loading branch information...
sstephenson committed Feb 20, 2011
1 parent 7e24a97 commit 3c4f3e9aa2847e9cea3cdb06492ea781ef93e555
Showing with 85 additions and 32 deletions.
  1. +5 −4 lib/stitch.js
  2. +25 −19 src/stitch.coffee
  3. +1 −0 test/fixtures/additional/foo/bar.js
  4. +1 −0 test/fixtures/additional/hello.js
  5. +53 −9 test/test_stitch.coffee
View

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

Oops, something went wrong.
View
@@ -19,11 +19,11 @@ catch err
exports.Package = class Package
constructor: (config) ->
@identifier = config.identifier or 'require'
@paths = config.paths or ['lib']
@identifier = config.identifier ? 'require'
@paths = config.paths ? ['lib']
@compilers = _.extend {}, defaultCompilers, config.compilers
@cache = config.cache or true
@cache = config.cache ? true
@mtimeCache = {}
@compileCache = {}
@@ -32,23 +32,29 @@ exports.Package = class Package
return callback err if err
result = """
var #{@identifier} = (function(modules) {
var exportCache = {};
return function require(name) {
var module = exportCache[name];
var fn;
if (module) {
return module;
} else if (fn = modules[name] || 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';
}
(function(/*! Stitch !*/) {
if (!this.#{@identifier}) {
var modules = {}, cache = {}, require;
this.#{@identifier} = require = function(name) {
var module = cache[name], fn;
if (module) {
return module;
} else if ((fn = modules[name]) || (fn = modules[name + '/index'])) {
module = {id: name, exports: {}};
fn(module.exports, require, module);
cache[name] = module.exports;
return module.exports;
} else {
throw 'module \\'' + name + '\\' not found';
}
};
require.define = function(bundle) {
for (var key in bundle)
modules[key] = bundle[key];
};
}
})({
return this.#{@identifier}.define;
}).call(this)({
"""
index = 0
@@ -0,0 +1 @@
exports.filename = "additional/foo/bar.js";
@@ -0,0 +1 @@
exports.hello = "hello";
View
@@ -5,6 +5,7 @@ stitch = require "stitch"
fixtureRoot = __dirname + "/fixtures"
fixtures = fixtureRoot + "/default"
altFixtures = fixtureRoot + "/alternate"
addlFixtures = fixtureRoot + "/additional"
fixtureCount = 8
defaultOptions =
@@ -13,8 +14,14 @@ defaultOptions =
defaultPackage = stitch.createPackage defaultOptions
additionalOptions =
identifier: "testRequire"
paths: [addlFixtures]
additionalPackage = stitch.createPackage additionalOptions
alternateOptions =
paths: [altFixtures]
paths: [altFixtures]
alternatePackage = stitch.createPackage alternateOptions
@@ -111,7 +118,7 @@ module.exports =
defaultPackage.compile (err, sources) ->
test.ok !err
eval sources
test.ok typeof testRequire is "function"
test.ok typeof @testRequire is "function"
test.done()
"compile module with custom exports": (test) ->
@@ -120,7 +127,7 @@ module.exports =
defaultPackage.compile (err, sources) ->
test.ok !err
eval sources
result = testRequire("custom_exports")
result = @testRequire("custom_exports")
test.ok typeof result is "function"
test.same "foo", result()
test.done()
@@ -131,7 +138,7 @@ module.exports =
defaultPackage.compile (err, sources) ->
test.ok !err
eval sources
test.same "bar", testRequire("exported_property").foo
test.same "bar", @testRequire("exported_property").foo
test.done()
"compile module with requires": (test) ->
@@ -140,7 +147,7 @@ module.exports =
defaultPackage.compile (err, sources) ->
test.ok !err
eval sources
module = testRequire("module")
module = @testRequire("module")
test.same "bar", module.foo
test.same "foo", module.bar()
test.same "biz", module.baz
@@ -152,10 +159,10 @@ module.exports =
defaultPackage.compile (err, sources) ->
test.ok !err
eval sources
module = testRequire("module")
module = @testRequire("module")
test.ok !module.x
module.x = "foo"
test.same "foo", testRequire("module").x
test.same "foo", @testRequire("module").x
test.done()
"look for module index if necessary": (test) ->
@@ -164,6 +171,43 @@ module.exports =
defaultPackage.compile (err, sources) ->
test.ok !err
eval sources
buz= testRequire("foo/buz").buz
test.same buz, "BUZ"
buz = @testRequire("foo/buz").buz
test.same buz, "BUZ"
test.done()
"modules can be defined at runtime": (test) ->
test.expect 3
defaultPackage.compile (err, sources) ->
test.ok !err
eval sources
raised = false
try
@testRequire("frob")
catch e
raised = true
test.ok raised
@testRequire.define
"frob": (exports, require, module) ->
exports.frob = require("foo/buz").buz
test.same "BUZ", @testRequire("frob").frob
test.done()
"multiple packages may share the same require namespace": (test) ->
test.expect 5
defaultPackage.compile (err, sources) ->
test.ok !err
eval sources
additionalPackage.compile (err, sources) =>
test.ok !err
eval sources
test.same "hello", @testRequire("hello").hello
test.same "additional/foo/bar.js", @testRequire("foo/bar").filename
test.same "biz", @testRequire("foo/bar/baz").baz;
test.done()

0 comments on commit 3c4f3e9

Please sign in to comment.