Class: Jekyll::Minibundle::AssetBundle

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/minibundle/asset_bundle.rb

Constant Summary collapse

TEMPFILE_PREFIX =
'jekyll-minibundle-'

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ AssetBundle

Returns a new instance of AssetBundle.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/jekyll/minibundle/asset_bundle.rb', line 12

def initialize(config)
  @type = config.fetch(:type)
  @asset_paths = config.fetch(:asset_paths)
  @destination_path = config.fetch(:destination_path)
  @site_dir = config.fetch(:site_dir)
  @minifier_cmd = config.fetch(:minifier_cmd)

  unless @minifier_cmd
    raise <<~MESSAGE
      Missing minification command for bundling #{bundle_destination_path.inspect}. Specify it in
      1) minibundle.minifier_commands.#{@type} setting in _config.yml,
      2) $JEKYLL_MINIBUNDLE_CMD_#{@type.to_s.upcase} environment variable, or
      3) minifier_cmd setting inside minibundle block.
    MESSAGE
  end

  @tempfile = Tempfile.new([TEMPFILE_PREFIX, ".#{@type}"])
end

Instance Method Details

#closeObject



31
32
33
34
# File 'lib/jekyll/minibundle/asset_bundle.rb', line 31

def close
  @tempfile.close!
  @tempfile = nil
end

#make_bundleObject

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jekyll/minibundle/asset_bundle.rb', line 42

def make_bundle
  raise IllegalStateError, 'Cannot make bundle with closed AssetBundle' unless @tempfile

  exit_status = spawn_minifier(@minifier_cmd) do |input|
    $stdout.puts  # place newline after "(Re)generating..." log messages
    Log.info("Bundling #{bundle_destination_path}:")
    @asset_paths.each do |asset|
      Log.info(" #{relative_path_from(asset, @site_dir)}")
      File.foreach(asset) { |line| input.write(line) }
      input.puts(';') if @type == :js
    end
  end

  if exit_status != 0
    msg = "Bundling #{bundle_destination_path.inspect} failed with exit status #{exit_status}, command: #{@minifier_cmd.inspect}"
    log_minifier_error(msg)
    raise msg
  end

  self
end

#pathObject

Raises:



36
37
38
39
40
# File 'lib/jekyll/minibundle/asset_bundle.rb', line 36

def path
  raise IllegalStateError, 'Cannot get path of closed AssetBundle' unless @tempfile

  @tempfile.path
end