Class: WebpackRails::Processor

Inherits:
Tilt::Template
  • Object
show all
Defined in:
lib/webpack_rails/processor.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configObject



15
16
17
# File 'lib/webpack_rails/processor.rb', line 15

def self.config
  @config
end

.config=(new_config) ⇒ Object



11
12
13
# File 'lib/webpack_rails/processor.rb', line 11

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

.configure(webpack_task_config) ⇒ Object



5
6
7
8
9
# File 'lib/webpack_rails/processor.rb', line 5

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

Instance Method Details

#evaluate(context, locals) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/webpack_rails/processor.rb', line 26

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 m.start_with?('/') # ignore non-filepath entries
    end

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

  rewrite_asset_paths(file_contents, context)
end

#prepareObject



19
20
# File 'lib/webpack_rails/processor.rb', line 19

def prepare
end

#rewrite_asset_paths(contents, context) ⇒ Object



22
23
24
# File 'lib/webpack_rails/processor.rb', line 22

def rewrite_asset_paths(contents, context)
  contents.gsub(/['"]\$asset_path\/([^'"]+?)['"]/) {|s| "'#{context.asset_path($1)}'" }
end