Class: PuppetHerald::Javascript

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-herald/javascript.rb

Overview

A javascript processing class

Instance Method Summary collapse

Constructor Details

#initializeJavascript

Initialize JS class



9
10
11
12
# File 'lib/puppet-herald/javascript.rb', line 9

def initialize
  @files = nil
  @base = 'lib/puppet-herald/public'
end

Instance Method Details

#filesArray

Returns a list of JS files to be inserted into main HTML

Returns:

  • (Array)

    list of JS’s



16
17
18
19
20
21
22
23
24
# File 'lib/puppet-herald/javascript.rb', line 16

def files
  @files = nil if PuppetHerald.in_dev?
  if @files.nil?
    public_dir = PuppetHerald.relative_dir(@base)
    all = Dir.chdir(public_dir) { Dir.glob('**/*.js') }
    @files = all.reverse.reject { |file| file.match(/_test\.js$/) }
  end
  @files
end

#uglify(mapname) ⇒ Hash

Uglify an application JS’s into one minified JS file

Parameters:

  • mapname (String)

    name of source map to be put into uglified JS

Returns:

  • (Hash)

    a hash with uglified JS and source map



29
30
31
32
33
34
35
36
37
38
# File 'lib/puppet-herald/javascript.rb', line 29

def uglify(mapname)
  sources = files.collect { |file| File.read("#{@base}/#{file}") }
  source = sources.join "\n"
  uglifier = Uglifier.new(source_map_url: mapname)
  uglified, source_map = uglifier.compile_with_map(source)
  {
    'js'     => uglified,
    'js.map' => source_map
  }
end