Permalink
Browse files

npm support and unknown module handling

  • Loading branch information...
1 parent ac68c5a commit f2a670eb45b80acd339f641d19c46c6d1b3d61a7 @afriggeri committed Jan 8, 2011
Showing with 125 additions and 34 deletions.
  1. +2 −0 .npmignore
  2. +69 −0 README.md
  3. +2 −3 example/Cakefile
  4. +1 −0 example/README.md
  5. +4 −0 example/build.js
  6. +14 −19 lib/ristretto.js
  7. +25 −0 package.json
  8. +8 −12 src/ristretto.coffee
View
@@ -0,0 +1,2 @@
+.DS_Store
+.git*
View
@@ -0,0 +1,69 @@
+Ristretto
+=========
+
+A simple dependency management and javascript concatenation library.
+
+Install
+-------
+
+ npm install ristretto
+
+Usage
+-----
+
+Ristretto `exports` only one method, namely `bundle`. Its options are passed as
+an object with the following keys.
+
+All keys but `main` are optional
+
+ * `main` : the main file to compile, all dependencies will be resolved and
+ bundled in the right order after being wrapped in a fake
+ CommonJS environment.
+ * `target` : javascript output file, if undefined, the function call returns
+ the output.
+ * `raw` : additionnal files to include without any wrapping/processing.
+ This can be useful to include libraries such as web frameworks
+ which are not CommonJS compatible. Those files are added in
+ order.
+ * `minify` : if set to `true`, will run the concatenated output through
+ [uglify](https://github.com/mishoo/UglifyJS)
+ * `libraries` : a module-filename mapping for absolute requires, e.g. `util`
+ which does not make sense in a browser context.
+ * `copyright` : a comment to be added to the top of the output file.
+
+Example
+-------
+
+ ristretto = require 'ristretto'
+ ristretto.bundle
+ target : 'target.js'
+ main : 'app.coffee'
+ raw : ['globals.js', 'jquery.min.js', 'etc.js']
+ minify : true
+ libraries :
+ util : 'libraries/util.js'
+ foo : process.ENV['HOME']+'/.node_libraries/foo'
+ copyright : """
+ ©2011 Your Name-Here
+ """
+
+License
+-------
+Copyright 2011 by Adrien Friggeri. All rights reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in the
+Software without restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
+Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
+AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -2,13 +2,12 @@ ristretto = require '../src/ristretto'
task 'build', 'compile sample app', (options) ->
ristretto.bundle
- target : 'output.js'
+ target : 'build.js'
main : 'src/app.coffee'
raw : ['raw/globals.js']
minify : true
libraries :
util : 'libraries/util.js'
copyright : """
- Ristretto sample © Adrien Friggeri
- MIT License
+ ©2011 Your Name-Here
"""
View
@@ -0,0 +1 @@
+A dummy application to illustrate the use of `ristretto`.
View

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

Oops, something went wrong.
View

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

Oops, something went wrong.
View
@@ -0,0 +1,25 @@
+{
+ "name": "ristretto",
+ "description": "A simple dependency management and javascript concatenation library.",
+ "keywords": ["javascript", "language", "coffeescript", "compiler"],
+ "version": "0.0.2",
+ "licenses": [{
+ "type": "MIT",
+ }],
+ "repository":{
+ "type":"git",
+ "url":"http://github.com/afriggeri/ristretto.git"
+ },
+ "author": "Adrien Friggeri (http://friggeri.net/)",
+ "main": "./lib/ristretto",
+ "directories": {
+ "lib": "lib"
+ },
+ "engines": {
+ "node": ">=0.3.0"
+ },
+ "dependencies": {
+ "coffee-script":"1.0.0",
+ "uglify-js":"0.0.1"
+ }
+}
View
@@ -4,6 +4,10 @@ path = require 'path'
constants = require 'constants'
class Ristretto
+ class @UnknownModule extends Error
+ constructor: (@req, @fileName) ->
+ @message = "Cannot find module `#{@req}` required in file `#{path.basename(@fileName)}`"
+
@extensions :
'.js' : (fileName) -> fs.readFileSync fileName, 'utf8'
'.coffee' : (fileName) -> coffee.compile(fs.readFileSync fileName, 'utf8')
@@ -45,7 +49,7 @@ class Ristretto
rank : 0
module.body = @load(fileName).replace /require\(('|")(.*?)\1\)/g, (_, _, req, pos, contents) =>
- depName = try @resolve(req, path.dirname(fileName)) catch e then throw new Ristretto.Error req, fileName
+ depName = try @resolve(req, path.dirname(fileName)) catch e then throw new Ristretto.UnknownModule req, fileName
dep = @add(@resolve(req, path.dirname(fileName)), req)
dep.children.push module
@@ -71,20 +75,12 @@ class Ristretto
})(_ristretto.require, _ristretto.modules["#{module.hash}"] = {exports:{}}, _ristretto.modules["#{module.hash}"].exports);
""" for module in (module for fileName, module of @modules).sort((a,b) -> a.rank - b.rank)).join('\n')
].join('\n')
-
-class Ristretto.Error extends Error
- constructor: (@req, @fileName) ->
- @message = "Unknown module `#{@req}` required in file `#{@fileName}`"
-
+
exports.bundle = (options) ->
- try
- output = (new Ristretto options).bundle()
- catch e
- console.log e.message
- process.exit 1
+ output = (new Ristretto options).bundle()
if options.minify?
- {uglify, parser} = require 'uglify'
+ {uglify, parser} = require 'uglify-js'
ast = parser.parse output
ast = uglify.ast_mangle ast
ast = uglify.ast_squeeze ast

0 comments on commit f2a670e

Please sign in to comment.