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

Commit

Permalink
Adapt the internals to the latest versions of Sprockets and sprockets…
Browse files Browse the repository at this point in the history
…-rails
  • Loading branch information
carsomyr committed Nov 3, 2016
1 parent 9008038 commit 1efba0e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 55 deletions.
37 changes: 9 additions & 28 deletions app/helpers/requirejs_helper.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
require "requirejs/error"
require "requirejs/rails/view_proxy"
require "requirejs/rails/view"

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

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

# EXPERIMENTAL: Additional priority settings appended to
Expand Down Expand Up @@ -85,26 +86,6 @@ def requirejs_include_tag(name = nil, &block)
end
end

def javascript_path(source, options = {})
if defined?(super)
if !SASS_RAILS_3_COMPATIBILITY
super
else
super(source)
end
else
view_proxy.javascript_path(source, options)
end
end

def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)
if defined?(super) && respond_to?(:output_buffer) && respond_to?(:output_buffer=)
super
else
view_proxy.content_tag(name, content_or_options_with_block, options, escape, &block)
end
end

private

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

def view_proxy
@view_proxy ||= Requirejs::Rails::ViewProxy.new
def view
@view ||= Requirejs::Rails::View.new
end
end
1 change: 1 addition & 0 deletions lib/requirejs/rails/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Config < ::ActiveSupport::OrderedOptions
LOGICAL_PATH_PATTERNS = [
Regexp.new("\\.html\\z"),
Regexp.new("\\.js\\z"),
Regexp.new("\\.es6\\z"),
Regexp.new("\\.txt\\z"),
BOWER_PATH_PATTERN
]
Expand Down
17 changes: 17 additions & 0 deletions lib/requirejs/rails/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ class Engine < ::Rails::Engine
end
end

# Are we running in the precompilation Rake task? If so, we need to adjust certain environmental configuration
# values.
if defined?(Rake) && Rake.application.top_level_tasks.include?("requirejs:precompile:all")
initializer "requirejs.modify_environment_config", after: "load_environment_config", group: :all do |app|
app.configure do
# If we don't set this to true, sprockets-rails will assign `Rails.application.assets` to `nil`.
config.assets.compile = true

# Don't compress JavaScripts fed into the r.js optimizer.
config.assets.js_compressor = false

# Don't use any cache to retrieve assets.
config.assets.cache = nil
end
end
end

if ::Rails::VERSION::MAJOR >= 4
config.after_initialize do |app|
config = app.config
Expand Down
8 changes: 8 additions & 0 deletions lib/requirejs/rails/view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Requirejs
module Rails
class View < ::ActionView::Base
# This allows requirejs-rails to serve up modules by their undigestified asset paths.
self.check_precompiled_asset = false
end
end
end
14 changes: 0 additions & 14 deletions lib/requirejs/rails/view_proxy.rb

This file was deleted.

14 changes: 1 addition & 13 deletions lib/tasks/requirejs-rails_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,6 @@ OS X Homebrew users can use 'brew install node'.
"requirejs:clean"] do
requirejs.config.source_dir.mkpath

# Save the original JS compressor and cache, which will be restored later.

original_js_compressor = requirejs.env.js_compressor
requirejs.env.js_compressor = false

original_cache = requirejs.env.cache
requirejs.env.cache = nil

requirejs.env.each_logical_path(requirejs.config.logical_path_patterns) do |logical_path|
m = ::Requirejs::Rails::Config::BOWER_PATH_PATTERN.match(logical_path)

Expand All @@ -118,10 +110,6 @@ OS X Homebrew users can use 'brew install node'.
end
end
end

# Restore the original JS compressor and cache.
requirejs.env.js_compressor = original_js_compressor
requirejs.env.cache = original_cache
end

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

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

Expand Down

0 comments on commit 1efba0e

Please sign in to comment.