forked from jwhitley/requirejs-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.rb
163 lines (145 loc) · 4.03 KB
/
config.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
require "pathname"
require "active_support/ordered_options"
require "erubis"
require "requirejs/error"
require "requirejs/rails"
module Requirejs
module Rails
class Config < ::ActiveSupport::OrderedOptions
LOADERS = [:requirejs, :almond]
def initialize(application)
super
self.manifest = nil
self.js_compressor = nil
self.logical_asset_filter = [/\.js$/, /\.html$/, /\.txt$/]
self.tmp_dir = application.root + 'tmp'
self.bin_dir = Pathname.new(__FILE__+'/../../../../bin').cleanpath
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
deps
jQuery
locale
map
packages
paths
priority
scriptType
shim
urlArgs
waitSeconds
xhtml
}
self.build_config_whitelist = %w{
appDir
baseUrl
closure
cssImportIgnore
cssIn
dir
fileExclusionRegExp
findNestedDependencies
has
hasOnSave
include
inlineText
locale
mainConfigFile
map
modules
name
namespace
onBuildRead
onBuildWrite
optimize
optimizeAllPluginResources
optimizeCss
out
packagePaths
packages
paths
pragmas
pragmasOnSave
preserveLicenseComments
shim
skipModuleInsertion
skipPragmas
skipDirOptimize
keepBuildDir
uglify
uglify2
useStrict
wrap
wrapShim
}
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
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
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
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
def module_name_for(mod)
case self.loader
when :almond
return mod['include']
when :requirejs
return mod['name']
end
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
end
end
end
end