From a4038b5fdae2a9cc91b58e745043456486524133 Mon Sep 17 00:00:00 2001 From: schneems Date: Mon, 18 Jan 2016 09:40:08 -0600 Subject: [PATCH] [close #221] Favor charset from processor If a charset is returned by a pre-processor use it instead of automatically assigning one. This is needed so that non-gzippable assets will not accidentally be assigned a charset and gzipped later. We're also removing a hash allocation needed for `Hash.merge!`. --- guides/extending_sprockets.md | 9 +++++++++ lib/sprockets/loader.rb | 9 ++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/guides/extending_sprockets.md b/guides/extending_sprockets.md index d1afae907..ada54c17e 100644 --- a/guides/extending_sprockets.md +++ b/guides/extending_sprockets.md @@ -118,6 +118,15 @@ Sprockets expects an array of hashes for this map. Each hash must have a `:sourc return {data: data, map: [{ source: "original.coffee", # ... }]} ``` +- charset: This key contains the mime charset for an asset + +A charset is the encoding for text based assets. If you do not specify a charset then one will be automatically assigned by sprockets based on the encoding type of the contents returned in the `:data` key. Normally you want that, the only time you don't want that is if you're working with binary data, or data you don't want to be compressed. If sprockets sees a `charset` then it will think that the contents of the file are text and can be compressed via GZIP. You can avoid this by setting the field manually + +```ruby +return { data: data, charset: nil } +``` + + WIP the format of the source map may be subject to change before 4.0 is released. Currently it takes a `:original` and `:generated` key which each hold an array of line and column numbers. Line numbers are 1 indexed column numbers are 0 indexed. The first character of a file will always be `[1,0]`. - WIP - other metadata keys diff --git a/lib/sprockets/loader.rb b/lib/sprockets/loader.rb index 0a641bdb1..1eb2fb028 100644 --- a/lib/sprockets/loader.rb +++ b/lib/sprockets/loader.rb @@ -160,11 +160,10 @@ def load_from_unloaded(unloaded) }) validate_processor_result!(result) source = result.delete(:data) - metadata = result.merge!( - charset: source.encoding.name.downcase, - digest: digest(source), - length: source.bytesize - ) + metadata = result + metadata[:charset] = source.encoding.name.downcase unless metadata.key?(:charset) + metadata[:digest] = digest(source) + metadata[:length] = source.bytesize else dependencies << build_file_digest_uri(unloaded.filename) metadata = {