Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit 24a2975

Browse files
committed
Improve handling of Bower packages
1 parent d25d105 commit 24a2975

File tree

4 files changed

+47
-26
lines changed

4 files changed

+47
-26
lines changed

lib/requirejs/rails/config.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,21 @@ module Rails
1111
class Config < ::ActiveSupport::OrderedOptions
1212
LOADERS = [:requirejs, :almond]
1313

14+
BOWER_PATH_PATTERN = Regexp.new("\\A(.*)/(?:\\.bower|bower|component)\\.json\\z")
15+
16+
LOGICAL_PATH_PATTERNS = [
17+
Regexp.new("\\.html\\z"),
18+
Regexp.new("\\.js\\z"),
19+
Regexp.new("\\.txt\\z"),
20+
BOWER_PATH_PATTERN
21+
]
22+
1423
def initialize(application)
1524
super
25+
1626
self.manifest = nil
27+
self.logical_path_patterns = LOGICAL_PATH_PATTERNS
1728

18-
self.logical_asset_filter = [/\.js$/, /\.html$/, /\.txt$/]
1929
self.tmp_dir = application.root + 'tmp'
2030
self.bin_dir = Pathname.new(__FILE__+'/../../../../bin').cleanpath
2131

@@ -149,10 +159,10 @@ def get_binding
149159
return binding()
150160
end
151161

152-
def asset_allowed?(asset)
153-
self.logical_asset_filter.reduce(false) do |accum, matcher|
154-
accum || (matcher =~ asset)
155-
end ? true : false
162+
def asset_allowed?(logical_path)
163+
logical_path_patterns.reduce(false) do |accum, pattern|
164+
accum || !!(pattern.match(logical_path))
165+
end
156166
end
157167
end
158168
end

lib/requirejs/rails/engine.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Engine < ::Rails::Engine
5454
config.after_initialize do |app|
5555
config = app.config
5656
rails_manifest_path = File.join(app.root, 'public', config.assets.prefix)
57-
rails_manifest = Sprockets::Manifest.new(app.assets, rails_manifest_path)
57+
rails_manifest = ::Sprockets::Manifest.new(app.assets, rails_manifest_path)
5858
if config.requirejs.manifest_path.exist? && rails_manifest
5959
rjs_digests = YAML.load(ERB.new(File.new(config.requirejs.manifest_path).read).result)
6060
rails_manifest.assets.merge!(rjs_digests)

lib/tasks/requirejs-rails_tasks.rake

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace :requirejs do
3939
end
4040

4141
task setup: ["assets:environment"] do
42-
unless defined?(Sprockets)
42+
unless defined?(::Sprockets)
4343
warn "Cannot precompile assets if sprockets is disabled. Please set config.assets.enabled to true"
4444
exit
4545
end
@@ -88,14 +88,6 @@ OS X Homebrew users can use 'brew install node'.
8888
# Copy all assets to the temporary staging directory.
8989
task prepare_source: ["requirejs:setup",
9090
"requirejs:clean"] do
91-
bower_json_pattern = Regexp.new("\\A(.*)/bower\\.json\\z")
92-
93-
js_ext = if requirejs.env.respond_to?(:extension_for_mime_type)
94-
requirejs.env.extension_for_mime_type("application/javascript")
95-
else
96-
requirejs.env.mime_types["application/javascript"][:extensions].first
97-
end
98-
9991
requirejs.config.source_dir.mkpath
10092

10193
# Save the original JS compressor and cache, which will be restored later.
@@ -106,16 +98,31 @@ OS X Homebrew users can use 'brew install node'.
10698
original_cache = requirejs.env.cache
10799
requirejs.env.cache = nil
108100

109-
requirejs.env.logical_paths do |logical_path|
101+
js_ext = requirejs.env.mime_types["application/javascript"][:extensions].first
102+
103+
requirejs.env.logical_paths do |logical_path, physical_path|
104+
next \
105+
if !requirejs.config.asset_allowed?(logical_path)
106+
107+
m = ::Requirejs::Rails::Config::BOWER_PATH_PATTERN.match(logical_path)
110108

111-
next if ! requirejs.config.asset_allowed?(logical_path)
109+
if !m
110+
asset = requirejs.env.find_asset(logical_path)
112111

113-
asset = requirejs.env.find_asset(logical_path)
112+
if asset
113+
file = requirejs.config.source_dir.join(asset.logical_path)
114+
file.dirname.mkpath
115+
asset.write_to(file)
116+
end
117+
else
118+
bower_logical_path = Pathname.new(logical_path).dirname.sub_ext(js_ext).to_s
119+
asset = requirejs.env.find_asset(bower_logical_path)
114120

115-
if asset
116-
filename = requirejs.config.source_dir.join(asset.logical_path)
117-
filename.dirname.mkpath
118-
asset.write_to(filename)
121+
if asset
122+
file = requirejs.config.source_dir.join(bower_logical_path)
123+
file.dirname.mkpath
124+
asset.write_to(file)
125+
end
119126
end
120127
end
121128

test/requirejs-rails_test.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def setup
4141
test "matches configured logical assets" do
4242
assert_equal true, @cfg.asset_allowed?('foo.js')
4343
assert_equal false, @cfg.asset_allowed?('bar.frobnitz')
44-
@cfg.logical_asset_filter += [/\.frobnitz$/]
44+
@cfg.logical_path_patterns += [/\.frobnitz$/]
4545
assert_equal true, @cfg.asset_allowed?('bar.frobnitz')
4646
end
4747

@@ -136,9 +136,13 @@ def wrap(tag)
136136
end
137137

138138
test "requirejs_include_tag_with_block" do
139-
render text: wrap(requirejs_include_tag("application") do
140-
{"class" => controller.class.name.demodulize}
141-
end)
139+
result = wrap(
140+
requirejs_include_tag("application") do
141+
{"class" => controller.class.name.demodulize}
142+
end
143+
)
144+
145+
render text: result
142146

143147
assert_select "script:first-of-type[src=\"/javascripts/require.js\"]" \
144148
"[data-class=\"TestController\"]", count: 1

0 commit comments

Comments
 (0)