Skip to content

Instantly share code, notes, and snippets.

@sairam
Created May 20, 2017 13:54
Show Gist options
  • Save sairam/f15d66952a61db75a0d613729cd60498 to your computer and use it in GitHub Desktop.
Save sairam/f15d66952a61db75a0d613729cd60498 to your computer and use it in GitHub Desktop.
Merge custom assets with webpack assets
namespace :assets do
task add_custom_assets: :environment do
source_manifest_file = "#{ActionView::Base.assets_manifest.dir}/custom-manifest.json"
unless File.exists?(source_manifest_file)
puts "source manifest file not found at #{source_manifest_file}"
else
# make sure the file exists and has content. better to use the below approach
# rails_manifest_file = Sprockets::ManifestUtils.find_directory_manifest(ActionView::Base.assets_manifest.dir)
# assets = JSON.parse(open(rails_manifest_file).read)
# new_assets = open(source_manifest_file).read
# assets['assets'].merge!(JSON.parse(new_assets))
# manifest = open(rails_manifest_file, 'w+')
# manifest.write(assets)
# manifest.save
# TODO - use only after https://github.com/rails/sprockets/pull/481/files is merged and released.
manifest = Sprockets::Manifest.new(ActionView::Base.assets_manifest.environment, ActionView::Base.assets_manifest.dir)
new_assets = open(source_manifest_file).read
manifest.add_assets(JSON.parse(new_assets))
manifest.save
puts "merged assets with rails assets"
end
end
end
if defined?(AssetSync) && AssetSync.config.run_on_precompile == true
Rake::Task['assets:sync'].enhance ['assets:add_custom_assets']
else
Rake::Task['assets:precompile'].enhance do
Rake::Task['assets:add_webpack'].invoke
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment