Class: JsRequire

Inherits:
Object
  • Object
show all
Defined in:
lib/jsrequire.rb

Defined Under Namespace

Classes: FileNotFoundInLoadpath

Constant Summary collapse

ALLOWED_EXTENSIONS =
%w(coffee js)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loadpaths = nil) ⇒ JsRequire

Returns a new instance of JsRequire.



7
8
9
10
11
12
13
14
15
16
# File 'lib/jsrequire.rb', line 7

def initialize(loadpaths = nil)
  @extract_loadpaths = []

  loadpaths = [loadpaths] unless loadpaths.is_a?(Array)
  @additional_loadpaths = JsRequire::normalize_filepaths(loadpaths.compact)

  @preprocessors = Hash.new { |h,k| h[k] = [] }

  on("css", &method(:collect_css))
end

Class Method Details

.namespace_helper(files, namespace_prefix) ⇒ Object

builds namespaces from script files by pathnames when the <namespace_prefix> is found in path.

e.g.

namespace_helper([“/foo/bar/quux/file1.js”, “/foo/bar/baz/file2.js”], “bar”)

=> ["bar.baz", "bar.quux"]

Interessting for ExtJs#namespace



96
97
98
99
100
101
102
103
104
# File 'lib/jsrequire.rb', line 96

def self.namespace_helper(files, namespace_prefix)
  files.inject([]) do |arr,js|
    if js =~ /\/(#{namespace_prefix}\/.+)$/
      file = File.dirname($1).gsub("/", ".")
      arr << file
    end
    arr
  end.sort.uniq
end

.web_path_helper(files, webroots) ⇒ Object

convert absolute filepaths to relatives by cutting the absolute path to the webroot

returns the webroot relative filepaths

web_path_helper(, => “/javascripts”)

=> ["/javascripts/bar.js"]

Parameters:

  • webroots:

    array of strings to remove the prefix path or a hash to replace with defined string



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/jsrequire.rb', line 71

def self.web_path_helper(files, webroots)
  webroots = [webroots] unless webroots.is_a?(Enumerable)

  files.map do |f|
    rel_file = nil
    webroots.each do |wr, replacement|
      wr = normalize_filepath(wr)
      rel_file = f.sub(/^#{Regexp.escape wr}/, replacement || '')
      break if rel_file != f
    end
    rel_file || f
  end
end

Instance Method Details

#collect_css(action, param) ⇒ Object



24
25
26
27
# File 'lib/jsrequire.rb', line 24

def collect_css(action, param)
  @css << param + ".css"
  nil
end

#on(action = nil, &block) ⇒ Object



19
20
21
# File 'lib/jsrequire.rb', line 19

def on(action = nil, &block)
  @preprocessors[action] << block
end

#resolve_dependencies(files) ⇒ Object

resolve dependencies of js input files

returns a hash with js and css dependencies

js files are returned with absolute filepaths, css files not. css files are returned as given by the parsed require statement.

e.g.

:javascripts => ["/foo/bar.js"],
:stylesheets => ["style.css"]



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jsrequire.rb', line 45

def resolve_dependencies(files)
  files = [files] unless files.is_a?(Enumerable)

  @css = []
  @extract_loadpaths = extract_loadpaths(files)

  js = extract_dependencies_recursive(JsRequire::normalize_filepaths(files))

  {
    :javascripts => js,
    :stylesheets => @css.uniq.sort
  }
end