lib/requirejs/rails/config.rb in requirejs-rails-0.6.1 vs lib/requirejs/rails/config.rb in requirejs-rails-0.7.0
- old
+ new
@@ -1,37 +1,32 @@
require 'requirejs/rails'
+require 'requirejs/error'
require 'active_support/ordered_options'
require 'erubis'
require 'pathname'
module Requirejs::Rails
class Config < ::ActiveSupport::OrderedOptions
+ LOADERS = [ :requirejs, :almond ]
- def initialize(app=Rails.application)
+ def initialize
super
self.manifest = nil
self.tmp_dir = Rails.root + 'tmp'
self.bin_dir = Pathname.new(__FILE__+'/../../../../bin').cleanpath
self.source_dir = self.tmp_dir + 'assets'
self.target_dir = Rails.root + 'public/assets'
self.rjs_path = self.bin_dir+'r.js'
+ self.loader = :requirejs
+
self.driver_template_path = Pathname.new(__FILE__+'/../rjs_driver.js.erb').cleanpath
self.driver_path = self.tmp_dir + 'rjs_driver.js'
- # The user-supplied config parameters, to be merged with the default params.
- # This file must contain a single JavaScript object.
- self.user_config_file = Pathname.new(app.paths["config"].first)+'requirejs.yml'
- if self.user_config_file.exist?
- self.user_config = YAML.load(self.user_config_file.read)
- else
- self.user_config = {}
- end
-
self.run_config_whitelist = %w{
baseUrl
callback
catchError
context
@@ -83,24 +78,54 @@
useStrict
wrap
}
end
+ def loader=(sym)
+ unless LOADERS.include?(sym)
+ raise Requirejs::ConfigError, "Attempt to set unknown loader: #{sym}"
+ end
+ self[:loader] = sym
+ end
+
def build_config
- build_config = self.run_config.merge "baseUrl" => source_dir.to_s
- build_config.merge!(self.user_config).slice(*self.build_config_whitelist)
+ unless self.has_key?(:build_config)
+ self[:build_config] = self.run_config.merge "baseUrl" => source_dir.to_s,
+ "modules" => [ { 'name' => 'application' } ]
+ self[:build_config].merge!(self.user_config).slice!(*self.build_config_whitelist)
+ case self.loader
+ when :requirejs
+ # nothing to do
+ when :almond
+ mods = self[:build_config]['modules']
+ unless mods.length == 1
+ raise Requirejs::ConfigError, "Almond build requires exactly one module, config has #{mods.length}."
+ end
+ mod = mods[0]
+ name = mod['name']
+ mod['name'] = 'almond'
+ mod['include'] = name
+ end
+ end
+ self[:build_config]
end
def run_config
- run_config = {
- "baseUrl" => "/assets",
- "modules" => [ { 'name' => 'application' } ]
- }
+ run_config = { "baseUrl" => "/assets" }
run_config.merge!(self.user_config).slice(*self.run_config_whitelist)
end
- def module_path_for(name)
- self.target_dir+(name+'.js')
+ def module_name_for(mod)
+ case self.loader
+ when :almond
+ return mod['include']
+ when :requirejs
+ return mod['name']
+ end
+ end
+
+ def module_path_for(mod)
+ self.target_dir+(module_name_for(mod)+'.js')
end
def get_binding
return binding()
end