turbo-sprockets-rails3 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -6,31 +6,55 @@
6
6
  This is a backport of the work I've done for Rails 4.0.0, released as
7
7
  a gem for Rails 3.2.x. (See [sprockets-rails #21](https://github.com/rails/sprockets-rails/pull/21) and [sprockets #367](https://github.com/sstephenson/sprockets/pull/367) for the Rails 4 pull requests.)
8
8
 
9
+
9
10
  ### Disclaimer
10
11
 
11
12
  Please test this out thoroughly on your local machine before deploying to a production site, and open an issue on GitHub if you have any problems. By using this software you agree to the terms and conditions in the [MIT license](https://github.com/ndbroadbent/turbo-sprockets-rails3/blob/master/MIT-LICENSE).
12
13
 
13
- ### Dependencies
14
+ ## Dependencies
14
15
 
15
16
  * sprockets `~> 2.1.3`
16
17
  * railties `~> 3.2.0`
17
18
 
18
- ### Usage
19
+ ## Usage
19
20
 
20
- Just drop the gem in your `Gemfile`, in the `:assets` group:
21
+ Just drop the gem in your `Gemfile`:
21
22
 
22
23
  ```ruby
23
- group :assets do
24
- ...
25
-
26
- gem 'turbo-sprockets-rails3'
27
- end
24
+ gem 'turbo-sprockets-rails3'
28
25
  ```
29
26
 
30
27
  Run `bundle`, and you're done!
31
28
 
29
+
32
30
  Test it out by running `rake assets:precompile`. When it's finished, your `public/assets/manifest.yml` file should include a `:source_digests` hash for your assets.
33
31
 
34
32
  Go on, run `rake assets:precompile` again, and it should be a whole lot faster than before.
35
33
 
36
34
  Enjoy your lightning fast deploys!
35
+
36
+ #### Automatic clean up of old assets
37
+
38
+ By default, the `assets:precompile` task now deletes out-of-date assets after a new version is compiled. If you don't want to automatically delete old assets, you can turn off this behaviour by setting `config.assets.clean_after_precompile = false` in `config/environments/production.rb`.
39
+
40
+ ## Deployments
41
+
42
+ ### Heroku
43
+
44
+ You won't be able to do an 'incremental update' on heroku, since your `public/assets`
45
+ folder will be empty at the start of each push. However, this gem can still cut your
46
+ precompile time in half, since it only compiles assets once to generate both digest and non-digest assets.
47
+
48
+ ### Capistrano
49
+
50
+ If you are deploying with Capistrano, it's not a good idea to symlink `public/assets` to a shared assets folder, because the asset cleanup task would delete the current assets before the deploy has finished. Keeping a separate copy of `public/assets` for each release also means that you can safely roll back to a previous release.
51
+ The best approach would be to add a deploy step that copies the `public/assets` folder from your previous release into the current release, before running `assets:precompile`.
52
+
53
+ ## Debugging
54
+
55
+ If you would like to view debugging information in your terminal during the `assets:precompile` task, add the following lines to the bottom of `config/environments/production.rb`:
56
+
57
+ ```ruby
58
+ config.log_level = :debug
59
+ config.logger = Logger.new(STDOUT)
60
+ ```
@@ -1,29 +1,31 @@
1
1
  require 'sprockets/asset'
2
2
 
3
- Sprockets::Asset.class_eval do
4
- # Internal initializer to load `Asset` from serialized `Hash`.
5
- def self.from_hash(environment, hash)
6
- return unless hash.is_a?(Hash)
3
+ module Sprockets
4
+ Asset.class_eval do
5
+ # Internal initializer to load `Asset` from serialized `Hash`.
6
+ def self.from_hash(environment, hash)
7
+ return unless hash.is_a?(Hash)
7
8
 
8
- klass = case hash['class']
9
- when 'BundledAsset'
10
- BundledAsset
11
- when 'ProcessedAsset'
12
- ProcessedAsset
13
- when 'UnprocessedAsset'
14
- UnprocessedAsset
15
- when 'StaticAsset'
16
- StaticAsset
17
- else
18
- nil
19
- end
9
+ klass = case hash['class']
10
+ when 'BundledAsset'
11
+ BundledAsset
12
+ when 'ProcessedAsset'
13
+ ProcessedAsset
14
+ when 'UnprocessedAsset'
15
+ UnprocessedAsset
16
+ when 'StaticAsset'
17
+ StaticAsset
18
+ else
19
+ nil
20
+ end
20
21
 
21
- if klass
22
- asset = klass.allocate
23
- asset.init_with(environment, hash)
24
- asset
22
+ if klass
23
+ asset = klass.allocate
24
+ asset.init_with(environment, hash)
25
+ asset
26
+ end
27
+ rescue UnserializeError
28
+ nil
25
29
  end
26
- rescue UnserializeError
27
- nil
28
30
  end
29
31
  end
@@ -1,31 +1,33 @@
1
1
  require 'sprockets/bundled_asset'
2
2
 
3
- Sprockets::BundledAsset.class_eval do
3
+ module Sprockets
4
+ BundledAsset.class_eval do
4
5
 
5
- # Adds :process options
6
+ # Adds :process options
6
7
 
7
- def initialize(environment, logical_path, pathname, options = {})
8
- super(environment, logical_path, pathname)
9
- @process = options.fetch(:process, true)
8
+ def initialize(environment, logical_path, pathname, options = {})
9
+ super(environment, logical_path, pathname)
10
+ @process = options.fetch(:process, true)
10
11
 
11
- @processed_asset = environment.find_asset(pathname, :bundle => false, :process => @process)
12
- @required_assets = @processed_asset.required_assets
13
- @dependency_paths = @processed_asset.dependency_paths
12
+ @processed_asset = environment.find_asset(pathname, :bundle => false, :process => @process)
13
+ @required_assets = @processed_asset.required_assets
14
+ @dependency_paths = @processed_asset.dependency_paths
14
15
 
15
- @source = ""
16
+ @source = ""
16
17
 
17
- # Explode Asset into parts and gather the dependency bodies
18
- to_a.each { |dependency| @source << dependency.to_s }
18
+ # Explode Asset into parts and gather the dependency bodies
19
+ to_a.each { |dependency| @source << dependency.to_s }
19
20
 
20
- if @process
21
- # Run bundle processors on concatenated source
22
- context = environment.context_class.new(environment, logical_path, pathname)
23
- @source = context.evaluate(pathname, :data => @source,
24
- :processors => environment.bundle_processors(content_type))
25
- end
21
+ if @process
22
+ # Run bundle processors on concatenated source
23
+ context = environment.context_class.new(environment, logical_path, pathname)
24
+ @source = context.evaluate(pathname, :data => @source,
25
+ :processors => environment.bundle_processors(content_type))
26
+ end
26
27
 
27
- @mtime = to_a.map(&:mtime).max
28
- @length = Rack::Utils.bytesize(source)
29
- @digest = environment.digest.update(source).hexdigest
28
+ @mtime = to_a.map(&:mtime).max
29
+ @length = Rack::Utils.bytesize(source)
30
+ @digest = environment.digest.update(source).hexdigest
31
+ end
30
32
  end
31
33
  end
@@ -1,19 +1,21 @@
1
1
  require 'sprockets/environment'
2
2
 
3
- Sprockets::Environment.class_eval do
3
+ module Sprockets
4
+ Environment.class_eval do
4
5
 
5
- # Adds :process options
6
+ # Adds :process options
6
7
 
7
- def find_asset(path, options = {})
8
- options[:bundle] = true unless options.key?(:bundle)
9
- options[:process] = true unless options.key?(:process)
8
+ def find_asset(path, options = {})
9
+ options[:bundle] = true unless options.key?(:bundle)
10
+ options[:process] = true unless options.key?(:process)
10
11
 
11
- # Ensure in-memory cached assets are still fresh on every lookup
12
- if (asset = @assets[cache_key_for(path, options)]) && asset.fresh?(self)
13
- asset
14
- elsif asset = index.find_asset(path, options)
15
- # Cache is pushed upstream by Index#find_asset
16
- asset
12
+ # Ensure in-memory cached assets are still fresh on every lookup
13
+ if (asset = @assets[cache_key_for(path, options)]) && asset.fresh?(self)
14
+ asset
15
+ elsif asset = index.find_asset(path, options)
16
+ # Cache is pushed upstream by Index#find_asset
17
+ asset
18
+ end
17
19
  end
18
20
  end
19
21
  end
@@ -1,28 +1,30 @@
1
1
  require 'sprockets/index'
2
2
 
3
- Sprockets::Index.class_eval do
3
+ module Sprockets
4
+ Index.class_eval do
4
5
 
5
- # Adds :process options
6
+ # Adds :process options
6
7
 
7
- def find_asset(path, options = {})
8
- options[:bundle] = true unless options.key?(:bundle)
9
- options[:process] = true unless options.key?(:process)
8
+ def find_asset(path, options = {})
9
+ options[:bundle] = true unless options.key?(:bundle)
10
+ options[:process] = true unless options.key?(:process)
10
11
 
11
- if asset = @assets[cache_key_for(path, options)]
12
- asset
13
- elsif asset = super
14
- logical_path_cache_key = cache_key_for(path, options)
15
- full_path_cache_key = cache_key_for(asset.pathname, options)
12
+ if asset = @assets[cache_key_for(path, options)]
13
+ asset
14
+ elsif asset = super
15
+ logical_path_cache_key = cache_key_for(path, options)
16
+ full_path_cache_key = cache_key_for(asset.pathname, options)
16
17
 
17
- # Cache on Index
18
- @assets[logical_path_cache_key] = @assets[full_path_cache_key] = asset
19
-
20
- # Push cache upstream to Environment
21
- @environment.instance_eval do
18
+ # Cache on Index
22
19
  @assets[logical_path_cache_key] = @assets[full_path_cache_key] = asset
23
- end
24
20
 
25
- asset
21
+ # Push cache upstream to Environment
22
+ @environment.instance_eval do
23
+ @assets[logical_path_cache_key] = @assets[full_path_cache_key] = asset
24
+ end
25
+
26
+ asset
27
+ end
26
28
  end
27
29
  end
28
30
  end
@@ -1,68 +1,70 @@
1
1
  require 'sprockets/static_compiler'
2
2
 
3
- Sprockets::StaticCompiler.class_eval do
4
- def initialize(env, target, paths, options = {})
5
- @env = env
6
- @target = target
7
- @paths = paths
8
- @digest = options.fetch(:digest, true)
9
- @manifest = options.fetch(:manifest, true)
10
- @manifest_path = options.delete(:manifest_path) || target
11
- @zip_files = options.delete(:zip_files) || /\.(?:css|html|js|svg|txt|xml)$/
3
+ module Sprockets
4
+ StaticCompiler.class_eval do
5
+ def initialize(env, target, paths, options = {})
6
+ @env = env
7
+ @target = target
8
+ @paths = paths
9
+ @digest = options.fetch(:digest, true)
10
+ @manifest = options.fetch(:manifest, true)
11
+ @manifest_path = options.delete(:manifest_path) || target
12
+ @zip_files = options.delete(:zip_files) || /\.(?:css|html|js|svg|txt|xml)$/
12
13
 
13
- @current_source_digests = options.fetch(:source_digests, {})
14
- @current_digest_files = options.fetch(:digest_files, {})
14
+ @current_source_digests = options.fetch(:source_digests, {})
15
+ @current_digest_files = options.fetch(:digest_files, {})
15
16
 
16
- @digest_files = {}
17
- @source_digests = {}
18
- end
17
+ @digest_files = {}
18
+ @source_digests = {}
19
+ end
19
20
 
20
- def compile
21
- start_time = Time.now.to_f
21
+ def compile
22
+ start_time = Time.now.to_f
22
23
 
23
- env.each_logical_path do |logical_path|
24
- if File.basename(logical_path)[/[^\.]+/, 0] == 'index'
25
- logical_path.sub!(/\/index\./, '.')
26
- end
27
- next unless compile_path?(logical_path)
24
+ env.each_logical_path do |logical_path|
25
+ if File.basename(logical_path)[/[^\.]+/, 0] == 'index'
26
+ logical_path.sub!(/\/index\./, '.')
27
+ end
28
+ next unless compile_path?(logical_path)
28
29
 
29
- # Fetch asset without any processing or compression,
30
- # to calculate a digest of the concatenated source files
31
- asset = env.find_asset(logical_path, :process => false)
30
+ # Fetch asset without any processing or compression,
31
+ # to calculate a digest of the concatenated source files
32
+ asset = env.find_asset(logical_path, :process => false)
32
33
 
33
- # Force digest to UTF-8, otherwise YAML dumps ASCII-8BIT as !binary
34
- @source_digests[logical_path] = asset.digest.force_encoding("UTF-8")
34
+ # Force digest to UTF-8, otherwise YAML dumps ASCII-8BIT as !binary
35
+ @source_digests[logical_path] = asset.digest.force_encoding("UTF-8")
35
36
 
36
- # Recompile if digest has changed or compiled digest file is missing
37
- current_digest_file = @current_digest_files[logical_path]
37
+ # Recompile if digest has changed or compiled digest file is missing
38
+ current_digest_file = @current_digest_files[logical_path]
38
39
 
39
- if @source_digests[logical_path] != @current_source_digests[logical_path] ||
40
- !(current_digest_file && File.exists?("#{@target}/#{current_digest_file}"))
40
+ if @source_digests[logical_path] != @current_source_digests[logical_path] ||
41
+ !(current_digest_file && File.exists?("#{@target}/#{current_digest_file}"))
41
42
 
42
- if asset = env.find_asset(logical_path)
43
- @digest_files[logical_path] = write_asset(asset)
44
- end
43
+ if asset = env.find_asset(logical_path)
44
+ @digest_files[logical_path] = write_asset(asset)
45
+ end
45
46
 
46
- else
47
- # Set asset file from manifest.yml
48
- digest_file = @current_digest_files[logical_path]
49
- @digest_files[logical_path] = digest_file
47
+ else
48
+ # Set asset file from manifest.yml
49
+ digest_file = @current_digest_files[logical_path]
50
+ @digest_files[logical_path] = digest_file
50
51
 
51
- env.logger.debug "Not compiling #{logical_path}, sources digest has not changed " <<
52
- "(#{@source_digests[logical_path][0...7]})"
52
+ env.logger.debug "Not compiling #{logical_path}, sources digest has not changed " <<
53
+ "(#{@source_digests[logical_path][0...7]})"
54
+ end
53
55
  end
54
- end
55
56
 
56
- if @manifest
57
- write_manifest(source_digests: @source_digests, digest_files: @digest_files)
58
- end
57
+ if @manifest
58
+ write_manifest(source_digests: @source_digests, digest_files: @digest_files)
59
+ end
59
60
 
60
- # Store digests in Rails config. (Important if non-digest is run after primary)
61
- config = ::Rails.application.config
62
- config.assets.digest_files = @digest_files
63
- config.assets.source_digests = @source_digests
61
+ # Store digests in Rails config. (Important if non-digest is run after primary)
62
+ config = ::Rails.application.config
63
+ config.assets.digest_files = @digest_files
64
+ config.assets.source_digests = @source_digests
64
65
 
65
- elapsed_time = ((Time.now.to_f - start_time) * 1000).to_i
66
- env.logger.debug "Processed #{'non-' unless @digest}digest assets in #{elapsed_time}ms"
66
+ elapsed_time = ((Time.now.to_f - start_time) * 1000).to_i
67
+ env.logger.debug "Processed #{'non-' unless @digest}digest assets in #{elapsed_time}ms"
68
+ end
67
69
  end
68
- end
70
+ end
@@ -1,3 +1,3 @@
1
1
  module TurboSprockets
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turbo-sprockets-rails3
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: