Permalink
Browse files

stitch.compile() -> stitch()

  • Loading branch information...
1 parent b497ccc commit 25b81c83170abab04c132e4754278176979ac8ec @sstephenson committed Oct 2, 2010
Showing with 87 additions and 87 deletions.
  1. +34 −34 lib/stitch.js
  2. +48 −48 src/stitch.coffee
  3. +5 −5 test/test_stitch.coffee
View

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

Oops, something went wrong.
View
@@ -36,7 +36,48 @@ forEachAsync = (elements, callback) ->
for element in elements
callback next, element
-exports.walkTree = walkTree = (directory, callback) ->
+module.exports = stitch = (options, callback) ->
+ options.identifier ?= 'require'
+ options.sourcePaths ?= ['lib']
+ options.requirePaths ?= ['lib']
+
+ gatherSources options, (err, sources) ->
+ if err
+ callback err
+ else
+ result = """
+ var #{options.identifier} = (function(modules) {
+ var exportCache = {};
+ return function require(name) {
+ var module = exportCache[name];
+ var fn;
+ if (module) {
+ return module;
+ } else if (fn = modules[name]) {
+ module = { id: name, exports: {} };
+ fn(module.exports, require, module);
+ exportCache[name] = module.exports;
+ return module.exports;
+ } else {
+ throw 'module \\'' + name + '\\' not found';
+ }
+ }
+ })({
+ """
+
+ index = 0
+ for name, {filename, source} of sources
+ result += if index++ is 0 then "" else ", "
+ result += sys.inspect name
+ result += ": function(exports, require, module) {#{source}}"
+
+ result += """
+ });\n
+ """
+
+ callback null, result
+
+stitch.walkTree = walkTree = (directory, callback) ->
fs.readdir directory, (err, files) ->
if err then return callback err
@@ -58,7 +99,7 @@ exports.walkTree = walkTree = (directory, callback) ->
else
callback err, null
-exports.getFilesInTree = getFilesInTree = (directory, callback) ->
+stitch.getFilesInTree = getFilesInTree = (directory, callback) ->
files = []
walkTree directory, (err, filename) ->
if err
@@ -77,7 +118,7 @@ compilerIsAvailableFor = (filename, options) ->
return true if name is extension
false
-exports.compileFile = compileFile = (path, options, callback) ->
+stitch.compileFile = compileFile = (path, options, callback) ->
compilers = getCompilersFrom options
extension = extname(path).slice(1)
@@ -95,7 +136,7 @@ exports.compileFile = compileFile = (path, options, callback) ->
else
callback "no compiler for '.#{extension}' files"
-exports.expandPaths = expandPaths = (sourcePaths, callback) ->
+stitch.expandPaths = expandPaths = (sourcePaths, callback) ->
paths = []
forEachAsync sourcePaths, (next, sourcePath) ->
@@ -109,7 +150,7 @@ exports.expandPaths = expandPaths = (sourcePaths, callback) ->
else
callback null, paths
-exports.getRelativePath = getRelativePath = (requirePaths, path, callback) ->
+stitch.getRelativePath = getRelativePath = (requirePaths, path, callback) ->
path = normalize path
expandPaths requirePaths, (err, expandedPaths) ->
@@ -125,7 +166,7 @@ exports.getRelativePath = getRelativePath = (requirePaths, path, callback) ->
callback "#{path} isn't in the require path"
-exports.stripExtension = stripExtension = (filename) ->
+stitch.stripExtension = stripExtension = (filename) ->
extension = extname filename
filename.slice 0, -extension.length
@@ -167,7 +208,7 @@ gatherSourcesFromPath = (sourcePath, options, callback) ->
else sources[key] = value
callback null, sources
-exports.gatherSources = gatherSources = (options, callback) ->
+stitch.gatherSources = gatherSources = (options, callback) ->
{sourcePaths} = options
sources = {}
@@ -181,44 +222,3 @@ exports.gatherSources = gatherSources = (options, callback) ->
next()
else
callback null, sources
-
-exports.compile = (options, callback) ->
- options.identifier ?= 'require'
- options.sourcePaths ?= ['lib']
- options.requirePaths ?= ['lib']
-
- gatherSources options, (err, sources) ->
- if err
- callback err
- else
- result = """
- var #{options.identifier} = (function(modules) {
- var exportCache = {};
- return function require(name) {
- var module = exportCache[name];
- var fn;
- if (module) {
- return module;
- } else if (fn = modules[name]) {
- module = { id: name, exports: {} };
- fn(module.exports, require, module);
- exportCache[name] = module.exports;
- return module.exports;
- } else {
- throw 'module \\'' + name + '\\' not found';
- }
- }
- })({
- """
-
- index = 0
- for name, {filename, source} of sources
- result += if index++ is 0 then "" else ", "
- result += sys.inspect name
- result += ": function(exports, require, module) {#{source}}"
-
- result += """
- });\n
- """
-
- callback null, result
View
@@ -133,7 +133,7 @@ module.exports =
"compile generates valid javascript": (test) ->
test.expect 2
- stitch.compile defaultOptions, (err, sources) ->
+ stitch defaultOptions, (err, sources) ->
test.ok !err
eval sources
test.ok typeof testRequire is "function"
@@ -142,7 +142,7 @@ module.exports =
"compile module with custom exports": (test) ->
test.expect 2
- stitch.compile defaultOptions, (err, sources) ->
+ stitch defaultOptions, (err, sources) ->
eval sources
result = testRequire("custom_exports")
test.ok typeof result is "function"
@@ -152,15 +152,15 @@ module.exports =
"compile module with exported property": (test) ->
test.expect 1
- stitch.compile defaultOptions, (err, sources) ->
+ stitch defaultOptions, (err, sources) ->
eval sources
test.same "bar", testRequire("exported_property").foo
test.done()
"compile module with requires": (test) ->
test.expect 3
- stitch.compile defaultOptions, (err, sources) ->
+ stitch defaultOptions, (err, sources) ->
eval sources
module = testRequire("module")
test.same "bar", module.foo
@@ -171,7 +171,7 @@ module.exports =
"runtime require only loads files once": (test) ->
test.expect 2
- stitch.compile defaultOptions, (err, sources) ->
+ stitch defaultOptions, (err, sources) ->
eval sources
module = testRequire("module")

0 comments on commit 25b81c8

Please sign in to comment.