Sha256: 83062f9f6de370adfefe4daa3bfa857cabddf940767d62ca597da47fbd8e6ac7

Contents?: true

Size: 982 Bytes

Versions: 1

Compression:

Stored size: 982 Bytes

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
    end

    self.default_mime_type = 'application/javascript'
    self.default_namespace = 'this.require'

    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, commonjs_module_name(scope), data ]
      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'

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sprockets-commonjs-0.0.5 lib/sprockets/commonjs.rb