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

Commit 1efba0e

Browse files
committed
Adapt the internals to the latest versions of Sprockets and sprockets-rails
1 parent 9008038 commit 1efba0e

File tree

6 files changed

+36
-55
lines changed

6 files changed

+36
-55
lines changed

app/helpers/requirejs_helper.rb

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
require "requirejs/error"
2-
require "requirejs/rails/view_proxy"
2+
require "requirejs/rails/view"
33

44
module RequirejsHelper
5-
if defined?(Sass::Rails::VERSION)
6-
sass_rails_version_pattern = Regexp.new("\\A(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\z")
5+
def self.included(clazz)
6+
clazz.class_eval do
7+
extend Forwardable
78

8-
SASS_RAILS_3_COMPATIBILITY = sass_rails_version_pattern.match(Sass::Rails::VERSION)[1].to_i < 4
9-
else
10-
SASS_RAILS_3_COMPATIBILITY = false
9+
# Delegate all JavaScript path queries to the specially modified internal view.
10+
def_delegators :view, :javascript_path
11+
end
1112
end
1213

1314
# EXPERIMENTAL: Additional priority settings appended to
@@ -85,26 +86,6 @@ def requirejs_include_tag(name = nil, &block)
8586
end
8687
end
8788

88-
def javascript_path(source, options = {})
89-
if defined?(super)
90-
if !SASS_RAILS_3_COMPATIBILITY
91-
super
92-
else
93-
super(source)
94-
end
95-
else
96-
view_proxy.javascript_path(source, options)
97-
end
98-
end
99-
100-
def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)
101-
if defined?(super) && respond_to?(:output_buffer) && respond_to?(:output_buffer=)
102-
super
103-
else
104-
view_proxy.content_tag(name, content_or_options_with_block, options, escape, &block)
105-
end
106-
end
107-
10889
private
10990

11091
def once_guard
@@ -129,7 +110,7 @@ def base_url(js_asset)
129110
[asset_host, Rails.application.config.relative_url_root, Rails.application.config.assets.prefix].join
130111
end
131112

132-
def view_proxy
133-
@view_proxy ||= Requirejs::Rails::ViewProxy.new
113+
def view
114+
@view ||= Requirejs::Rails::View.new
134115
end
135116
end

lib/requirejs/rails/config.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Config < ::ActiveSupport::OrderedOptions
1616
LOGICAL_PATH_PATTERNS = [
1717
Regexp.new("\\.html\\z"),
1818
Regexp.new("\\.js\\z"),
19+
Regexp.new("\\.es6\\z"),
1920
Regexp.new("\\.txt\\z"),
2021
BOWER_PATH_PATTERN
2122
]

lib/requirejs/rails/engine.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ class Engine < ::Rails::Engine
5050
end
5151
end
5252

53+
# Are we running in the precompilation Rake task? If so, we need to adjust certain environmental configuration
54+
# values.
55+
if defined?(Rake) && Rake.application.top_level_tasks.include?("requirejs:precompile:all")
56+
initializer "requirejs.modify_environment_config", after: "load_environment_config", group: :all do |app|
57+
app.configure do
58+
# If we don't set this to true, sprockets-rails will assign `Rails.application.assets` to `nil`.
59+
config.assets.compile = true
60+
61+
# Don't compress JavaScripts fed into the r.js optimizer.
62+
config.assets.js_compressor = false
63+
64+
# Don't use any cache to retrieve assets.
65+
config.assets.cache = nil
66+
end
67+
end
68+
end
69+
5370
if ::Rails::VERSION::MAJOR >= 4
5471
config.after_initialize do |app|
5572
config = app.config

lib/requirejs/rails/view.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module Requirejs
2+
module Rails
3+
class View < ::ActionView::Base
4+
# This allows requirejs-rails to serve up modules by their undigestified asset paths.
5+
self.check_precompiled_asset = false
6+
end
7+
end
8+
end

lib/requirejs/rails/view_proxy.rb

Lines changed: 0 additions & 14 deletions
This file was deleted.

lib/tasks/requirejs-rails_tasks.rake

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,6 @@ OS X Homebrew users can use 'brew install node'.
8888
"requirejs:clean"] do
8989
requirejs.config.source_dir.mkpath
9090

91-
# Save the original JS compressor and cache, which will be restored later.
92-
93-
original_js_compressor = requirejs.env.js_compressor
94-
requirejs.env.js_compressor = false
95-
96-
original_cache = requirejs.env.cache
97-
requirejs.env.cache = nil
98-
9991
requirejs.env.each_logical_path(requirejs.config.logical_path_patterns) do |logical_path|
10092
m = ::Requirejs::Rails::Config::BOWER_PATH_PATTERN.match(logical_path)
10193

@@ -118,10 +110,6 @@ OS X Homebrew users can use 'brew install node'.
118110
end
119111
end
120112
end
121-
122-
# Restore the original JS compressor and cache.
123-
requirejs.env.js_compressor = original_js_compressor
124-
requirejs.env.cache = original_cache
125113
end
126114

127115
task generate_rjs_driver: ["requirejs:setup"] do
@@ -162,7 +150,7 @@ OS X Homebrew users can use 'brew install node'.
162150
built_asset_path = requirejs.config.build_dir.join(asset_name)
163151

164152
# Compute the digest based on the contents of the compiled file, *not* on the contents of the RequireJS module.
165-
file_digest = ::Rails.application.assets.file_digest(built_asset_path.to_s)
153+
file_digest = requirejs.env.file_digest(built_asset_path.to_s)
166154
hex_digest = file_digest.unpack("H*").first
167155
digest_name = asset.logical_path.gsub(path_extension_pattern) { |ext| "-#{hex_digest}#{ext}" }
168156

0 commit comments

Comments
 (0)