Skip to content

Commit

Permalink
use Holmes
Browse files Browse the repository at this point in the history
  • Loading branch information
maccman committed Feb 23, 2012
1 parent a745668 commit 2e8b10c
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 14 deletions.
61 changes: 61 additions & 0 deletions assets/javascripts/commonjs.js
@@ -0,0 +1,61 @@
(function() {
var modules = {}, cache = {};

if (this.require && this.require.modules) {
modules = this.require.modules;
}

var require = function(name, root) {
var path = expand(root, name), indexPath = expand(path, './index'), module, fn;
module = cache[path] || cache[indexPath];
if (module) {
return module;
} else if (fn = modules[path] || modules[path = indexPath]) {
module = {id: path, exports: {}};
cache[path] = module.exports;
fn(module.exports, function(name) {
return require(name, dirname(path));
}, module);
return cache[path] = module.exports;
} else {
throw 'module ' + name + ' not found';
}
};

var expand = function(root, name) {
var results = [], parts, part;
// If path is relative
if (/^\.\.?(\/|$)/.test(name)) {
parts = [root, name].join('/').split('/');
} else {
parts = name.split('/');
}
for (var i = 0, length = parts.length; i < length; i++) {
part = parts[i];
if (part == '..') {
results.pop();
} else if (part != '.' && part != '') {
results.push(part);
}
}
return results.join('/');
};

var dirname = function(path) {
return path.split('/').slice(0, -1).join('/');
};

this.require = function(name) {
return require(name, '');
};

this.require.define = function(bundle) {
for (var key in bundle) {
modules[key] = bundle[key];
}
};

this.require.modules = modules;
this.require.cache = cache;
return this.require;
}).call(this);
7 changes: 0 additions & 7 deletions lib/sprockets-commonjs.rb

This file was deleted.

5 changes: 0 additions & 5 deletions lib/sprockets-commonjs/version.rb

This file was deleted.

49 changes: 49 additions & 0 deletions lib/sprockets/commonjs.rb
@@ -0,0 +1,49 @@
require 'sprockets'
require 'tilt'
require 'holmes'

module Sprockets
class CommonJS < Tilt::Template
COMMONJS_PATH = File.expand_path('../../../assets/javascripts/commonjs.js', __FILE__)

def self.default_mime_type
'application/javascript'
end

def self.default_namespace
'this.require'
end

def prepare
@namespace = self.class.default_namespace
end

attr_reader :namespace

def evaluate(scope, locals, &block)
scope.require_asset(COMMONJS_PATH)

requires = Holmes.parse(data)
raise 'Dynamic require calls' if requires['expressions'].any?

requires['strings'].each do |dependency|
scope.require_asset(dependency)
end

<<-JS
(function() {
#{namespace} || (#{namespace} = {});
#{namespace}.modules || (#{namespace}.modules = {});
#{namespace}.modules[#{scope.logical_path.inspect}] = function(exports, require, modules){
#{indent(data)}
};
}).call(this);
JS
end

private
def indent(string)
string.gsub(/$(.)/m, "\\1 ").strip
end
end
end
4 changes: 2 additions & 2 deletions sprockets-commonjs.gemspec
@@ -1,10 +1,9 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "sprockets-commonjs/version"

Gem::Specification.new do |s|
s.name = "sprockets-commonjs"
s.version = Sprockets::Commonjs::VERSION
s.version = '0.0.1'
s.authors = ["Alex MacCaw"]
s.email = ["info@eribium.org"]
s.homepage = ""
Expand All @@ -21,4 +20,5 @@ Gem::Specification.new do |s|
# specify any dependencies here; for example:
# s.add_development_dependency "rspec"
s.add_runtime_dependency "sprockets", "~>2.1.2"
s.add_runtime_dependency "holmes", "~>0.0.1"
end

0 comments on commit 2e8b10c

Please sign in to comment.