Class: BrowserifyRails::BrowserifyProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/browserify-rails/browserify_processor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBrowserifyProcessor

Returns a new instance of BrowserifyProcessor.



18
19
20
# File 'lib/browserify-rails/browserify_processor.rb', line 18

def initialize
  self.config = Rails.application.config.browserify_rails
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/browserify-rails/browserify_processor.rb', line 8

def config
  @config
end

#dataObject

Returns the value of attribute data.



8
9
10
# File 'lib/browserify-rails/browserify_processor.rb', line 8

def data
  @data
end

#fileObject

Returns the value of attribute file.



8
9
10
# File 'lib/browserify-rails/browserify_processor.rb', line 8

def file
  @file
end

Class Method Details

.call(input) ⇒ Object



14
15
16
# File 'lib/browserify-rails/browserify_processor.rb', line 14

def self.call(input)
  instance.call(input)
end

.instanceObject



10
11
12
# File 'lib/browserify-rails/browserify_processor.rb', line 10

def self.instance
  @instance ||= new
end

Instance Method Details

#call(input) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/browserify-rails/browserify_processor.rb', line 22

def call(input)
  self.data = input[:data]
  self.file = input[:filename]

  # Clear the cached dependencies because the source file changes
  @dependencies = nil

  ensure_tmp_dir_exists!
  ensure_commands_exist!

  # If there's nothing to do, we just return the data we received
  return data unless should_browserify?

  dependencies = Set.new(input[:metadata][:dependencies])

  # Signal dependencies to sprockets to ensure we track changes
  evaluate_dependencies(input[:environment].paths).each do |path|
    resolved = input[:environment].resolve(path)
    if config.evaluate_node_modules && !resolved
      resolved = path
    end
    dependencies << "file-digest://#{URI.escape resolved}" if resolved
  end

  new_data = run_browserify(input[:name])
  { data: new_data, dependencies: dependencies }
end