Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Change register_processor to take a mime_type instead of a extension

  • Loading branch information...
commit 1724d0618a66505a4e8b51a4f9bbc2d0cd392851 1 parent ae31388
@josh josh authored
View
10 lib/sprockets/asset_pathname.rb
@@ -46,16 +46,6 @@ def engine_content_type
nil
end
- if {}.respond_to?(:key)
- def engine_format_extension
- @environment.mime_types.key(engine_content_type)
- end
- else
- def engine_format_extension
- @environment.mime_types.index(engine_content_type)
- end
- end
-
def content_type
@content_type ||= begin
if format_extension.nil?
View
4 lib/sprockets/context.rb
@@ -69,11 +69,9 @@ def depend_on(path)
def evaluate(filename, options = {})
pathname = resolve(filename)
asset_pathname = AssetPathname.new(pathname, environment)
- extension = asset_pathname.format_extension ||
- asset_pathname.engine_format_extension
data = options[:data] || pathname.read
- engines = options[:engines] || environment.processors(extension) +
+ engines = options[:engines] || environment.processors(content_type) +
asset_pathname.engines.reverse
engines.inject(data) do |result, engine|
View
10 lib/sprockets/directive_processor.rb
@@ -29,12 +29,12 @@ module Sprockets
#
# To remove the processor entirely:
#
- # env.unregister_processor('.css', Sprockets::DirectiveProcessor)
- # env.unregister_processor('.js', Sprockets::DirectiveProcessor)
+ # env.unregister_processor('text/css', Sprockets::DirectiveProcessor)
+ # env.unregister_processor('application/css', Sprockets::DirectiveProcessor)
#
# Then inject your own preprocessor:
#
- # env.register_processor('.css', MyProcessor)
+ # env.register_processor('text/css', MyProcessor)
#
class DirectiveProcessor < Tilt::Template
attr_reader :pathname
@@ -184,8 +184,8 @@ def extract_directive(line)
#
# Replace the current processor on the environment with your own:
#
- # env.unregister_processor('.css', Sprockets::DirectiveProcessor)
- # env.register_processor('.css', DirectiveProcessor)
+ # env.unregister_processor('test/css', Sprockets::DirectiveProcessor)
+ # env.register_processor('text/css', DirectiveProcessor)
#
def process_directives
directives.each do |name, *args|
View
6 lib/sprockets/environment.rb
@@ -31,9 +31,6 @@ def initialize(root = ".")
register_mime_type 'text/css', '.css'
register_mime_type 'application/javascript', '.js'
- register_processor '.css', DirectiveProcessor
- register_processor '.js', DirectiveProcessor
-
register_engine '.jst', JstProcessor
register_engine '.ejs', EjsTemplate
@@ -44,6 +41,9 @@ def initialize(root = ".")
register_engine '.less', Tilt::LessTemplate
register_engine '.coffee', CoffeeScriptTemplate
+ register_processor 'text/css', DirectiveProcessor
+ register_processor 'application/javascript', DirectiveProcessor
+
register_bundle_processor 'text/css', CharsetNormalizer
expire_index!
View
4 lib/sprockets/environment_index.rb
@@ -106,10 +106,8 @@ def build_asset(logical_path, pathname, options)
pathname = Pathname.new(pathname)
asset_pathname = AssetPathname.new(pathname, self)
- extension = asset_pathname.format_extension ||
- asset_pathname.engine_format_extension
- if processors(extension).any?
+ if processors(asset_pathname.content_type).any?
logger.info "[Sprockets] #{logical_path} building"
asset = BundledAsset.new(self, logical_path, pathname, options)
else
View
42 lib/sprockets/processing.rb
@@ -20,48 +20,46 @@ def register_mime_type(mime_type, ext)
@mime_types[ext] = mime_type
end
- def processors(ext = nil)
+ def engines(ext = nil)
if ext
- @processors[normalize_extension(ext)].dup
+ ext = normalize_extension(ext)
+ @engines[ext] || Tilt[ext]
else
- deep_copy_hash(@processors)
+ @engines.dup
end
end
+ def engine_extensions
+ @engines.keys
+ end
+
def format_extensions
- @processors.keys
+ @trail.extensions - @engines.keys
end
- def register_processor(ext, klass)
+ def register_engine(ext, klass)
expire_index!
ext = normalize_extension(ext)
@trail.extensions << ext
- @processors[ext].push(klass)
- end
-
- def unregister_processor(ext, klass)
- expire_index!
- @processors[normalize_extension(ext)].delete(klass)
+ @engines[ext] = klass
end
- def engines(ext = nil)
- if ext
- ext = normalize_extension(ext)
- @engines[ext] || Tilt[ext]
+ def processors(mime_type = nil)
+ if mime_type
+ @processors[mime_type].dup
else
- @engines.dup
+ deep_copy_hash(@processors)
end
end
- def engine_extensions
- @engines.keys
+ def register_processor(mime_type, klass)
+ expire_index!
+ @processors[mime_type].push(klass)
end
- def register_engine(ext, klass)
+ def unregister_processor(mime_type, klass)
expire_index!
- ext = normalize_extension(ext)
- @trail.extensions << ext
- @engines[ext] = klass
+ @processors[mime_type].delete(klass)
end
def bundle_processors(mime_type = nil)
View
4 test/test_directive_processor.rb
@@ -111,8 +111,8 @@ def process_require_glob_directive(glob)
end
test "custom processor using Context#sprockets_resolve and Context#sprockets_depend" do
- @env.unregister_processor('.js', Sprockets::DirectiveProcessor)
- @env.register_processor('.js', TestDirectiveProcessor)
+ @env.unregister_processor('application/javascript', Sprockets::DirectiveProcessor)
+ @env.register_processor('application/javascript', TestDirectiveProcessor)
assert_equal "var Foo = {};\n\n", @env["require_glob.js"].to_s
end
View
2  test/test_environment.rb
@@ -403,7 +403,7 @@ def foo; end
end
test "disabling default directive preprocessor" do
- @env.unregister_processor('.js', Sprockets::DirectiveProcessor)
+ @env.unregister_processor('application/javascript', Sprockets::DirectiveProcessor)
assert_equal "// =require \"notfound\"\n", @env["missing_require.js"].to_s
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.