lib/requirejs/rails/config.rb in requirejs-rails-0.9.5 vs lib/requirejs/rails/config.rb in requirejs-rails-0.9.6
- old
+ new
@@ -1,37 +1,49 @@
-require 'requirejs/rails'
-require 'requirejs/error'
+require "pathname"
-require 'active_support/ordered_options'
-require 'erubis'
-require 'pathname'
+require "active_support/ordered_options"
+require "erubis"
-module Requirejs::Rails
- class Config < ::ActiveSupport::OrderedOptions
- LOADERS = [ :requirejs, :almond ]
+require "requirejs/error"
+require "requirejs/rails"
- def initialize(application)
- super
- self.manifest = nil
+module Requirejs
+ module Rails
+ class Config < ::ActiveSupport::OrderedOptions
+ LOADERS = [:requirejs, :almond]
- self.logical_asset_filter = [/\.js$/,/\.html$/,/\.txt$/]
- self.tmp_dir = application.root + 'tmp'
- self.bin_dir = Pathname.new(__FILE__+'/../../../../bin').cleanpath
+ BOWER_PATH_PATTERN = Regexp.new("\\A(.*)/(?:\\.bower|bower|component)\\.json\\z")
- self.source_dir = self.tmp_dir.join("requirejs/src")
- self.build_dir = self.tmp_dir.join("requirejs/dst")
- self.target_dir = application.root + 'public/assets'
- self.rjs_path = self.bin_dir+'r.js'
+ LOGICAL_PATH_PATTERNS = [
+ Regexp.new("\\.html\\z"),
+ Regexp.new("\\.js\\z"),
+ Regexp.new("\\.txt\\z"),
+ BOWER_PATH_PATTERN
+ ]
- self.loader = :requirejs
+ def initialize(application)
+ super
- self.driver_template_path = Pathname.new(__FILE__+'/../rjs_driver.js.erb').cleanpath
- self.driver_path = self.tmp_dir.join("requirejs/rjs_driver.js")
+ self.manifest = nil
+ self.logical_path_patterns = LOGICAL_PATH_PATTERNS
- self.user_config = {}
+ self.tmp_dir = application.root + 'tmp'
+ self.bin_dir = Pathname.new(__FILE__+'/../../../../bin').cleanpath
- self.run_config_whitelist = %w{
+ self.source_dir = self.tmp_dir.join("requirejs/src")
+ self.build_dir = self.tmp_dir.join("requirejs/dst")
+ self.target_dir = application.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.join("requirejs/rjs_driver.js")
+
+ self.user_config = {}
+
+ self.run_config_whitelist = %w{
baseUrl
callback
catchError
config
context
@@ -47,11 +59,11 @@
urlArgs
waitSeconds
xhtml
}
- self.build_config_whitelist = %w{
+ self.build_config_whitelist = %w{
appDir
baseUrl
closure
cssImportIgnore
cssIn
@@ -86,71 +98,72 @@
uglify
uglify2
useStrict
wrap
}
- end
+ end
- def loader=(sym)
- unless LOADERS.include?(sym)
- raise Requirejs::ConfigError, "Attempt to set unknown loader: #{sym}"
+ def loader=(sym)
+ unless LOADERS.include?(sym)
+ raise Requirejs::ConfigError, "Attempt to set unknown loader: #{sym}"
+ end
+ self[:loader] = sym
end
- self[:loader] = sym
- end
- def build_config
- 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}."
+ def build_config
+ 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
- mod = mods[0]
- name = mod['name']
- mod['name'] = 'almond'
- mod['include'] = name
end
+ self[:build_config]
end
- self[:build_config]
- end
- def run_config
- unless self.has_key?(:run_config)
- self[:run_config] = { "baseUrl" => "/assets" }
- self[:run_config].merge!(self.user_config).slice!(*self.run_config_whitelist)
+ def run_config
+ unless self.has_key?(:run_config)
+ self[:run_config] = {"baseUrl" => "/assets"}
+ self[:run_config].merge!(self.user_config).slice!(*self.run_config_whitelist)
+ end
+ self[:run_config]
end
- self[:run_config]
- end
- def user_config=(cfg)
- if url = cfg.delete('baseUrl')
- raise Requirejs::ConfigError, "baseUrl is not needed or permitted in the configuration"
+ def user_config=(cfg)
+ if url = cfg.delete('baseUrl')
+ raise Requirejs::ConfigError, "baseUrl is not needed or permitted in the configuration"
+ end
+ self[:user_config] = cfg
end
- self[:user_config] = cfg
- end
- def module_name_for(mod)
- case self.loader
- when :almond
- return mod['include']
- when :requirejs
- return mod['name']
+ def module_name_for(mod)
+ case self.loader
+ when :almond
+ return mod['include']
+ when :requirejs
+ return mod['name']
+ end
end
- end
- def get_binding
- return binding()
- end
+ def get_binding
+ return binding()
+ end
- def asset_allowed?(asset)
- self.logical_asset_filter.reduce(false) do |accum, matcher|
- accum || (matcher =~ asset)
- end ? true : false
+ def asset_allowed?(logical_path)
+ logical_path_patterns.reduce(false) do |accum, pattern|
+ accum || !!(pattern.match(logical_path))
+ end
+ end
end
end
end