Sha256: d0f93d8f745639ff92304f5ea90475531b7ba98f3122d6e9f47bfdb5a5c1a51b
Contents?: true
Size: 1.5 KB
Versions: 1
Compression:
Stored size: 1.5 KB
Contents
require 'sprockets' require 'tilt' module Sprockets class CommonJS < Tilt::Template WRAPPER = '%s.define({"%s":' + "function(exports, require, module){" + '%s' + ";}});\n" class << self attr_accessor :default_namespace attr_accessor :module_paths end self.default_mime_type = 'application/javascript' self.default_namespace = 'this.require' self.module_paths = [] protected def prepare @namespace = self.class.default_namespace end def evaluate(scope, locals, &block) if commonjs_module?(scope) scope.require_asset 'sprockets/commonjs' WRAPPER % [ namespace, module_name(scope), data ] else data end end private attr_reader :namespace def commonjs_module?(scope) in_path?(scope) && (exports_module? || has_dependencies?) end def in_path?(scope) self.class.module_paths.any? do |path| scope.pathname.to_s.start_with? path end end def exports_module? data.include?('module.exports') end def dependencies @dependencies ||= data.scan(/require\(['"](.*)['"]\)/).map(&:last) end def has_dependencies? dependencies.length > 0 end def module_name(scope) scope.logical_path.gsub(/^\.?\//, '') # Remove relative paths end end register_postprocessor 'application/javascript', CommonJS append_path File.expand_path('../../../assets', __FILE__) end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
sprockets-commonjs-mindreframer-0.1.0 | lib/sprockets/commonjs.rb |