Skip to content

Instantly share code, notes, and snippets.

@juanca
Created June 19, 2016 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanca/4ef6dfab5d589b21898ca5bbe7abd9c0 to your computer and use it in GitHub Desktop.
Save juanca/4ef6dfab5d589b21898ca5bbe7abd9c0 to your computer and use it in GitHub Desktop.
sprockets-loader pre-script
# Script to go through all files in a specified directory (defaults to javascirpts)
# and replace all relative `require`, `require_tree` and `require_directory` paths
# as absolute paths given `app/assets/javascripts` as root
#
# This is useful for using sprockets-loader (or webpack loaders in general because there is no access
# to path related information)
require 'Pathname'
JAVASCRIPT_ROOT = Pathname.new('app/assets/javascripts/')
def add_extension(require_file)
['', '.js', '.js.coffee', '.coffee', '/index.js', '/index.coffee'].each do |extension|
file = "#{require_file}#{extension}"
file_with_extension = Pathname.new("#{JAVASCRIPT_ROOT.to_s}/#{file}")
return file if !file_with_extension.directory? && file_with_extension.exist?
end
puts 'WARNING: Cannot find file (js / coffee / index) -- maybe a gem / vendor file:'
puts "Preserving require directive: #{require_file}", ''
require_file
end
file_names = Dir.glob('app/assets/javascripts/**/*.js')
# file_names = Dir.glob('app/assets/javascripts/**/*.coffee')
file_names.each do |file_name|
dirname = Pathname.new(file_name).dirname
source = File.read(file_name)
lines = source.split("\n") # .each_line / .lines always has trailing newline
new_source = lines.reduce('') do |new_source, line|
new_line = if matches = /(\/\/=)\s*(require)\s+(\S+)/.match(line)
# new_line = if matches = /(#=)\s*(require)\s+(\S+)/.match(line)
new_require_path = if matches[3][0] == '.'
Pathname.new("#{dirname.to_s}/#{matches[3]}").relative_path_from(JAVASCRIPT_ROOT)
else
matches[3]
end
# puts 'require match!', file_name, line, new_require_path, ''
# "#{matches[1]} #{matches[2]} #{matches[3]}" # whitespace fixes only
"#{matches[1]} #{matches[2]} #{add_extension(new_require_path)}"
elsif matches = /(\/\/=)\s*(require_tree)\s+(\S+)/.match(line)
# elsif matches = /(#=)\s*(require_tree)\s+(\S+)/.match(line)
new_require_path = if matches[3][0] == '.'
Pathname.new("#{dirname.to_s}/#{matches[3]}").relative_path_from(JAVASCRIPT_ROOT)
else
matches[3]
end
unless Pathname.new("#{JAVASCRIPT_ROOT}/#{new_require_path.to_s}").directory?
puts 'WARNING: Cannot find directory!:'
puts "Preserving require_tree directive: #{new_require_path}", ''
end
# puts 'require match!', file_name, line, new_require_path, ''
"#{matches[1]} #{matches[2]} #{new_require_path}"
elsif matches = /(\/\/=)\s*(require_directory)\s+(\S+)/.match(line)
# elsif matches = /(#=)\s*(require_directory)\s+(\S+)/.match(line)
new_require_path = if matches[3][0] == '.'
Pathname.new("#{dirname.to_s}/#{matches[3]}").relative_path_from(JAVASCRIPT_ROOT)
else
matches[3]
end
unless Pathname.new("#{JAVASCRIPT_ROOT}/#{new_require_path.to_s}").directory?
puts 'WARNING: Cannot find directory!:'
puts "Preserving require_directory directive: #{new_require_path}", ''
end
# puts 'require match!', file_name, line, new_require_path, ''
"#{matches[1]} #{matches[2]} #{new_require_path}"
else
line
end
new_source += new_line + "\n"
end
# puts new_source
File.open(file_name, "w") {|file| file.puts new_source }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment