Module: Roda::RodaPlugins::Sprockets

Defined in:
lib/roda/plugins/sprockets.rb,
lib/roda/plugins/sprockets_task.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods, RequestMethods Classes: Task

Constant Summary collapse

DEFAULTS =
{
  sprockets:      nil,
  precompile:     %w(app.js app.css *.png *.jpg *.svg *.eot *.ttf *.woff *.woff2),
  prefix:         %w(assets vendor/assets),
  root:           false,
  public_path:    "public/assets/",
  path_prefix:    "/assets",
  protocol:       :http,
  css_compressor: nil,
  js_compressor:  nil,
  host:           nil,
  digest:         true,
  opal:           false,
  debug:          nil,
  cache:          nil,
}.freeze

Class Method Summary collapse

Class Method Details

.configure(app, plugin_options = {}) ⇒ Object



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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/roda/plugins/sprockets.rb', line 25

def self.configure(app, plugin_options = {})
  if app.opts[:sprockets]
    app.opts[:sprockets].merge!(plugin_options)
  else
    app.opts[:sprockets] = plugin_options.dup
  end
  options = app.opts[:sprockets]
  DEFAULTS.each { |k, v| options[k] = v unless options.key?(k) }

  if !options[:root]
    options[:root] = app.opts[:root] || Dir.pwd
  end

  if options[:debug].nil?
    if %w[development test].include? ENV['RACK_ENV']
      options[:debug] = true
    else
      options[:debug] = false
    end
  end

  # opal-sprockets registers engines when required, but if we create Sprockets::Environment before
  # requiring that, they don't get registered
  require 'opal/sprockets' if options[:opal]
  options[:sprockets] ||= ::Sprockets::Environment.new

  options[:prefix].each do |prefix|
    # Support absolute asset paths
    # https://github.com/kalasjocke/sinatra-asset-pipeline/pull/54
    if prefix.end_with? '/'
      paths = if Pathname.new(prefix).absolute?
        Dir[File.join(prefix)]
      else
        Dir[File.join(options[:root], prefix)]
      end
    else
      paths = if Pathname.new(prefix).absolute?
        Dir[File.join(prefix, '*')]
      else
        Dir[File.join(options[:root], prefix, '*')]
      end
    end

    paths.each do |path|
      options[:sprockets].append_path path
    end
  end

  if options[:opal]
    Opal.paths.each do |path|
      options[:sprockets].append_path path
    end
  end

  if options[:cache]
    options[:sprockets].cache = options[:cache]
  end

  options[:sprockets_helpers] = ::Sprockets::Helpers::Settings.new

  options[:sprockets_helpers].environment = options[:sprockets]
  options[:sprockets_helpers].digest      = options[:digest]
  options[:sprockets_helpers].prefix      = options[:path_prefix]
  options[:sprockets_helpers].debug       = options[:debug]

  unless options[:debug]
    options[:sprockets].css_compressor = options[:css_compressor] unless options[:css_compressor].nil?
    options[:sprockets].js_compressor  = options[:js_compressor] unless options[:js_compressor].nil?  

    options[:sprockets_helpers].manifest   = ::Sprockets::Manifest.new(options[:sprockets], options[:public_path])
    options[:sprockets_helpers].protocol   = options[:protocol]
    options[:sprockets_helpers].asset_host = options[:host] unless options[:host].nil?
  end
end