Created
April 23, 2015 17:03
-
-
Save jrolfs/8bfe66931fe2dd293732 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler' | |
require 'fileutils' | |
require 'sprockets/standalone' | |
require 'rake/hooks' | |
Bundler.require | |
GEM_ASSETS = %w( | |
my-gem-asset | |
my-other-gem-asset | |
my-gem-asset-etc | |
) | |
gem_asset_path = Rails.root.join 'app', 'assets', 'gems' | |
javascript_asset_path = gem_asset_path.join 'javascripts' | |
desc "Convert any gem wrapped assets to standalone files" | |
Sprockets::Standalone::RakeTask.new(:javascript_gems) do |task, sprockets| | |
sprockets.context_class.instance_eval do | |
include ::Sprockets::Helpers::IsolatedHelper | |
include ::Sprockets::Helpers::RailsHelper | |
end | |
task.assets = GEM_ASSETS.map { |asset| javascript_asset_path.join("#{asset}.js.tmp").to_s } | |
task.sources = Rails.application.config.assets.paths.push(javascript_asset_path.to_s) | |
task.output = gem_asset_path | |
end | |
before 'javascript_gems:compile' do | |
FileUtils.remove_dir gem_asset_path, force: true | |
FileUtils.mkdir_p javascript_asset_path | |
GEM_ASSETS.each do |asset| | |
directories = asset.split('/') | |
if directories.length > 1 | |
path = javascript_asset_path.join(*directories) | |
FileUtils.mkdir_p javascript_asset_path.join(*directories[0...-1]) | |
else | |
path = javascript_asset_path.join(asset) | |
end | |
File.open("#{path}.js.tmp", 'w') do |file| | |
file.write("//= require #{asset}") | |
end | |
end | |
end | |
after 'javascript_gems:compile' do | |
FileUtils.rm_f gem_asset_path.join('manifest.json') | |
Dir.glob(javascript_asset_path.join('**/*.js.tmp')) do |file| | |
File.rename file, file.gsub(/\.tmp/, '') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment