sprockets-rails 2.3.3 → 3.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ module Sprockets
2
+ module Rails
3
+ class QuietAssets
4
+ def initialize(app)
5
+ @app = app
6
+ @assets_regex = %r(\A/{0,2}#{::Rails.application.config.assets.prefix})
7
+ end
8
+
9
+ def call(env)
10
+ if env['PATH_INFO'] =~ @assets_regex
11
+ ::Rails.logger.silence { @app.call(env) }
12
+ else
13
+ @app.call(env)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ module Sprockets
2
+ module Rails
3
+ module RouteWrapper
4
+
5
+ def internal_assets_path?
6
+ path =~ %r{\A#{self.class.assets_prefix}\z}
7
+ end
8
+
9
+ def internal?
10
+ super || internal_assets_path?
11
+ end
12
+
13
+ def self.included(klass)
14
+ klass.class_eval do
15
+ def internal_with_sprockets?
16
+ internal_without_sprockets? || internal_assets_path?
17
+ end
18
+ alias_method_chain :internal?, :sprockets
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -2,7 +2,6 @@ require 'rake'
2
2
  require 'rake/sprocketstask'
3
3
  require 'sprockets'
4
4
  require 'action_view'
5
- require 'action_view/base'
6
5
 
7
6
  module Sprockets
8
7
  module Rails
@@ -16,7 +15,9 @@ module Sprockets
16
15
 
17
16
  def environment
18
17
  if app
19
- app.assets
18
+ # Use initialized app.assets or force build an environment if
19
+ # config.assets.compile is disabled
20
+ app.assets || Sprockets::Railtie.build_environment(app)
20
21
  else
21
22
  super
22
23
  end
@@ -24,7 +25,8 @@ module Sprockets
24
25
 
25
26
  def output
26
27
  if app
27
- File.join(app.root, 'public', app.config.assets.prefix)
28
+ config = app.config
29
+ File.join(config.paths['public'].first, config.assets.prefix)
28
30
  else
29
31
  super
30
32
  end
@@ -46,17 +48,12 @@ module Sprockets
46
48
  end
47
49
  end
48
50
 
49
- def cache_path
50
- if app
51
- "#{app.config.root}/tmp/cache/assets"
52
- else
53
- @cache_path
54
- end
55
- end
56
- attr_writer :cache_path
57
-
58
51
  def define
59
52
  namespace :assets do
53
+ %w( environment precompile clean clobber ).each do |task|
54
+ Rake::Task[task].clear if Rake::Task.task_defined?(task)
55
+ end
56
+
60
57
  # Override this task change the loaded dependencies
61
58
  desc "Load asset compile environment"
62
59
  task :environment do
@@ -73,9 +70,8 @@ module Sprockets
73
70
 
74
71
  desc "Remove old compiled assets"
75
72
  task :clean, [:keep] => :environment do |t, args|
76
- keep = Integer(args.keep || 2)
77
73
  with_logger do
78
- manifest.clean(keep)
74
+ manifest.clean(Integer(args.keep || self.keep))
79
75
  end
80
76
  end
81
77
 
@@ -83,7 +79,6 @@ module Sprockets
83
79
  task :clobber => :environment do
84
80
  with_logger do
85
81
  manifest.clobber
86
- rm_rf cache_path if cache_path
87
82
  end
88
83
  end
89
84
  end
@@ -0,0 +1,11 @@
1
+ require 'sprockets'
2
+
3
+ module Sprockets
4
+ module Rails
5
+ module Utils
6
+ def using_sprockets4?
7
+ Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new('4.x')
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,5 +1,5 @@
1
1
  module Sprockets
2
2
  module Rails
3
- VERSION = "2.3.3"
3
+ VERSION = "3.2.2"
4
4
  end
5
5
  end
@@ -2,9 +2,14 @@ require 'rails'
2
2
  require 'rails/railtie'
3
3
  require 'action_controller/railtie'
4
4
  require 'active_support/core_ext/module/remove_method'
5
+ require 'active_support/core_ext/numeric/bytes'
5
6
  require 'sprockets'
7
+ require 'sprockets/rails/context'
6
8
  require 'sprockets/rails/helper'
9
+ require 'sprockets/rails/quiet_assets'
10
+ require 'sprockets/rails/route_wrapper'
7
11
  require 'sprockets/rails/version'
12
+ require 'set'
8
13
 
9
14
  module Rails
10
15
  class Application
@@ -19,37 +24,40 @@ module Rails
19
24
  remove_possible_method :assets=
20
25
 
21
26
  # Returns Sprockets::Environment for app config.
22
- def assets
23
- @assets ||= Sprockets::Environment.new(root.to_s) do |env|
24
- env.version = ::Rails.env
27
+ attr_accessor :assets
25
28
 
26
- path = "#{config.root}/tmp/cache/assets/#{::Rails.env}"
27
- env.cache = Sprockets::Cache::FileStore.new(path)
29
+ # Returns Sprockets::Manifest for app config.
30
+ attr_accessor :assets_manifest
28
31
 
29
- env.context_class.class_eval do
30
- include ::Sprockets::Rails::Helper
31
- end
32
+ # Called from asset helpers to alert you if you reference an asset URL that
33
+ # isn't precompiled and hence won't be available in production.
34
+ def asset_precompiled?(logical_path)
35
+ if precompiled_assets.include?(logical_path)
36
+ true
37
+ elsif !config.cache_classes
38
+ # Check to see if precompile list has been updated
39
+ precompiled_assets(true).include?(logical_path)
40
+ else
41
+ false
32
42
  end
33
43
  end
34
- attr_writer :assets
35
44
 
36
- # Returns Sprockets::Manifest for app config.
37
- attr_accessor :assets_manifest
45
+ # Lazy-load the precompile list so we don't cause asset compilation at app
46
+ # boot time, but ensure we cache the list so we don't recompute it for each
47
+ # request or test case.
48
+ def precompiled_assets(clear_cache = false)
49
+ @precompiled_assets = nil if clear_cache
50
+ @precompiled_assets ||= assets_manifest.find(config.assets.precompile).map(&:logical_path).to_set
51
+ end
38
52
  end
39
53
 
40
54
  class Engine < Railtie
41
55
  # Skip defining append_assets_path on Rails <= 4.2
42
56
  unless initializers.find { |init| init.name == :append_assets_path }
43
57
  initializer :append_assets_path, :group => :all do |app|
44
- if paths["app/assets"].respond_to?(:existent_directories)
45
- app.config.assets.paths.unshift(*paths["vendor/assets"].existent_directories)
46
- app.config.assets.paths.unshift(*paths["lib/assets"].existent_directories)
47
- app.config.assets.paths.unshift(*paths["app/assets"].existent_directories)
48
- else
49
- app.config.assets.paths.unshift(*paths["vendor/assets"].paths.select { |d| File.directory?(d) })
50
- app.config.assets.paths.unshift(*paths["lib/assets"].paths.select { |d| File.directory?(d) })
51
- app.config.assets.paths.unshift(*paths["app/assets"].paths.select { |d| File.directory?(d) })
52
- end
58
+ app.config.assets.paths.unshift(*paths["vendor/assets"].existent_directories)
59
+ app.config.assets.paths.unshift(*paths["lib/assets"].existent_directories)
60
+ app.config.assets.paths.unshift(*paths["app/assets"].existent_directories)
53
61
  end
54
62
  end
55
63
  end
@@ -57,8 +65,26 @@ end
57
65
 
58
66
  module Sprockets
59
67
  class Railtie < ::Rails::Railtie
60
- LOOSE_APP_ASSETS = lambda do |filename, path|
61
- path =~ /app\/assets/ && !%w(.js .css).include?(File.extname(filename))
68
+ include Sprockets::Rails::Utils
69
+
70
+ class ManifestNeededError < StandardError
71
+ def initialize
72
+ msg = "Expected to find a manifest file in `app/assets/config/manifest.js`\n" +
73
+ "But did not, please create this file and use it to link any assets that need\n" +
74
+ "to be rendered by your app:\n\n" +
75
+ "Example:\n" +
76
+ " //= link_tree ../images\n" +
77
+ " //= link_directory ../javascripts .js\n" +
78
+ " //= link_directory ../stylesheets .css\n" +
79
+ "and restart your server\n\n" +
80
+ "For more information see: https://github.com/rails/sprockets/blob/070fc01947c111d35bb4c836e9bb71962a8e0595/UPGRADING.md#manifestjs"
81
+ super msg
82
+ end
83
+ end
84
+
85
+ LOOSE_APP_ASSETS = lambda do |logical_path, filename|
86
+ filename.start_with?(::Rails.root.join("app/assets").to_s) &&
87
+ !['.js', '.css', ''].include?(File.extname(logical_path))
62
88
  end
63
89
 
64
90
  class OrderedOptions < ActiveSupport::OrderedOptions
@@ -68,92 +94,162 @@ module Sprockets
68
94
  end
69
95
 
70
96
  config.assets = OrderedOptions.new
71
- config.assets._blocks = []
72
- config.assets.paths = []
73
- config.assets.prefix = "/assets"
74
- config.assets.manifest = nil
75
- config.assets.precompile = [LOOSE_APP_ASSETS, /(?:\/|\\|\A)application\.(css|js)$/]
76
- config.assets.version = ""
77
- config.assets.debug = false
78
- config.assets.compile = true
79
- config.assets.digest = false
97
+ config.assets._blocks = []
98
+ config.assets.paths = []
99
+ config.assets.precompile = []
100
+ config.assets.prefix = "/assets"
101
+ config.assets.manifest = nil
102
+ config.assets.quiet = false
103
+
104
+ initializer :set_default_precompile do |app|
105
+ if using_sprockets4?
106
+ raise ManifestNeededError unless ::Rails.root.join("app/assets/config/manifest.js").exist?
107
+ app.config.assets.precompile += %w( manifest.js )
108
+ else
109
+ app.config.assets.precompile += [LOOSE_APP_ASSETS, /(?:\/|\\|\A)application\.(css|js)$/]
110
+ end
111
+ end
112
+
113
+ initializer :quiet_assets do |app|
114
+ if app.config.assets.quiet
115
+ app.middleware.insert_before ::Rails::Rack::Logger, ::Sprockets::Rails::QuietAssets
116
+ end
117
+ end
118
+
119
+ config.assets.version = ""
120
+ config.assets.debug = false
121
+ config.assets.compile = true
122
+ config.assets.digest = true
123
+ config.assets.cache_limit = 50.megabytes
124
+ config.assets.gzip = true
125
+ config.assets.check_precompiled_asset = true
126
+ config.assets.unknown_asset_fallback = true
127
+
128
+ config.assets.configure do |env|
129
+ config.assets.paths.each { |path| env.append_path(path) }
130
+ end
131
+
132
+ config.assets.configure do |env|
133
+ env.context_class.send :include, ::Sprockets::Rails::Context
134
+ env.context_class.assets_prefix = config.assets.prefix
135
+ env.context_class.digest_assets = config.assets.digest
136
+ env.context_class.config = config.action_controller
137
+ end
138
+
139
+ config.assets.configure do |env|
140
+ env.cache = Sprockets::Cache::FileStore.new(
141
+ "#{env.root}/tmp/cache/assets",
142
+ config.assets.cache_limit,
143
+ env.logger
144
+ )
145
+ end
146
+
147
+ Sprockets.register_dependency_resolver 'rails-env' do
148
+ ::Rails.env.to_s
149
+ end
150
+
151
+ config.assets.configure do |env|
152
+ env.depend_on 'rails-env'
153
+ end
154
+
155
+ config.assets.configure do |env|
156
+ env.version = config.assets.version
157
+ end
158
+
159
+ config.assets.configure do |env|
160
+ env.gzip = config.assets.gzip if env.respond_to?(:gzip=)
161
+ end
80
162
 
81
163
  rake_tasks do |app|
82
164
  require 'sprockets/rails/task'
83
165
  Sprockets::Rails::Task.new(app)
84
166
  end
85
167
 
86
- config.after_initialize do |app|
87
- config = app.config
88
-
89
- # Configuration options that should invalidate
90
- # the Sprockets cache when changed.
91
- app.assets.version = [
92
- app.assets.version,
93
- config.assets.version,
94
- config.action_controller.relative_url_root,
95
- (config.action_controller.asset_host unless config.action_controller.asset_host.respond_to?(:call)),
96
- Sprockets::Rails::VERSION
97
- ].compact.join('-')
98
-
99
- # Copy config.assets.paths to Sprockets
100
- config.assets.paths.each do |path|
101
- app.assets.append_path path
168
+ def build_environment(app, initialized = nil)
169
+ initialized = app.initialized? if initialized.nil?
170
+ unless initialized
171
+ ::Rails.logger.warn "Application uninitialized: Try calling YourApp::Application.initialize!"
102
172
  end
103
173
 
174
+ env = Sprockets::Environment.new(app.root.to_s)
175
+
176
+ config = app.config
177
+
104
178
  # Run app.assets.configure blocks
105
179
  config.assets._blocks.each do |block|
106
- block.call app.assets
180
+ block.call(env)
107
181
  end
108
182
 
109
183
  # Set compressors after the configure blocks since they can
110
184
  # define new compressors and we only accept existent compressors.
111
- app.assets.js_compressor = config.assets.js_compressor
112
- app.assets.css_compressor = config.assets.css_compressor
185
+ env.js_compressor = config.assets.js_compressor
186
+ env.css_compressor = config.assets.css_compressor
113
187
 
114
188
  # No more configuration changes at this point.
115
189
  # With cache classes on, Sprockets won't check the FS when files
116
190
  # change. Preferable in production when the FS only changes on
117
191
  # deploys when the app restarts.
118
192
  if config.cache_classes
119
- app.assets = app.assets.index
193
+ env = env.cached
120
194
  end
121
195
 
122
- manifest_assets_path = File.join(config.paths['public'].first, config.assets.prefix)
196
+ env
197
+ end
198
+
199
+ def self.build_manifest(app)
200
+ config = app.config
201
+ path = File.join(config.paths['public'].first, config.assets.prefix)
202
+ Sprockets::Manifest.new(app.assets, path, config.assets.manifest)
203
+ end
204
+
205
+ config.after_initialize do |app|
206
+ config = app.config
207
+
123
208
  if config.assets.compile
124
- app.assets_manifest = Sprockets::Manifest.new(app.assets, manifest_assets_path, config.assets.manifest)
125
- else
126
- app.assets_manifest = Sprockets::Manifest.new(manifest_assets_path, config.assets.manifest)
209
+ app.assets = self.build_environment(app, true)
210
+ app.routes.prepend do
211
+ mount app.assets => config.assets.prefix
212
+ end
213
+ end
214
+
215
+ app.assets_manifest = build_manifest(app)
216
+
217
+ if config.assets.resolve_with.nil?
218
+ config.assets.resolve_with = []
219
+ config.assets.resolve_with << :manifest if config.assets.digest && !config.assets.debug
220
+ config.assets.resolve_with << :environment if config.assets.compile
221
+ end
222
+
223
+ ActionDispatch::Routing::RouteWrapper.class_eval do
224
+ class_attribute :assets_prefix
225
+
226
+ if defined?(prepend) && ::Rails.version >= '4'
227
+ prepend Sprockets::Rails::RouteWrapper
228
+ else
229
+ include Sprockets::Rails::RouteWrapper
230
+ end
231
+
232
+ self.assets_prefix = config.assets.prefix
127
233
  end
128
234
 
129
235
  ActiveSupport.on_load(:action_view) do
130
236
  include Sprockets::Rails::Helper
131
237
 
132
238
  # Copy relevant config to AV context
133
- self.debug_assets = config.assets.debug
134
- self.digest_assets = config.assets.digest
135
- self.assets_prefix = config.assets.prefix
239
+ self.debug_assets = config.assets.debug
240
+ self.digest_assets = config.assets.digest
241
+ self.assets_prefix = config.assets.prefix
242
+ self.assets_precompile = config.assets.precompile
136
243
 
137
- # Copy over to Sprockets as well
138
- context = app.assets.context_class
139
- context.assets_prefix = config.assets.prefix
140
- context.digest_assets = config.assets.digest
141
- context.config = config.action_controller
142
-
143
- self.assets_environment = app.assets if config.assets.compile
244
+ self.assets_environment = app.assets
144
245
  self.assets_manifest = app.assets_manifest
145
- end
146
246
 
147
- Sprockets::Rails::Helper.precompile ||= app.config.assets.precompile
148
- Sprockets::Rails::Helper.assets ||= app.assets
149
- Sprockets::Rails::Helper.raise_runtime_errors = app.config.assets.raise_runtime_errors
247
+ self.resolve_assets_with = config.assets.resolve_with
150
248
 
151
- if config.assets.compile
152
- if app.routes.respond_to?(:prepend)
153
- app.routes.prepend do
154
- mount app.assets => config.assets.prefix
155
- end
156
- end
249
+ self.check_precompiled_asset = config.assets.check_precompiled_asset
250
+ self.unknown_asset_fallback = config.assets.unknown_asset_fallback
251
+ # Expose the app precompiled asset check to the view
252
+ self.precompiled_asset_checker = -> logical_path { app.asset_precompiled? logical_path }
157
253
  end
158
254
  end
159
255
  end