Class: Sprockets::Commoner::Processor

Inherits:
Schmooze::Base
  • Object
show all
Defined in:
lib/sprockets/commoner/processor.rb

Constant Summary collapse

ExcludedFileError =
Class.new(::StandardError)
VERSION =
'3'.freeze
BABELRC_FILE =
'.babelrc'.freeze
PACKAGE_JSON =
'package.json'.freeze
JS_PACKAGE_PATH =
File.expand_path('../../../js', __dir__)
ALLOWED_EXTENSIONS =
/\.js(?:on)?(?:\.erb)?\z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, include: [root], exclude: ['vendor/bundle'], babel_exclude: [/node_modules/], transform_options: []) ⇒ Processor

Returns a new instance of Processor.



64
65
66
67
68
69
70
71
# File 'lib/sprockets/commoner/processor.rb', line 64

def initialize(root, include: [root], exclude: ['vendor/bundle'], babel_exclude: [/node_modules/], transform_options: [])
  @root = root
  @include = include.map {|path| expand_to_root(path, root) }
  @exclude = exclude.map {|path| expand_to_root(path, root) }
  @babel_exclude = babel_exclude.map {|path| expand_to_root(path, root) }
  @transform_options = transform_options.map {|(path, options)| [expand_to_root(path, root), options]}
  super(root, 'NODE_PATH' => JS_PACKAGE_PATH)
end

Instance Attribute Details

#babel_excludeObject (readonly)

Returns the value of attribute babel_exclude.



63
64
65
# File 'lib/sprockets/commoner/processor.rb', line 63

def babel_exclude
  @babel_exclude
end

#excludeObject (readonly)

Returns the value of attribute exclude.



63
64
65
# File 'lib/sprockets/commoner/processor.rb', line 63

def exclude
  @exclude
end

#includeObject (readonly)

Returns the value of attribute include.



63
64
65
# File 'lib/sprockets/commoner/processor.rb', line 63

def include
  @include
end

#transform_optionsObject (readonly)

Returns the value of attribute transform_options.



63
64
65
# File 'lib/sprockets/commoner/processor.rb', line 63

def transform_options
  @transform_options
end

Class Method Details

.call(input) ⇒ Object



46
47
48
# File 'lib/sprockets/commoner/processor.rb', line 46

def self.call(input)
  instance(input[:environment]).call(input)
end

.configure(env, *args, **kwargs) ⇒ Object



58
59
60
61
# File 'lib/sprockets/commoner/processor.rb', line 58

def self.configure(env, *args, **kwargs)
  unregister(env)
  env.register_postprocessor('application/javascript', self.new(env.root, *args, **kwargs))
end

.instance(env) ⇒ Object



42
43
44
# File 'lib/sprockets/commoner/processor.rb', line 42

def self.instance(env)
  @instance ||= new(env.root)
end

.unregister(env) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/sprockets/commoner/processor.rb', line 50

def self.unregister(env)
  env.postprocessors['application/javascript'].each do |processor|
    if processor == self || processor.is_a?(self)
      env.unregister_postprocessor('application/javascript', processor)
    end
  end
end

Instance Method Details

#cache_keyObject



73
74
75
# File 'lib/sprockets/commoner/processor.rb', line 73

def cache_key
  @cache_key ||= compute_cache_key
end

#call(input) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/sprockets/commoner/processor.rb', line 77

def call(input)
  filename = input[:filename]

  return unless should_process?(filename)

  @env = input[:environment]
  @required = input[:metadata][:required].to_a
  insertion_index = @required.index(input[:uri]) || -1
  @dependencies = Set.new(input[:metadata][:dependencies])

  babel_config = babelrc_data(filename)
  result = transform(input[:data], options(input), commoner_options(input))

  commoner_required = Set.new(input[:metadata][:commoner_required])
  result['metadata']['targetsToProcess'].each do |t|
    unless should_process?(t)
      raise ExcludedFileError, "#{t} was imported from #{filename} but this file won't be processed by Sprockets::Commoner"
    end
    commoner_required.add(t)
  end

  result['metadata']['required'].each do |r|
    asset = resolve(r, accept: input[:content_type], pipeline: :self)
    @required.insert(insertion_index, asset)
  end

  result['metadata']['includedEnvironmentVariables'].each do |env|
    @dependencies << "commoner-environment-variable:#{env}"
  end

  map = process_map(input[:metadata][:map], result['map'], input)

  {
    data: result['code'],
    dependencies: @dependencies,
    required: Set.new(@required),
    map: map,

    commoner_global_identifier: result['metadata']['globalIdentifier'],
    commoner_required: commoner_required,
    commoner_used_helpers: Set.new(input[:metadata][:commoner_used_helpers]) + result['metadata']['usedHelpers'],
    commoner_enabled: input[:metadata][:commoner_enabled] | result['metadata']['commonerEnabled'],
  }
end