jsrequire

A small lib to add a require functionality to JavaScript files.

Features

  • Dependencies are solved from one or multiple files
  • Multiple loadpaths for js file lookup
  • Simple assets collector for stylesheets
  • Support for namespaces like ExtJS requires it

A JavaScript file

On top of a file add something like:

/* js foo/bar */
/* css baz */

This requires the js file foo/bar.js and the stylesheet baz.css. Dependencies are resolved in right order of require statments over all files.

Controller

resolve js dependencies

path_to_js = "/absolute/path/to/javascripts"

jsrequire = JsRequire.new
dependencies = jsrequire.resolve_dependencies(File.join(path_to_js, "foo.js"))

rewrite js paths for web usage

@javascripts = JsRequire::web_path_helper(dependencies[:javascripts],
  path_to_js => "/public/javascripts"
})

@stylesheets = dependencies[:stylesheets]

A view file

e.g. in a Haml view template

- for css in @stylesheets
  %link{:rel => "stylesheet", :href => css, :type => "text/css"}

- for js in @javascripts
  %script{:type => "text/javascript", :src => js}

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Copyright (c) 2010 aekym. See LICENSE for details.