From 2e8b10cb70dd05d8708652324f15d1c12aa25969 Mon Sep 17 00:00:00 2001 From: Alex MacCaw Date: Wed, 22 Feb 2012 19:49:47 -0800 Subject: [PATCH] use Holmes --- assets/javascripts/commonjs.js | 61 +++++++++++++++++++++++++++++++ lib/sprockets-commonjs.rb | 7 ---- lib/sprockets-commonjs/version.rb | 5 --- lib/sprockets/commonjs.rb | 49 +++++++++++++++++++++++++ sprockets-commonjs.gemspec | 4 +- 5 files changed, 112 insertions(+), 14 deletions(-) create mode 100644 assets/javascripts/commonjs.js delete mode 100644 lib/sprockets-commonjs.rb delete mode 100644 lib/sprockets-commonjs/version.rb create mode 100644 lib/sprockets/commonjs.rb diff --git a/assets/javascripts/commonjs.js b/assets/javascripts/commonjs.js new file mode 100644 index 0000000..930a62f --- /dev/null +++ b/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); \ No newline at end of file diff --git a/lib/sprockets-commonjs.rb b/lib/sprockets-commonjs.rb deleted file mode 100644 index a09ab98..0000000 --- a/lib/sprockets-commonjs.rb +++ /dev/null @@ -1,7 +0,0 @@ -require "sprockets-commonjs/version" - -module Sprockets - module Commonjs - # Your code goes here... - end -end diff --git a/lib/sprockets-commonjs/version.rb b/lib/sprockets-commonjs/version.rb deleted file mode 100644 index 05512da..0000000 --- a/lib/sprockets-commonjs/version.rb +++ /dev/null @@ -1,5 +0,0 @@ -module Sprockets - module Commonjs - VERSION = "0.0.1" - end -end diff --git a/lib/sprockets/commonjs.rb b/lib/sprockets/commonjs.rb new file mode 100644 index 0000000..1efefa5 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/sprockets-commonjs.gemspec b/sprockets-commonjs.gemspec index e3b37e0..ef62f76 100644 --- a/sprockets-commonjs.gemspec +++ b/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 = "" @@ -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