aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/sprockets/railtie.rb
blob: c8d6af942d45ae112c39e26cdf89ed3e34b74a02 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
module Sprockets
  autoload :Helpers, "sprockets/helpers"
  autoload :LazyCompressor, "sprockets/compressors"
  autoload :NullCompressor, "sprockets/compressors"

  # TODO: Get rid of config.assets.enabled
  class Railtie < ::Rails::Railtie
    config.default_asset_host_protocol = :relative

    rake_tasks do
      load "sprockets/assets.rake"
    end

    # Configure ActionController to use sprockets.
    initializer "sprockets.set_configs", :after => "action_controller.set_configs" do |app|
      ActiveSupport.on_load(:action_controller) do
        self.use_sprockets = app.config.assets.enabled
      end
    end

    # We need to configure this after initialization to ensure we collect
    # paths from all engines. This hook is invoked exactly before routes
    # are compiled, and so that other Railties have an opportunity to
    # register compressors.
    config.after_initialize do |app|
      assets = app.config.assets
      next unless assets.enabled

      app.assets = asset_environment(app)

      ActiveSupport.on_load(:action_view) do
        include ::Sprockets::Helpers::RailsHelper

        app.assets.context_class.instance_eval do
          include ::Sprockets::Helpers::RailsHelper
        end
      end

      app.routes.prepend do
        mount app.assets => assets.prefix
      end

      if config.action_controller.perform_caching
        app.assets = app.assets.index
      end
    end

    protected
      def asset_environment(app)
        require "sprockets"

        assets = app.config.assets

        env = Sprockets::Environment.new(app.root.to_s)

        env.static_root = File.join(app.root.join("public"), assets.prefix)

        if env.respond_to?(:append_path)
          assets.paths.each { |path| env.append_path(path) }
        else
          env.paths.concat assets.paths
        end

        env.logger = ::Rails.logger

        if env.respond_to?(:cache) && assets.cache_store != false
          env.cache = ActiveSupport::Cache.lookup_store(assets.cache_store) || ::Rails.cache
        end

        if assets.compress
          # temporarily hardcode default JS compressor to uglify. Soon, it will work
          # the same as SCSS, where a default plugin sets the default.
          unless assets.js_compressor == false
            env.js_compressor  = LazyCompressor.new { expand_js_compressor(assets.js_compressor || :uglifier) }
          end

          unless assets.css_compressor == false
            env.css_compressor = LazyCompressor.new { expand_css_compressor(assets.css_compressor) }
          end
        end

        env
      end

      def expand_js_compressor(sym)
        case sym
        when :closure
          require 'closure-compiler'
          Closure::Compiler.new
        when :uglifier
          require 'uglifier'
          Uglifier.new
        when :yui
          require 'yui/compressor'
          YUI::JavaScriptCompressor.new
        else
          sym
        end
      end

      def expand_css_compressor(sym)
        case sym
        when :yui
          require 'yui/compressor'
          YUI::CssCompressor.new
        else
          sym
        end
      end
  end
end