Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 6244f3fc645e6601da51f4c89fdb6eea261f2675 @afriggeri committed Jan 7, 2011
@@ -0,0 +1,5 @@
+fs = require 'fs'
+coffee = require 'coffee-script'
+
+task 'build', 'build ristretto', (options) ->
+ fs.writeFileSync 'lib/ristretto.js', coffee.compile(fs.readFileSync('src/ristretto.coffee', 'utf8'))
@@ -0,0 +1,9 @@
+fs = require 'fs'
+ristretto = require '../src/ristretto'
+
+task 'build', 'compile sample app', (options) ->
+ fs.writeFileSync 'output.js', ristretto.bundle
+ main : 'src/app.coffee'
+ raw : ['raw/globals.js']
+ libraries :
+ util : 'libraries/util.js'
@@ -0,0 +1,4 @@
+exports.debug = console.debug
+exports.log = console.log
+exports.puts = console.log
+exports.error = console.error
@@ -0,0 +1,45 @@
+function WORLD(){
+ return 'outside world';
+}
+;var _ristretto={
+ modules:{},
+ require:function(id){return _ristretto.modules[id].exports}
+};
+//util [3]
+;(function(require, module, exports){
+exports.debug = console.debug
+exports.log = console.log
+exports.puts = console.log
+exports.error = console.error
+})(_ristretto.require, _ristretto.modules["util [3]"] = {exports:{}}, _ristretto.modules["util [3]"].exports);
+//./bang [2]
+;(function(require, module, exports){
+(function() {
+ var util;
+ util = require('util [3]');
+ util.puts('got `!`');
+ exports.bang = '!';
+}).call(this);
+
+})(_ristretto.require, _ristretto.modules["./bang [2]"] = {exports:{}}, _ristretto.modules["./bang [2]"].exports);
+//./message [1]
+;(function(require, module, exports){
+var bang = require('./bang [2]').bang;
+
+if (typeof WORLD == 'undefined'){
+ var world = 'inside world';
+} else {
+ var world = WORLD();
+}
+exports.message = "hello "+world+" "+bang;
+})(_ristretto.require, _ristretto.modules["./message [1]"] = {exports:{}}, _ristretto.modules["./message [1]"].exports);
+//./main [0]
+;(function(require, module, exports){
+(function() {
+ var message, util;
+ message = require('./message [1]').message;
+ util = require('util [3]');
+ util.puts(message);
+}).call(this);
+
+})(_ristretto.require, _ristretto.modules["./main [0]"] = {exports:{}}, _ristretto.modules["./main [0]"].exports);
@@ -0,0 +1,3 @@
+function WORLD(){
+ return 'outside world';
+}
@@ -0,0 +1,4 @@
+message = require('./message').message
+util = require 'util'
+
+util.puts message
@@ -0,0 +1,4 @@
+util = require 'util'
+
+util.puts 'got `!`'
+exports.bang = '!'
@@ -0,0 +1,8 @@
+var bang = require('./bang').bang;
+
+if (typeof WORLD == 'undefined'){
+ var world = 'inside world';
+} else {
+ var world = WORLD();
+}
+exports.message = "hello "+world+" "+bang;
@@ -0,0 +1,137 @@
+(function() {
+ var Ristretto, coffee, constants, fs, path;
+ var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
+ fs = require('fs');
+ coffee = require('coffee-script');
+ path = require('path');
+ constants = require('constants');
+ Ristretto = (function() {
+ Ristretto.extensions = {
+ '.js': function(fileName) {
+ return fs.readFileSync(fileName, 'utf8');
+ },
+ '.coffee': function(fileName) {
+ return coffee.compile(fs.readFileSync(fileName, 'utf8'));
+ }
+ };
+ function Ristretto(options) {
+ var _ref, _ref2, _ref3;
+ if (options == null) {
+ options = {};
+ }
+ this.main = (_ref = options.main) != null ? _ref : ((function() {
+ throw new Error('No entry point specified');
+ })());
+ this.libraries = (_ref2 = options.libraries) != null ? _ref2 : {};
+ this.raw = (_ref3 = options.raw) != null ? _ref3 : [];
+ this.modules = {};
+ this.add(path.join(process.cwd(), this.main), "./main");
+ }
+ Ristretto.prototype.load = function(fileName) {
+ return Ristretto.extensions[path.extname(fileName)](fileName);
+ };
+ Ristretto.prototype.resolve = function(req, dir) {
+ var failure, reqPath;
+ failure = function() {
+ throw new Error("Unknown module `" + req + "`");
+ };
+ reqPath = (function() {
+ var _ref;
+ switch (req.charAt(0)) {
+ case '/':
+ return req;
+ case '.':
+ return path.normalize(path.join(dir, req));
+ default:
+ return (_ref = this.libraries[req]) != null ? _ref : failure();
+ }
+ }).call(this);
+ if ((path.existsSync(reqPath)) && (fs.statSync(reqPath)).isDirectory()) {
+ try {
+ return this.resolve(path.join(reqPath, 'index'), reqPath);
+ } catch (e) {
+ return failure();
+ }
+ } else {
+ if (path.existsSync(reqPath + '.js')) {
+ return reqPath + '.js';
+ }
+ if (path.existsSync(reqPath + '.coffee')) {
+ return reqPath + '.coffee';
+ }
+ return failure();
+ }
+ };
+ Ristretto.prototype.add = function(fileName, moduleName) {
+ var module, parent;
+ if (fileName in this.modules) {
+ return this.modules[fileName];
+ }
+ module = this.modules[fileName] = {
+ hash: moduleName + ' [' + (Object.keys(this.modules)).length + ']',
+ children: [],
+ parents: [],
+ rank: 0
+ };
+ module.body = this.load(fileName).replace(/require\(('|")(.*?)\1\)/g, __bind(function(_, _, req) {
+ var dep;
+ dep = this.add(this.resolve(req, path.dirname(fileName)), req);
+ dep.children.push(module);
+ module.parents.push(dep);
+ return "require('" + dep.hash + "')";
+ }, this));
+ if (module.parents.length) {
+ module.rank = 1 + Math.max.apply({}, (function() {
+ var _i, _len, _ref, _results;
+ _ref = module.parents;
+ _results = [];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ parent = _ref[_i];
+ _results.push(parent.rank);
+ }
+ return _results;
+ })());
+ }
+ return module;
+ };
+ Ristretto.prototype.bundle = function() {
+ var fileName, module;
+ return [
+ ((function() {
+ var _i, _len, _ref, _results;
+ _ref = this.raw;
+ _results = [];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ fileName = _ref[_i];
+ _results.push(this.load(fileName));
+ }
+ return _results;
+ }).call(this)).join(';\n'), ";var _ristretto={\n modules:{},\n require:function(id){return _ristretto.modules[id].exports}\n};", ((function() {
+ var _i, _len, _ref, _results;
+ _ref = ((function() {
+ var _ref, _results;
+ _ref = this.modules;
+ _results = [];
+ for (fileName in _ref) {
+ module = _ref[fileName];
+ _results.push(module);
+ }
+ return _results;
+ }).call(this)).sort(function(a, b) {
+ return a.rank - b.rank;
+ });
+ _results = [];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ module = _ref[_i];
+ _results.push("//" + module.hash + "\n;(function(require, module, exports){\n" + module.body + "\n})(_ristretto.require, _ristretto.modules[\"" + module.hash + "\"] = {exports:{}}, _ristretto.modules[\"" + module.hash + "\"].exports);");
+ }
+ return _results;
+ }).call(this)).join('\n')
+ ].join('\n');
+ };
+ return Ristretto;
+ })();
+ exports.bundle = function(options) {
+ return (new Ristretto(options)).bundle();
+ };
+}).call(this);
@@ -0,0 +1,74 @@
+fs = require 'fs'
+coffee = require 'coffee-script'
+path = require 'path'
+constants = require 'constants'
+
+class Ristretto
+ @extensions :
+ '.js' : (fileName) -> fs.readFileSync fileName, 'utf8'
+ '.coffee' : (fileName) -> coffee.compile(fs.readFileSync fileName, 'utf8')
+
+ constructor : (options={}) ->
+ @main = options.main ? (throw new Error 'No entry point specified')
+ @libraries = options.libraries ? {}
+ @raw = options.raw ? []
+ @modules = {}
+
+ @add path.join(process.cwd(), @main), "./main"
+
+ load : (fileName) ->
+ Ristretto.extensions[path.extname fileName] fileName
+
+ resolve : (req, dir) ->
+ failure = -> (throw new Error "Unknown module `#{req}`")
+
+ reqPath = switch req.charAt 0
+ when '/' then req
+ when '.' then path.normalize(path.join dir, req)
+ else @libraries[req] ? failure()
+
+ if (path.existsSync reqPath) and (fs.statSync reqPath).isDirectory()
+ try @resolve(path.join(reqPath, 'index'), reqPath) catch e then failure()
+ else
+ return reqPath if path.existsSync reqPath
+ return reqPath+'.js' if path.existsSync reqPath+'.js'
+ return reqPath+'.coffee' if path.existsSync reqPath+'.coffee'
+ failure()
+
+ add : (fileName, moduleName) ->
+ return @modules[fileName] if fileName of @modules
+
+ module = @modules[fileName] =
+ hash : moduleName+' ['+(Object.keys @modules).length+']'
+ children : []
+ parents : []
+ rank : 0
+
+ module.body = @load(fileName).replace /require\(('|")(.*?)\1\)/g, (_, _, req) =>
+ dep = @add(@resolve(req, path.dirname(fileName)), req)
+ dep.children.push module
+ module.parents.push dep
+ "require('#{dep.hash}')"
+ module.rank = 1 + Math.max.apply({}, (parent.rank for parent in module.parents)) if module.parents.length
+
+ return module
+
+ bundle : ->
+ [
+ (@load fileName for fileName in @raw).join(';\n'),
+ """
+ ;var _ristretto={
+ modules:{},
+ require:function(id){return _ristretto.modules[id].exports}
+ };
+ """,
+ ("""
+ //#{module.hash}
+ ;(function(require, module, exports){
+ #{module.body}
+ })(_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')
+
+
+exports.bundle = (options) -> (new Ristretto options).bundle()

0 comments on commit 6244f3f

Please sign in to comment.