Sha256: 0e02d3c52b60f1ba657565cc92a6149e2d5ea278f025fb093325a87b3621a099

Contents?: true

Size: 1.18 KB

Versions: 5

Compression:

Stored size: 1.18 KB

Contents

require 'tilt'

module WebpackRails
  class Processor < Tilt::Template
    def self.configure(webpack_task_config)
      Class.new(Processor) 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 rewrite_asset_paths(contents, context)
      contents.gsub(/['"]\$asset_path\/([^'"]+?)['"]/) {|s| "'#{context.asset_path($1)}'" }
    end

    def dependable_asset(filepath)
      File.file?(filepath) # ignore non-filepath entries
    end

    def evaluate(context, locals)
      return data unless context.pathname.to_s.include?('.bundle')

      file_contents = nil
      if self.class.config[:watch]
        result = WebpackRails::Task.run_webpack(self.class.config)

        # add webpack bundle dependencies as sprockets dependencies for this file
        result[:modules].map do |m|
          context.depend_on(m) if dependable_asset(m)
        end

        file_contents = context.pathname.open.read # reload file contents after build
      else
        file_contents = data
      end

      rewrite_asset_paths(file_contents, context)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
webpack_rails-2.0.1 lib/webpack_rails/processor.rb
webpack_rails-2.0.0 lib/webpack_rails/processor.rb
webpack_rails-1.3.1 lib/webpack_rails/processor.rb
webpack_rails-1.3.0 lib/webpack_rails/processor.rb
webpack_rails-1.2.2 lib/webpack_rails/processor.rb