lib/roda/plugins/assets.rb in roda-2.29.0 vs lib/roda/plugins/assets.rb in roda-3.0.0
- old
+ new
@@ -22,11 +22,11 @@
# == Usage
#
# When loading the plugin, use the :css and :js options
# to set the source file(s) to use for CSS and javascript assets:
#
- # plugin :assets, :css => 'some_file.scss', :js => 'some_file.coffee'
+ # plugin :assets, css: 'some_file.scss', js: 'some_file.coffee'
#
# This will look for the following files:
#
# assets/css/some_file.scss
# assets/js/some_file.coffee
@@ -55,14 +55,14 @@
# <%= assets(:css) %>
# <%= assets(:js) %>
#
# You can add attributes to the tags by using an options hash:
#
- # <%= assets(:css, :media => 'print') %>
+ # <%= assets(:css, media: 'print') %>
#
- # The assets method will respect the application's :add_script_name option,
- # if it set it will automatically prefix the path with the SCRIPT_NAME for
+ # The assets method will respect the application's +:add_script_name+ option,
+ # if it set it will automatically prefix the path with the +SCRIPT_NAME+ for
# the request.
#
# == Asset Paths
#
# If you just want the paths rather than the full tags, you can use
@@ -82,20 +82,20 @@
#
# The asset plugin supports groups for the cases where you have different
# css/js files for your front end and back end. To use asset groups, you
# pass a hash for the :css and/or :js options:
#
- # plugin :assets, :css => {:frontend => 'some_frontend_file.scss',
- # :backend => 'some_backend_file.scss'}
+ # plugin :assets, css: {frontend: 'some_frontend_file.scss',
+ # backend: 'some_backend_file.scss'}
#
# This expects the following directory structure for your assets:
#
# assets/css/frontend/some_frontend_file.scss
# assets/css/backend/some_backend_file.scss
#
# If you do not want to force that directory structure when using
- # asset groups, you can use the <tt>:group_subdirs => false</tt> option.
+ # asset groups, you can use the <tt>group_subdirs: false</tt> option.
#
# In your view code use an array argument in your call to assets:
#
# <%= assets([:css, :frontend]) %>
#
@@ -104,11 +104,11 @@
# Asset groups also support nesting, though that should only be needed
# in fairly large applications. You can use a nested hash when loading
# the plugin:
#
# plugin :assets,
- # :css => {:frontend => {:dashboard => 'some_frontend_file.scss'}}
+ # css: {frontend: {dashboard: 'some_frontend_file.scss'}}
#
# and an extra entry per nesting level when creating the tags.
#
# <%= assets([:css, :frontend, :dashboard]) %>
#
@@ -124,19 +124,19 @@
# using the :dependencies option to the plugin, which takes a hash where
# the keys are paths to asset files, and values are arrays of paths to
# dependencies of those asset files:
#
# app.plugin :assets,
- # :dependencies=>{'assets/css/bootstrap.scss'=>Dir['assets/css/bootstrap/' '**/*.scss']}
+ # dependencies: {'assets/css/bootstrap.scss'=>Dir['assets/css/bootstrap/' '**/*.scss']}
#
# == Asset Compilation
#
# In production, you are generally going to want to compile your assets
# into a single file, with you can do by calling compile_assets after
# loading the plugin:
#
- # plugin :assets, :css => 'some_file.scss', :js => 'some_file.coffee'
+ # plugin :assets, css: 'some_file.scss', js: 'some_file.coffee'
# compile_assets
#
# After calling compile_assets, calls to assets in your views will default
# to a using a single link each to your CSS and javascript compiled asset
# files. By default the compiled files are written to the public directory,
@@ -222,12 +222,12 @@
# It should return the new content for the asset.
#
# You can use this to call Autoprefixer on your CSS:
#
# plugin :assets, {
- # :css => [ 'style.scss' ],
- # :postprocessor => lambda do |file, type, content|
+ # css: [ 'style.scss' ],
+ # postprocessor: lambda do |file, type, content|
# type == :css ? AutoprefixerRails.process(content).css : content
# end
# }
#
# == External Assets/Assets from Gems
@@ -290,11 +290,12 @@
# non-compiled mode, but will write the metadata to the file if compile_assets is called.
# :public :: Path to your public folder, in which compiled files are placed (default: 'public'). Relative
# paths will be considered relative to the application's :root option.
# :sri :: Enables subresource integrity when setting up references to compiled assets. The value should be
# :sha256, :sha384, or :sha512 depending on which hash algorithm you want to use. This changes the
- # hash algorithm that Roda will use when naming compiled asset files.
+ # hash algorithm that Roda will use when naming compiled asset files. The default is :sha256, you
+ # can use nil to disable subresource integrity.
module Assets
DEFAULTS = {
:compiled_name => 'app'.freeze,
:js_dir => 'js'.freeze,
:css_dir => 'css'.freeze,
@@ -303,41 +304,13 @@
:compiled => false,
:add_suffix => false,
:group_subdirs => true,
:compiled_css_dir => nil,
:compiled_js_dir => nil,
+ :sri => :sha256
}.freeze
- EMPTY_ATTRS = {}.freeze
- RodaPlugins.deprecate_constant(self, :EMPTY_ATTRS)
- JS_END = "\"></script>".freeze
- RodaPlugins.deprecate_constant(self, :JS_END)
- CSS_END = "\" />".freeze
- RodaPlugins.deprecate_constant(self, :CSS_END)
- SPACE = ' '.freeze
- RodaPlugins.deprecate_constant(self, :SPACE)
- DOT = '.'.freeze
- RodaPlugins.deprecate_constant(self, :DOT)
- SLASH = '/'.freeze
- RodaPlugins.deprecate_constant(self, :SLASH)
- NEWLINE = "\n".freeze
- RodaPlugins.deprecate_constant(self, :NEWLINE)
- EMPTY_STRING = ''.freeze
- RodaPlugins.deprecate_constant(self, :EMPTY_STRING)
- JS_SUFFIX = '.js'.freeze
- RodaPlugins.deprecate_constant(self, :JS_SUFFIX)
- CSS_SUFFIX = '.css'.freeze
- RodaPlugins.deprecate_constant(self, :CSS_SUFFIX)
- HTTP_ACCEPT_ENCODING = 'HTTP_ACCEPT_ENCODING'.freeze
- RodaPlugins.deprecate_constant(self, :HTTP_ACCEPT_ENCODING)
- CONTENT_ENCODING = 'Content-Encoding'.freeze
- RodaPlugins.deprecate_constant(self, :CONTENT_ENCODING)
- GZIP = 'gzip'.freeze
- RodaPlugins.deprecate_constant(self, :GZIP)
- DOTGZ = '.gz'.freeze
- RodaPlugins.deprecate_constant(self, :DOTGZ)
-
# Internal exception raised when a compressor cannot be found
CompressorNotFound = Class.new(RodaError)
# Load the render, caching, and h plugins, since the assets plugin
# depends on them.
@@ -525,21 +498,20 @@
end
nil
end
- # Compress the given content for the given type using yuicompressor,
- # but handle cases where yuicompressor isn't installed or can't find
- # a java runtime. This method can be overridden by the application
- # to use a different compressor.
+ # Compress the given content for the given type by using the
+ # configured compressor, or trying the supported compressors.
def compress_asset(content, type)
case compressor = assets_opts[:"#{type}_compressor"]
when :none
return content
when nil
# default, try different compressors
else
+ # Allow calling private compress methods
return send("compress_#{type}_#{compressor}", content)
end
compressors = if type == :js
[:yui, :closure, :uglifier, :minjs]
@@ -547,10 +519,11 @@
[:yui]
end
compressors.each do |comp|
begin
+ # Allow calling private compress methods
if c = send("compress_#{type}_#{comp}", content)
return c
end
rescue LoadError, CompressorNotFound
end
@@ -593,29 +566,23 @@
end
# Compress the CSS/JS using YUI Compressor, requires java runtime
def compress_yui(content, meth)
require 'yuicompressor'
- ::YUICompressor.send(meth, content, :munge => true)
+ ::YUICompressor.public_send(meth, content, :munge => true)
rescue ::Errno::ENOENT => e
raise CompressorNotFound, "#{e.class}: #{e.message}", e.backtrace
end
# Return a unique id for the given content. By default, uses the
- # SHA1 hash of the content. This method can be overridden to use
+ # SHA256 hash of the content. This method can be overridden to use
# a different digest type or to return a static string if you don't
# want to use a unique value.
def asset_digest(content)
- klass = if algo = assets_opts[:sri]
- require 'digest/sha2'
- ::Digest.const_get(algo.to_s.upcase)
- else
- require 'digest/sha1'
- ::Digest::SHA1
- end
-
- klass.hexdigest(content)
+ require 'digest/sha2'
+ algo = assets_opts[:sri] || :sha256
+ ::Digest.const_get(algo.to_s.upcase).hexdigest(content)
end
end
module InstanceMethods
# Return an array of paths for the given asset type and optionally
@@ -680,10 +647,10 @@
end
# Render the asset with the given filename. When assets are compiled,
# or when the file is already of the given type (no rendering necessary),
# this returns the contents of the compiled file.
- # When assets are not compiled and the file is not already of the correct,
+ # When assets are not compiled and the file is not already in the same format,
# this will render the asset using the render plugin.
# In both cases, if the file has not been modified since the last request,
# this will return a 304 response.
def render_asset(file, type)
o = self.class.assets_opts