Skip to content

allow custom assets into the manifest #481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/sprockets/manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ def assets
@data['assets'] ||= {}
end

# Allows to add custom assets which are compiled from
# sources outside the asset pipeline.
#
# Logical path (String): Fingerprinted path (String)
#
# { "app.js" => "app.2e8e97c6.js",
# "vendor.js" => "vendor.ae090855.js" }
def add_assets(custom_assets)
@data['assets'] = assets.merge(custom_assets)
end

# Returns internal file directory listing. Keys are filenames
# which map to an attributes array.
#
Expand Down
26 changes: 26 additions & 0 deletions test/test_manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,32 @@ def teardown
assert data['assets']['application.js']
end

test 'allow custom assets' do
manifest = Sprockets::Manifest.new(@env, File.join(@dir, 'manifest.json'))
custom_assets = {'app.js' => 'app.2e8e97c6.js', 'vendor.js' => 'vendor.ae090855.js'}

digest_path = @env['application.js'].digest_path

assert !File.exist?("#{@dir}/#{digest_path}")
manifest.compile('application.js')
manifest.add_assets(custom_assets)
manifest.save

assert File.directory?(manifest.directory)
assert File.file?(manifest.filename)

assert File.exist?("#{@dir}/manifest.json")
assert File.exist?("#{@dir}/#{digest_path}")

data = JSON.parse(File.read(manifest.filename))
assert data['files'][digest_path]
assert_equal 'application.js', data['files'][digest_path]['logical_path']
assert data['files'][digest_path]['size'] > 230
assert_equal digest_path, data['assets']['application.js']
assert_equal custom_assets['app.js'], data['assets']['app.js']
assert_equal custom_assets['vendor.js'], data['assets']['vendor.js']
end

test "nil environment raises compilation error" do
assert !File.exist?("#{@dir}/manifest.json")

Expand Down