Sha256: 714b1740c8819ddea06aca5485e1e6baeb892138cf30a03a23e0bb2fba8c0519

Contents?: true

Size: 1.12 KB

Versions: 8

Compression:

Stored size: 1.12 KB

Contents

require 'tilt'

module WebpackRails
  class RequireDirectiveProcessor < Tilt::Template
    DIRECTIVE_PATTERN = /^.*?=\s*webpack_require\s+(.*?)\s*$/

    def self.configure(webpack_task_config)
      Class.new(RequireDirectiveProcessor) do
        self.config = webpack_task_config
      end
    end

    def self.config=(new_config)
      @config = new_config
    end

    def self.config
      @config
    end

    def prepare
    end

    def config
      self.class.config
    end

    def dev_server_base_url
      "#{config[:protocol]}://#{config[:host]}:#{config[:port]}"
    end

    def process_require(context, locals, bundle_filename)
      if config[:dev_server]
        if bundle_filename.end_with? '.js'
          return %{document.write('<script src="#{dev_server_base_url}/#{bundle_filename}"></script>');}
        end
        return ''
      end

      # will be handled by normal sprockets require
      context.require_asset(bundle_filename)
      return ''
    end

    def evaluate(context, locals)
      data.gsub(DIRECTIVE_PATTERN) do |match_text|
        process_require(context, locals, $1)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
webpack_rails-1.3.1 lib/webpack_rails/require_directive_processor.rb
webpack_rails-1.3.0 lib/webpack_rails/require_directive_processor.rb
webpack_rails-1.2.2 lib/webpack_rails/require_directive_processor.rb
webpack_rails-1.2.1 lib/webpack_rails/require_directive_processor.rb
webpack_rails-1.1.1 lib/webpack_rails/require_directive_processor.rb
webpack_rails-1.1.0 lib/webpack_rails/require_directive_processor.rb
webpack_rails-1.0.1 lib/webpack_rails/require_directive_processor.rb
webpack_rails-1.0.0 lib/webpack_rails/require_directive_processor.rb