From e7402e0646b1818e2d4ebe696b30550d941c56f4 Mon Sep 17 00:00:00 2001 From: Christine Lao Date: Fri, 11 Aug 2017 14:51:29 -0700 Subject: [PATCH] Fix for Sprockets 3 on Rails 5 https://github.com/jwhitley/requirejs-rails/issues/268 --- lib/tasks/requirejs-rails_tasks.rake | 80 +++++++++++++++++++--------- 1 file changed, 56 insertions(+), 24 deletions(-) diff --git a/lib/tasks/requirejs-rails_tasks.rake b/lib/tasks/requirejs-rails_tasks.rake index 9aa1714e..c846ef55 100644 --- a/lib/tasks/requirejs-rails_tasks.rake +++ b/lib/tasks/requirejs-rails_tasks.rake @@ -52,7 +52,9 @@ namespace :requirejs do # sprockets hooks get executed _ = ActionView::Base - requirejs.env = Rails.application.assets + requirejs.env = Rails.application.config.assets + + requirejs.sprockets = Rails.application.assets || ::Sprockets::Railtie.build_environment(Rails.application) # Preserve the original asset paths, as we'll be manipulating them later requirejs.env_paths = requirejs.env.paths.dup @@ -93,27 +95,53 @@ OS X Homebrew users can use 'brew install node'. requirejs.config.source_dir.mkpath logger.info "Preparing source files for r.js in #{requirejs.config.source_dir}" - js_compressor = Sprockets::Rails::Helper.assets.js_compressor - requirejs.env.each_logical_path(requirejs.config.logical_path_patterns) do |logical_path| - m = ::Requirejs::Rails::Config::BOWER_PATH_PATTERN.match(logical_path) - - if !m - logger.info "Preparing #{logical_path}" - Sprockets::Rails::Helper.assets.js_compressor = requirejs.config.asset_precompiled?(logical_path, logical_path) ? js_compressor : false - asset = requirejs.env.find_asset(logical_path) - if asset - file = requirejs.config.source_dir.join(asset.logical_path) - file.dirname.mkpath - asset.write_to(file) - end - else - bower_logical_path = "#{Pathname.new(logical_path).dirname.to_s}.js" - asset = requirejs.env.find_asset(bower_logical_path) - if asset - file = requirejs.config.source_dir.join(bower_logical_path) - file.dirname.mkpath - asset.write_to(file) + if ::Sprockets::VERSION.split(".").first.to_i < 3 + # Sprockets 2.x + requirejs.env.each_logical_path(requirejs.config.logical_path_patterns) do |logical_path| + m = ::Requirejs::Rails::Config::BOWER_PATH_PATTERN.match(logical_path) + + if !m + asset = requirejs.env.find_asset(logical_path) + + if asset + file = requirejs.config.source_dir.join(asset.logical_path) + file.dirname.mkpath + asset.write_to(file) + end + else + bower_logical_path = "#{Pathname.new(logical_path).dirname.to_s}.js" + asset = requirejs.env.find_asset(bower_logical_path) + + if asset + file = requirejs.config.source_dir.join(bower_logical_path) + file.dirname.mkpath + asset.write_to(file) + end + end + end + else + # Sprockets 3.x + requirejs.sprockets.each_file do |file| + begin + asset_uri, deps = requirejs.sprockets.resolve! file + asset = requirejs.sprockets.load asset_uri + asset_logical_path = asset.logical_path + if requirejs.config.logical_path_patterns.any? { |pattern| pattern.match asset_logical_path } + puts "Found logical match: #{asset_logical_path}" + m = ::Requirejs::Rails::Config::BOWER_PATH_PATTERN.match(asset_logical_path) + if !m + target_file = requirejs.config.source_dir.join(asset_logical_path) + puts "Copying js file #{target_file}" + asset.write_to(target_file) + else + raise "Not supported yet" + end + end + rescue + # Ignore if precompiled assets fail. + # This happens for example if we stumble on a scss file that has + # a variable defined elsewhere. end end end @@ -155,12 +183,16 @@ OS X Homebrew users can use 'brew install node'. asset_name = "#{paths[module_name]}.js" end - asset = requirejs.env.find_asset(asset_name) + # Old Sprockets 2 + # asset = requirejs.env.find_asset(asset_name) + asset = requirejs.sprockets.find_asset(asset_name) 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 = requirejs.env.file_digest(built_asset_path.to_s) - hex_digest = file_digest.unpack("H*").first + # Old Sprockets 2 + # file_digest = requirejs.env.file_digest(built_asset_path.to_s) + # hex_digest = file_digest.unpack("H*").first + hex_digest = requirejs.sprockets.pack_hexdigest(requirejs.sprockets.digest_class.digest(built_asset_path.to_s)) digest_name = asset.logical_path.gsub(path_extension_pattern) { |ext| "-#{hex_digest}#{ext}" } digest_asset_path = requirejs.config.target_dir + digest_name