module Jekyll::JsBundle

The main GTN function library

Public Instance Methods

bundle_preloads(_test) click to toggle source

Return the preloads for the bundles, when in production

test

ignore this

Returns the HTML to load the bundle

Example: {{ ‘load’ | bundle_preloads }}

# File _plugins/jekyll-bundler.rb, line 54
def bundle_preloads(_test)
  if Jekyll.env == 'production'
    bundle_preloads_prod
  else
    ''
  end
end
bundle_preloads_prod() click to toggle source

(Internal) Return the production preloads for the bundles

# File _plugins/jekyll-bundler.rb, line 63
def bundle_preloads_prod
  bundles = @context.registers[:site].config['javascript_bundles']
  baseurl = @context.registers[:site].config['baseurl']

  # Select the ones wishing to be preloaded
  bundles = bundles.select do |_name, bundle|
    bundle['preload'] == true
  end

  bundles.map do |_name, bundle|
    bundle_path = "#{baseurl}#{bundle['path']}"
    "<link rel='preload' href='#{bundle_path}' as='script'>"
  end.join("\n")
end
load_bundle(name) click to toggle source

Load a specific bundle, in liquid

name

the name of the bundle to load

Returns the HTML to load the bundle

Example: {{ ‘main’ | load_bundle }}

# File _plugins/jekyll-bundler.rb, line 84
def load_bundle(name)
  if Jekyll.env == 'production'
    load_bundle_production(name)
  else
    load_bundle_dev(name)
  end
end
load_bundle_dev(name) click to toggle source
# File _plugins/jekyll-bundler.rb, line 92
def load_bundle_dev(name)
  bundle = @context.registers[:site].config['javascript_bundles'][name]
  raise "Bundle #{name} not found in site config" if bundle.nil?

  Jekyll.logger.debug "[GTN/Bundler] Bundle #{bundle}"

  baseurl = @context.registers[:site].config['baseurl']

  bundle['resources'].map do |f|
    "<script src='#{baseurl}/#{f}'></script>"
  end.join("\n")
end
load_bundle_production(name) click to toggle source
# File _plugins/jekyll-bundler.rb, line 105
def load_bundle_production(name)
  bundle = @context.registers[:site].config['javascript_bundles'][name]
  raise "Bundle #{name} not found in site config" if bundle.nil?

  baseurl = @context.registers[:site].config['baseurl']
  attrs = ''
  attrs += ' async' if bundle['async']
  attrs += ' defer' if bundle['defer']
  bundle_path = "#{baseurl}#{bundle['path']}"
  "<script #{attrs} src='#{bundle_path}'></script>"
end