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



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

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



8
9
10
# File 'lib/middleman-sprockets/environment.rb', line 8

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.



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

def imported_assets
  @imported_assets
end

#last_request_pathObject (readonly)

The current path, useful when inside helper methods



15
16
17
# File 'lib/middleman-sprockets/environment.rb', line 15

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



168
169
170
171
172
# File 'lib/middleman-sprockets/environment.rb', line 168

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

  super
end

#call(env) ⇒ Object



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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/middleman-sprockets/environment.rb', line 200

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



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

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

#css_exception_response(exception) ⇒ Object



185
186
187
188
# File 'lib/middleman-sprockets/environment.rb', line 185

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.



157
158
159
160
# File 'lib/middleman-sprockets/environment.rb', line 157

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)


196
197
198
# File 'lib/middleman-sprockets/environment.rb', line 196

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.



246
247
248
249
250
251
252
253
254
# File 'lib/middleman-sprockets/environment.rb', line 246

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



190
191
192
193
# File 'lib/middleman-sprockets/environment.rb', line 190

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

#path_fingerprint(path) ⇒ Object

Strip our custom 8-char hex/sha



163
164
165
# File 'lib/middleman-sprockets/environment.rb', line 163

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

#prepend_path(path) ⇒ Object



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

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

  super
end

#prune_imported_assets!Object

Make sure all the defined imported assets still exist



61
62
63
64
65
66
67
68
69
70
# File 'lib/middleman-sprockets/environment.rb', line 61

def prune_imported_assets!
  @imported_assets = @imported_assets.select do |a|
    begin
      a = Middleman::Sprockets::Asset.new(@app, a.logical_path, self)
      a.exist?
    rescue
      false
    end
  end
end