Class: Sprockets::Commoner::Processor
- Inherits:
-
Schmooze::Base
- Object
- Schmooze::Base
- Sprockets::Commoner::Processor
- 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.('../../../js', __dir__)
- ALLOWED_EXTENSIONS =
/\.js(?:on)?(?:\.erb)?\z/
Instance Attribute Summary collapse
-
#babel_exclude ⇒ Object
readonly
Returns the value of attribute babel_exclude.
-
#exclude ⇒ Object
readonly
Returns the value of attribute exclude.
-
#include ⇒ Object
readonly
Returns the value of attribute include.
-
#transform_options ⇒ Object
readonly
Returns the value of attribute transform_options.
Class Method Summary collapse
- .call(input) ⇒ Object
- .configure(env, *args, **kwargs) ⇒ Object
- .instance(env) ⇒ Object
- .unregister(env) ⇒ Object
Instance Method Summary collapse
- #cache_key ⇒ Object
- #call(input) ⇒ Object
-
#initialize(root, include: [root], exclude: ['vendor/bundle'], babel_exclude: [/node_modules/], transform_options: []) ⇒ Processor
constructor
A new instance of Processor.
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| (path, root) } @exclude = exclude.map {|path| (path, root) } @babel_exclude = babel_exclude.map {|path| (path, root) } @transform_options = .map {|(path, )| [(path, root), ]} super(root, 'NODE_PATH' => JS_PACKAGE_PATH) end |
Instance Attribute Details
#babel_exclude ⇒ Object (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 |
#exclude ⇒ Object (readonly)
Returns the value of attribute exclude.
63 64 65 |
# File 'lib/sprockets/commoner/processor.rb', line 63 def exclude @exclude end |
#include ⇒ Object (readonly)
Returns the value of attribute include.
63 64 65 |
# File 'lib/sprockets/commoner/processor.rb', line 63 def include @include end |
#transform_options ⇒ Object (readonly)
Returns the value of attribute transform_options.
63 64 65 |
# File 'lib/sprockets/commoner/processor.rb', line 63 def @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_key ⇒ Object
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], (input), (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 |