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.



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

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

Instance Attribute Details

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#fileObject

Returns the value of attribute file.



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

def file
  @file
end

Class Method Details

.call(input) ⇒ Object



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

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

.instanceObject



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

def self.instance
  @instance ||= new
end

Instance Method Details

#call(input) ⇒ Object



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
49
50
51
52
53
54
# File 'lib/browserify-rails/browserify_processor.rb', line 23

def call(input)
  self.file = input[:filename]
  return input[:data] unless in_path?
  self.data = input[:data]

  # 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 resolved && resolved.is_a?(Array)
      dependencies += resolved[1]
    elsif resolved
      dependencies << "file-digest://#{Addressable::URI.escape resolved}"
    elsif config.evaluate_node_modules
      dependencies << "file-digest://#{Addressable::URI.escape path}"
    end
  end

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