Class: Middleman::Sprockets::Environment

Inherits:
Sprockets::Environment
  • Object
show all
Defined in:
lib/middleman-sprockets/environment.rb

Overview

Generic Middleman Sprockets env

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Environment

Setup



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
# File 'lib/middleman-sprockets/environment.rb', line 16

def initialize(app, options={})
  @imported_assets = []
  @app = app
  @debug_assets = options.fetch(:debug_assets, false)

  super app.source_dir

  # By default, sprockets has no cache! Give it an in-memory one using a Hash
  # There is also a Sprockets::Cache::FileStore option, but it is fraught with cache-invalidation
  # peril, so we choose not to use it.
  @cache = {}

  enhance_context_class!

  # Remove compressors, we handle these with middleware
  unregister_bundle_processor 'application/javascript', :js_compressor
  unregister_bundle_processor 'text/css', :css_compressor

  # configure search paths
  append_path app.config[:js_dir]
  append_path app.config[:css_dir]
  append_path app.config[:images_dir]
  append_path app.config[:fonts_dir]

  if app.config.respond_to?(:bower_dir)
    warn ":bower_dir is deprecated. Call sprockets.append_path from a 'ready' block instead."
    append_path app.config[:bower_dir]
  end

  # add custom assets paths to the scope
  app.config[:js_assets_paths].each do |p|
    warn ":js_assets_paths is deprecated. Call sprockets.append_path from a 'ready' block instead."
    append_path p
  end if app.config.respond_to?(:js_assets_paths)

  # Stylus support
  if defined?(::Stylus)
    require 'stylus/sprockets'
    ::Stylus.setup(self, app.config[:styl])
  end
end

Instance Attribute Details

#debug_assetsObject (readonly)

Whether or not we should debug assets by splitting them all out into individual includes



6
7
8
# File 'lib/middleman-sprockets/environment.rb', line 6

def debug_assets
  @debug_assets
end

#imported_assetsObject (readonly)

A list of Sprockets logical paths for assets that should be brought into the Middleman application and built.



10
11
12
# File 'lib/middleman-sprockets/environment.rb', line 10

def imported_assets
  @imported_assets
end

#last_request_pathObject (readonly)

The current path, useful when inside helper methods



13
14
15
# File 'lib/middleman-sprockets/environment.rb', line 13

def last_request_path
  @last_request_path
end

Instance Method Details

#append_path(path) ⇒ Object

Invalidate sitemap when users mess with the sprockets load paths



154
155
156
157
158
# File 'lib/middleman-sprockets/environment.rb', line 154

def append_path(path)
  @app.sitemap.rebuild_resource_list!(:sprockets_paths)

  super
end

#call(env) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/middleman-sprockets/environment.rb', line 186

def call(env)
  # Set the app current path based on the full URL so that helpers work
  script_name = env['SCRIPT_NAME'].dup
  script_name.gsub!(/^#{@app.config[:http_prefix]}/i, '') if @app.config[:http_prefix]
  request_path = URI.decode(File.join(script_name, env['PATH_INFO']))
  if request_path.respond_to? :force_encoding
    request_path.force_encoding('UTF-8')
  end
  resource = @app.sitemap.find_resource_by_destination_path(request_path)

  if !resource && !debug_assets
    response = ::Rack::Response.new
    response.status = 404
    response.write """<html><body><h1>File Not Found</h1><p>#{request_path}</p>
  <p>If this is an an asset from a gem, add <tt>sprockets.import_asset '#{File.basename(request_path)}'</tt>
  to your <tt>config.rb</tt>.</body>"""
    return response.finish
  end

  if @app.respond_to?(:current_path=)
    @app.current_path = request_path
  else
    @last_request_path = request_path
  end

  # Fix https://github.com/sstephenson/sprockets/issues/533
  if resource && File.basename(resource.path) == 'bower.json'
    file = ::Rack::File.new nil
    file.path = resource.source_file
    response = file.serving({})
    response[1]['Content-Type'] = resource.content_type
    return response
  end

  if resource
    # incase the path has been rewrite, let sprockets know the original so it can find it
    logical_path = resource..fetch(:options, {})
                                    .fetch(:sprockets, {})
                                    .fetch(:logical_path, nil)
    env['PATH_INFO'] = logical_path.to_s if logical_path
  end

  super
end

#clear_pathsObject



166
167
168
169
# File 'lib/middleman-sprockets/environment.rb', line 166

def clear_paths
  @app.sitemap.rebuild_resource_list!(:sprockets_paths)
  super
end

#css_exception_response(exception) ⇒ Object



171
172
173
174
# File 'lib/middleman-sprockets/environment.rb', line 171

def css_exception_response(exception)
  raise exception if @app.build?
  super
end

#digestObject

Override Sprockets’ default digest function to not change depending on the exact Sprockets version. It still takes into account “version” which is a user-suppliable version number that can be used to force assets to have a new hash.



143
144
145
146
# File 'lib/middleman-sprockets/environment.rb', line 143

def digest
  @digest ||= Digest::SHA1.new.update(version.to_s)
  @digest.dup
end

#etag_match?(asset, env) ⇒ Boolean

Never return 304s, downstream may want to manipulate data.

Returns:

  • (Boolean)


182
183
184
# File 'lib/middleman-sprockets/environment.rb', line 182

def etag_match?(asset, env)
  false
end

#import_asset(asset_logical_path, &determine_output_dir) ⇒ Object

Tell Middleman to build this asset, referenced as a logical path.



232
233
234
235
236
237
238
239
240
# File 'lib/middleman-sprockets/environment.rb', line 232

def import_asset(asset_logical_path, &determine_output_dir)
  args = []
  args << asset_logical_path
  args << determine_output_dir if block_given?

  imported_assets << ImportedAsset.new(*args)

  @app.sitemap.rebuild_resource_list!(:sprockets_import_asset)
end

#javascript_exception_response(exception) ⇒ Object



176
177
178
179
# File 'lib/middleman-sprockets/environment.rb', line 176

def javascript_exception_response(exception)
  raise exception if @app.build?
  super
end

#path_fingerprint(path) ⇒ Object

Strip our custom 8-char hex/sha



149
150
151
# File 'lib/middleman-sprockets/environment.rb', line 149

def path_fingerprint(path)
  path[/-([0-9a-f]{8})\.[^.]+$/, 1]
end

#prepend_path(path) ⇒ Object



160
161
162
163
164
# File 'lib/middleman-sprockets/environment.rb', line 160

def prepend_path(path)
  @app.sitemap.rebuild_resource_list!(:sprockets_paths)

  super
end