lib/sprockets/commonjs.rb in sprockets-commonjs-0.0.5 vs lib/sprockets/commonjs.rb in sprockets-commonjs-0.0.6.pre

- old
+ new

@@ -1,49 +1,40 @@ require 'sprockets' require 'tilt' module Sprockets class CommonJS < Tilt::Template + self.default_mime_type = 'application/javascript' - WRAPPER = '%s.define({"%s":' + - 'function(exports, require, module){' + - '%s' + - ";}});\n" - - class << self - attr_accessor :default_namespace + def self.default_namespace + 'this.require' end - self.default_mime_type = 'application/javascript' - self.default_namespace = 'this.require' - - protected - def prepare @namespace = self.class.default_namespace end + attr_reader :namespace + def evaluate(scope, locals, &block) - if commonjs_module?(scope) + if File.extname(scope.logical_path) == '.module' + path = scope.logical_path + path = path.gsub(/^\.?\//, '') # Remove relative paths + path = path.chomp('.module') # Remove module ext + scope.require_asset 'sprockets/commonjs' - WRAPPER % [ namespace, commonjs_module_name(scope), data ] + + code = '' + code << "#{namespace}.define({#{path.inspect}:" + code << 'function(exports, require, module){' + code << data + code << ";}});\n" + code else data end end - - private - - attr_reader :namespace - - def commonjs_module?(scope) - scope.pathname.basename.to_s.include?('.module') - end - - def commonjs_module_name(scope) - scope.logical_path.sub(/\.module$/, '') - end - end -end -require 'sprockets/commonjs/engine' + register_postprocessor 'application/javascript', CommonJS + append_path File.expand_path('../../../assets', __FILE__) +end \ No newline at end of file