Skip to content
This repository
Newer
Older
100644 165 lines (131 sloc) 6.057 kb
bc7073a6 » jashkenas
2009-11-14 getting close to a first release -- commented and lightly refactored,…
1 $LOAD_PATH.push File.expand_path(File.dirname(__FILE__))
a55e9d6e » jashkenas
2009-11-07 bits and snatches of tests
2
17dfaa8e » jashkenas
2009-11-15 serving datauri-embedded-images to IE8, never embedding images larger…
3 # @Jammit@ is the central namespace for all Jammit classes, and provides access
4 # to all of the configuration options.
8f30dbd0 » jashkenas
2009-11-07 initial jammit commit
5 module Jammit
440d3475 » jashkenas
2009-11-11 lots of jammit goodies -- versioning for far-futures expires headers,…
6
8629653f » jashkenas
2010-01-26 Jammit 0.4.1, with combined JS/JST reverted
7 VERSION = "0.4.1"
440d3475 » jashkenas
2009-11-11 lots of jammit goodies -- versioning for far-futures expires headers,…
8
8e264a39 » jashkenas
2009-11-23 first draft of relative URL rewriting
9 ROOT = File.expand_path(File.dirname(__FILE__) + '/..')
440d3475 » jashkenas
2009-11-11 lots of jammit goodies -- versioning for far-futures expires headers,…
10
c1575b30 » jashkenas
2010-01-15 removing all references to RAILS_ROOT in favor of Rails.root
11 ASSET_ROOT = File.expand_path(defined?(Rails) ? Rails.root : ".") unless defined?(ASSET_ROOT)
8e264a39 » jashkenas
2009-11-23 first draft of relative URL rewriting
12
13 PUBLIC_ROOT = "#{ASSET_ROOT}/public"
c9dd3dda » jashkenas
2009-11-18 Jammit 0.2.4
14
15 DEFAULT_CONFIG_PATH = "#{ASSET_ROOT}/config/assets.yml"
440d3475 » jashkenas
2009-11-11 lots of jammit goodies -- versioning for far-futures expires headers,…
16
fd636c43 » jashkenas
2009-11-18 0.2.3, with more robust compressor setting
17 DEFAULT_PACKAGE_PATH = "assets"
14e66b9d » jashkenas
2009-11-15 wrote a first draft of documentation into index -- got the configurat…
18
fd636c43 » jashkenas
2009-11-18 0.2.3, with more robust compressor setting
19 DEFAULT_JST_SCRIPT = "#{ROOT}/lib/jammit/jst.js"
bc7073a6 » jashkenas
2009-11-14 getting close to a first release -- commented and lightly refactored,…
20
fd636c43 » jashkenas
2009-11-18 0.2.3, with more robust compressor setting
21 DEFAULT_JST_COMPILER = "template"
97953ad5 » jashkenas
2010-01-16 merging in agibralter's patch that adds template_namespace as an opti…
22
6ab2f65d » agibralter
2010-01-15 Added the ability to specify an alternative for window.JST in assets.…
23 DEFAULT_JST_NAMESPACE = "window.JST"
bc7073a6 » jashkenas
2009-11-14 getting close to a first release -- commented and lightly refactored,…
24
fd636c43 » jashkenas
2009-11-18 0.2.3, with more robust compressor setting
25 AVAILABLE_COMPRESSORS = [:yui, :closure]
26
27 DEFAULT_COMPRESSOR = :yui
4a82cbb6 » jashkenas
2009-11-17 initial support for Google's Closure Compiler as an option
28
17dfaa8e » jashkenas
2009-11-15 serving datauri-embedded-images to IE8, never embedding images larger…
29 # Jammit raises a @PackageNotFound@ exception when a non-existent package is
30 # requested by a browser -- rendering a 404.
bc7073a6 » jashkenas
2009-11-14 getting close to a first release -- commented and lightly refactored,…
31 class PackageNotFound < NameError; end
440d3475 » jashkenas
2009-11-11 lots of jammit goodies -- versioning for far-futures expires headers,…
32
c9dd3dda » jashkenas
2009-11-18 Jammit 0.2.4
33 # Jammit raises a ConfigurationNotFound exception when you try to load the
34 # configuration of an assets.yml file that doesn't exist.
35 class ConfigurationNotFound < NameError; end
36
6d6d154c » jashkenas
2009-11-29 raising an OutputNotWritable exception when you try to cache packages…
37 # Jammit raises an OutputNotWritable exception if the output directory for
38 # cached packages is locked.
39 class OutputNotWritable < StandardError; end
40
11f0d5ea » jashkenas
2009-11-12 asset versioning, and precompliation through the 'jammit' command
41 class << self
1dda206a » jashkenas
2010-01-27 adding a gzip_assets option to assets.yml
42 attr_reader :configuration, :template_function, :template_namespace,
43 :embed_assets, :package_assets, :compress_assets, :gzip_assets,
44 :package_path, :mhtml_enabled, :include_jst_script,
af21b30a » jashkenas
2010-01-05 Jammit 0.3.3, allowing linebreaks
45 :javascript_compressor, :compressor_options, :css_compressor_options
8f30dbd0 » jashkenas
2009-11-07 initial jammit commit
46 end
440d3475 » jashkenas
2009-11-11 lots of jammit goodies -- versioning for far-futures expires headers,…
47
14e66b9d » jashkenas
2009-11-15 wrote a first draft of documentation into index -- got the configurat…
48 # The minimal required configuration.
49 @configuration = {}
50 @package_path = DEFAULT_PACKAGE_PATH
51
17dfaa8e » jashkenas
2009-11-15 serving datauri-embedded-images to IE8, never embedding images larger…
52 # Load the complete asset configuration from the specified @config_path@.
11f0d5ea » jashkenas
2009-11-12 asset versioning, and precompliation through the 'jammit' command
53 def self.load_configuration(config_path)
0543e419 » jashkenas
2010-01-22 passing assets.yml through ERB before loading it, for environment-spe…
54 exists = config_path && File.exists?(config_path)
55 raise ConfigurationNotFound, "could not find the \"#{config_path}\" configuration file" unless exists
56 conf = YAML.load(ERB.new(File.read(config_path)).result)
4a82cbb6 » jashkenas
2009-11-17 initial support for Google's Closure Compiler as an option
57 @config_path = config_path
c9dd3dda » jashkenas
2009-11-18 Jammit 0.2.4
58 @configuration = conf = conf.symbolize_keys
4a82cbb6 » jashkenas
2009-11-17 initial support for Google's Closure Compiler as an option
59 @package_path = conf[:package_path] || DEFAULT_PACKAGE_PATH
c65ed0de » jashkenas
2010-01-19 renaming embed_images to embed_assets in anticipation of font embedding
60 @embed_assets = conf[:embed_assets] || conf[:embed_images]
e5253563 » jashkenas
2009-12-08 fixing broken configuration test -- making 'compress_assets' a first-…
61 @compress_assets = !(conf[:compress_assets] == false)
1dda206a » jashkenas
2010-01-27 adding a gzip_assets option to assets.yml
62 @gzip_assets = !(conf[:gzip_assets] == false)
c65ed0de » jashkenas
2010-01-19 renaming embed_images to embed_assets in anticipation of font embedding
63 @mhtml_enabled = @embed_assets && @embed_assets != "datauri"
a8fb7d3c » jashkenas
2009-11-19 adding a JavaNotFound exception, and raising it if Java isn't install…
64 @compressor_options = (conf[:compressor_options] || {}).symbolize_keys
af21b30a » jashkenas
2010-01-05 Jammit 0.3.3, allowing linebreaks
65 @css_compressor_options = (conf[:css_compressor_options] || {}).symbolize_keys
fd636c43 » jashkenas
2009-11-18 0.2.3, with more robust compressor setting
66 set_javascript_compressor(conf[:javascript_compressor])
fdf1e641 » jashkenas
2009-11-15 with diagram image and opt-out of the template_function
67 set_package_assets(conf[:package_assets])
68 set_template_function(conf[:template_function])
6ab2f65d » agibralter
2010-01-15 Added the ability to specify an alternative for window.JST in assets.…
69 set_template_namespace(conf[:template_namespace])
a8fb7d3c » jashkenas
2009-11-19 adding a JavaNotFound exception, and raising it if Java isn't install…
70 check_java_version
17dfaa8e » jashkenas
2009-11-15 serving datauri-embedded-images to IE8, never embedding images larger…
71 self
8f30dbd0 » jashkenas
2009-11-07 initial jammit commit
72 end
440d3475 » jashkenas
2009-11-11 lots of jammit goodies -- versioning for far-futures expires headers,…
73
bc7073a6 » jashkenas
2009-11-14 getting close to a first release -- commented and lightly refactored,…
74 # Force a reload by resetting the Packager and reloading the configuration.
29c624f9 » jashkenas
2009-11-17 0.1.3 is going to have a fix for this before_filter issue, one way or…
75 # In development, this will be called as a before_filter before every request.
b577b642 » jashkenas
2009-11-13 fixed image compression for data-uri-supporting browsers plus reloadi…
76 def self.reload!
77 Thread.current[:jammit_packager] = nil
78 load_configuration(@config_path)
79 end
80
17dfaa8e » jashkenas
2009-11-15 serving datauri-embedded-images to IE8, never embedding images larger…
81 # Keep a global (thread-local) reference to a @Jammit::Packager@, to avoid
82 # recomputing asset lists unnecessarily.
11f0d5ea » jashkenas
2009-11-12 asset versioning, and precompliation through the 'jammit' command
83 def self.packager
84 Thread.current[:jammit_packager] ||= Packager.new
440d3475 » jashkenas
2009-11-11 lots of jammit goodies -- versioning for far-futures expires headers,…
85 end
86
17dfaa8e » jashkenas
2009-11-15 serving datauri-embedded-images to IE8, never embedding images larger…
87 # Generate the base filename for a version of a given package.
bc7073a6 » jashkenas
2009-11-14 getting close to a first release -- commented and lightly refactored,…
88 def self.filename(package, extension, suffix=nil)
89 suffix_part = suffix ? "-#{suffix}" : ''
90 "#{package}#{suffix_part}.#{extension}"
8f30dbd0 » jashkenas
2009-11-07 initial jammit commit
91 end
440d3475 » jashkenas
2009-11-11 lots of jammit goodies -- versioning for far-futures expires headers,…
92
17dfaa8e » jashkenas
2009-11-15 serving datauri-embedded-images to IE8, never embedding images larger…
93 # Generates the server-absolute URL to an asset package.
a3932232 » jashkenas
2009-11-14 got relatively bulletproof mhtml generation working
94 def self.asset_url(package, extension, suffix=nil, mtime=nil)
95 timestamp = mtime ? "?#{mtime.to_i}" : ''
14e66b9d » jashkenas
2009-11-15 wrote a first draft of documentation into index -- got the configurat…
96 "/#{package_path}/#{filename(package, extension, suffix)}#{timestamp}"
2de7c26e » jashkenas
2009-11-14 working pre-compiled mhtml
97 end
98
fdf1e641 » jashkenas
2009-11-15 with diagram image and opt-out of the template_function
99
100 private
101
9697b785 » jashkenas
2009-11-19 removing the clunky case statements for setting 'package_assets' and …
102 # Ensure that the JavaScript compressor is a valid choice.
fd636c43 » jashkenas
2009-11-18 0.2.3, with more robust compressor setting
103 def self.set_javascript_compressor(value)
104 value = value && value.to_sym
105 @javascript_compressor = AVAILABLE_COMPRESSORS.include?(value) ? value : DEFAULT_COMPRESSOR
106 end
107
9697b785 » jashkenas
2009-11-19 removing the clunky case statements for setting 'package_assets' and …
108 # Turn asset packaging on or off, depending on configuration and environment.
fdf1e641 » jashkenas
2009-11-15 with diagram image and opt-out of the template_function
109 def self.set_package_assets(value)
18419fe0 » jashkenas
2009-11-29 removing all traces of RAILS_ENV
110 package_env = !defined?(Rails) || !Rails.env.development?
9697b785 » jashkenas
2009-11-19 removing the clunky case statements for setting 'package_assets' and …
111 @package_assets = value == true || value.nil? ? package_env :
112 value == 'always' ? true : false
fdf1e641 » jashkenas
2009-11-15 with diagram image and opt-out of the template_function
113 end
114
9697b785 » jashkenas
2009-11-19 removing the clunky case statements for setting 'package_assets' and …
115 # Assign the JST template function, unless explicitly turned off.
fdf1e641 » jashkenas
2009-11-15 with diagram image and opt-out of the template_function
116 def self.set_template_function(value)
9697b785 » jashkenas
2009-11-19 removing the clunky case statements for setting 'package_assets' and …
117 @template_function = value == true || value.nil? ? DEFAULT_JST_COMPILER :
118 value == false ? '' : value
fdf1e641 » jashkenas
2009-11-15 with diagram image and opt-out of the template_function
119 @include_jst_script = @template_function == DEFAULT_JST_COMPILER
120 end
121
97953ad5 » jashkenas
2010-01-16 merging in agibralter's patch that adds template_namespace as an opti…
122 # Set the root JS object in which to stash all compiled JST.
6ab2f65d » agibralter
2010-01-15 Added the ability to specify an alternative for window.JST in assets.…
123 def self.set_template_namespace(value)
97953ad5 » jashkenas
2010-01-16 merging in agibralter's patch that adds template_namespace as an opti…
124 @template_namespace = value == true || value.nil? ? DEFAULT_JST_NAMESPACE : value.to_s
6ab2f65d » agibralter
2010-01-15 Added the ability to specify an alternative for window.JST in assets.…
125 end
126
a8fb7d3c » jashkenas
2009-11-19 adding a JavaNotFound exception, and raising it if Java isn't install…
127 # The YUI Compressor requires Java > 1.4, and Closure requires Java > 1.6.
128 def self.check_java_version
129 java = @compressor_options[:java] || 'java'
af21b30a » jashkenas
2010-01-05 Jammit 0.3.3, allowing linebreaks
130 @css_compressor_options[:java] ||= java if @compressor_options[:java]
a58a8d49 » jashkenas
2009-12-03 adding a test for broken java configuration
131 version = (`#{java} -version 2>&1`)[/\d+\.\d+/]
c5f98824 » jashkenas
2009-12-03 removed the JavaNotFound exception in favor of running in degraded mo…
132 disable_compression if !version ||
133 (@javascript_compressor == :closure && version < '1.6') ||
134 (@javascript_compressor == :yui && version < '1.4')
135 end
136
137 # If we don't have a working Java VM, then disable asset compression and
138 # complain loudly.
139 def self.disable_compression
e5253563 » jashkenas
2009-12-08 fixing broken configuration test -- making 'compress_assets' a first-…
140 @compress_assets = false
b119b577 » chriseppstein
2010-01-27 Support for legacy rails logging and when no default logger has been …
141 logger.warn("Warning: Jammit asset compression disabled -- Java unavailable.")
142 end
143
144 def self.logger
145 @logger ||= if defined?(Rails)
146 Rails.logger
147 elsif defined?(RAILS_DEFAULT_LOGGER)
148 RAILS_DEFAULT_LOGGER
149 end
150 @logger || std_error_logger
151 end
152
153 def self.std_error_logger
154 @std_error_logger ||= begin
155 err_logger = Object.new
156 def err_logger.warn(message)
157 STDERR.puts(message)
158 end
159 err_logger
160 end
a8fb7d3c » jashkenas
2009-11-19 adding a JavaNotFound exception, and raising it if Java isn't install…
161 end
162
8f30dbd0 » jashkenas
2009-11-07 initial jammit commit
163 end
164
23103391 » jashkenas
2009-11-29 pull out all external dependencies into a standalone file
165 require 'jammit/dependencies'
Something went wrong with that request. Please try again.