Class: Jekyll::Assets::Env

Inherits:
Sprockets::Environment show all
Extended by:
Forwardable::Extended
Includes:
Utils
Defined in:
lib/jekyll/assets/env.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

activate, #external?, #external_asset, #find_assets_by_glob, #glob_paths, html, html_fragment, #in_cache_dir, #in_dest_dir, javascript?, make_https, manifest_files, new_uglifier?, old_sprockets?, #parse_liquid, #prefix_url, #raw_precompiles, #strip_paths, strip_secondary_content_type, strip_slashes, #url_asset, xml

Constructor Details

#initialize(jekyll = nil) ⇒ Env



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
# File 'lib/jekyll/assets/env.rb', line 39

def initialize(jekyll = nil)
  @asset_config = Config.new(jekyll.config["assets"] ||= {})
  Hook.trigger :env, :before_init do |h|
    instance_eval(&h)
  end

  super()
  @jekyll = jekyll
  @assets_to_write = []
  @manifest = Manifest.new(self, in_dest_dir)
  @jekyll.sprockets = self
  @total_time = 0.000000
  @logger = Logger
  @cache = nil

  setup_sources!
  setup_resolver!
  ignore_caches!
  setup_drops!
  precompile!
  copy_raw!

  Hook.trigger :env, :after_init do |h|
    instance_eval(&h)
  end
end

Instance Attribute Details

#asset_configObject (readonly)

Returns the value of attribute asset_config.



35
36
37
# File 'lib/jekyll/assets/env.rb', line 35

def asset_config
  @asset_config
end

#assets_to_writeObject

Returns the value of attribute assets_to_write.



34
35
36
# File 'lib/jekyll/assets/env.rb', line 34

def assets_to_write
  @assets_to_write
end

#jekyllObject (readonly)

Returns the value of attribute jekyll.



36
37
38
# File 'lib/jekyll/assets/env.rb', line 36

def jekyll
  @jekyll
end

#manifestObject (readonly)



33
34
35
# File 'lib/jekyll/assets/env.rb', line 33

def manifest
  @manifest
end

Instance Method Details

#cacheSprockets::Cache

Note:

this is configurable with :caching -> :type

Note:

this is configurable with :caching -> :enabled

– Create a cache, or a null cache (if no caching) for caching. –

Returns:

  • (Sprockets::Cache)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/jekyll/assets/env.rb', line 101

def cache
  @cache ||= begin
    type = asset_config[:caching][:type]
    enbl = asset_config[:caching][:enabled]
    path = in_cache_dir

    out = Sprockets::Cache::MemoryStore.new if enbl && type == "memory"
    out = Sprockets::Cache::FileStore.new(path) if enbl && type == "file"
    out = Sprockets::Cache::NullStore.new unless enbl
    out = Sprockets::Cache.new(out, Logger)
    clear_cache(out)
    out
  end
end

#find_asset(v, *a) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/jekyll/assets/env.rb', line 72

def find_asset(v, *a)
  msg = "Searched for, and rendered #{v} in %<time>s"
  out = Logger.with_timed_logging msg do
    super(v, *a)
  end

  # We keep track.
  @total_time += out[:time]
  out[:result]
end

#find_asset!(v, *a) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/jekyll/assets/env.rb', line 84

def find_asset!(v, *a)
  msg = "Searched for, and rendered #{v} in %<time>s"
  out = Logger.with_timed_logging msg do
    super(v, *a)
  end

  # We keep track.
  @total_time += out[:time]
  out[:result]
end

#remove_old_assetsObject




150
151
152
153
154
# File 'lib/jekyll/assets/env.rb', line 150

def remove_old_assets
  assets_to_write.each do |v|
    in_dest_dir(find_asset!(v).logical_path).rm_f
  end
end

#skip_gzip?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/jekyll/assets/env.rb', line 67

def skip_gzip?
  !asset_config[:gzip]
end

#to_liquid_payloadHash

Note:

this does not find the asset.

– Takes all user assets and turns them into a drop. –

Returns:

  • (Hash)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/jekyll/assets/env.rb', line 121

def to_liquid_payload
  each_file.each_with_object({}) do |k, h|
    skip, path = false, Pathutil.new(strip_paths(k))
    path.descend do |p|
      skip = p.start_with?("_")
      if skip
        break
      end
    end

    next if skip
    h.update({
      path.to_s => Drop.new(path, {
        jekyll: jekyll,
      }),
    })
  end
end

#write_allObject



141
142
143
144
145
146
147
# File 'lib/jekyll/assets/env.rb', line 141

def write_all
  remove_old_assets unless asset_config[:digest]
  manifest.compile(*assets_to_write); @asset_to_write = []
  Hook.trigger(:env, :after_write) { |h| instance_eval(&h) }
  Logger.debug "took #{format(@total_time.round(2).to_s,
    '%.2f')}s"
end