Sha256: a4ef31b5d2dbec67ef0345e3d37b0322c7bfcc4eb8fab670de56bf7955679913

Contents?: true

Size: 966 Bytes

Versions: 1

Compression:

Stored size: 966 Bytes

Contents

require 'sprockets'
require 'tilt'

module Sprockets
  class CommonJS < Tilt::Template
    self.default_mime_type = 'application/javascript'

    def self.default_namespace
      'this.require'
    end

    def prepare
      @namespace = self.class.default_namespace
    end

    attr_reader :namespace

    def evaluate(scope, locals, &block)
      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'

        code = ''
        code << "#{namespace}.define({#{path.inspect}:"
        code << 'function(exports, require, module){'
        code << data
        code << ";}});\n"
        code
      else
        data
      end
    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-0.0.6.pre lib/sprockets/commonjs.rb