Class: Nanoc::Filters::JavascriptConcatenator

Inherits:
Nanoc::Filter
  • Object
show all
Defined in:
lib/nanoc/filters/javascript_concatenator.rb

Constant Summary collapse

VERSION =
'0.0.2'
REQUIRE_RE =
/^\s*\/\/\s*=?\s*require\s+(.+?)\s*$/

Instance Method Summary collapse

Instance Method Details

#run(content, args = {}) ⇒ String

Provides a simple to include other JavaScript files so you can concatenate multiple JavaScript files together.

Example:

//= require file-one.js
//= require relative/path/to/file-two.js
//= require /absolute/path/to/file-three.js

This method takes no options.

Parameters:

  • content (String)

    The JavaScript

Returns:

  • (String)

    The resulting JavaScript



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nanoc/filters/javascript_concatenator.rb', line 27

def run(content, args = {})
  current_dir_pathname = Pathname.new(@item[:content_filename]).dirname.realpath
  required_items = []
  content = content.gsub(REQUIRE_RE) do |orig|
    imported_pathname = Pathname.new($1)
    imported_pathname = current_dir_pathname + imported_pathname if imported_pathname.relative?
    if imported_pathname.exist?
      imported_filename = imported_pathname.realpath
      @items.each do |i|
        if !i[:content_filename].nil? &&
           Pathname.new(i[:content_filename]).realpath == imported_filename
          required_items << i
        end
      end
      "\n;" + File.read(imported_filename)
    else
      orig
    end
  end
  depend_on required_items.uniq if required_items.size > 0
  content
end