Skip to content

Commit 1ae333a

Browse files
Move CSS file path into the YAML config as well
1 parent d3a07c5 commit 1ae333a

File tree

6 files changed

+32
-16
lines changed

6 files changed

+32
-16
lines changed

.rubocop_todo.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2015-10-03 13:02:27 -0500 using RuboCop version 0.34.1.
3+
# on 2015-10-03 13:56:01 -0500 using RuboCop version 0.34.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 1
9+
# Offense count: 2
1010
# Configuration parameters: AllowURI, URISchemes.
1111
Metrics/LineLength:
12-
Max: 90
12+
Max: 86

lib/config/critical_path_css.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
defaults: &defaults
2+
# If using the asset pipeline, provide the manifest name
3+
manifest_name: application
4+
# Else provide the relative path of your CSS file from the /public directory
5+
# css_path: /path/to/css/from/public/main.css
26
routes:
37
- /
48

lib/critical_path_css/configuration.rb

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,33 @@ def initialize
66
@configurations = YAML.load_file(configuration_file_path)[Rails.env]
77
end
88

9-
def routes
10-
@routes ||= @configurations['routes']
9+
def base_url
10+
@configurations['base_url']
1111
end
1212

13-
def base_url
14-
@base_url ||= @configurations['base_url']
13+
def css_path
14+
@css_path ||= begin
15+
relative_path = @configurations['css_path'] || manifest_path
16+
"#{Rails.root}/public#{relative_path}"
17+
end
18+
end
19+
20+
def manifest_name
21+
@configurations['manifest_name']
22+
end
23+
24+
def routes
25+
@configurations['routes']
1526
end
1627

1728
private
1829

1930
def configuration_file_path
20-
Rails.root.join('config', CONFIGURATION_FILENAME)
31+
@configuration_file_path ||= Rails.root.join('config', CONFIGURATION_FILENAME)
32+
end
33+
34+
def manifest_path
35+
@manifest_path ||= ActionController::Base.helpers.stylesheet_path(manifest_name)
2136
end
2237
end
2338
end

lib/critical_path_css/css_fetcher.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ class CssFetcher
55

66
PENTHOUSE_PATH = "#{File.dirname(__FILE__)}/../penthouse/penthouse.js"
77

8-
def initialize(main_css_relative_path)
9-
@main_css_path = "#{Rails.root}/public#{main_css_relative_path}"
8+
def initialize
109
@config = Configuration.new
1110
end
1211

@@ -17,7 +16,7 @@ def fetch
1716
private
1817

1918
def css_for_route(route)
20-
Phantomjs.run(PENTHOUSE_PATH, @config.base_url + route, @main_css_path)
19+
Phantomjs.run(PENTHOUSE_PATH, @config.base_url + route, @config.css_path)
2120
end
2221
end
2322
end

lib/critical_path_css_rails.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module CriticalPathCss
33

44
CACHE_NAMESPACE = 'critical-path-css'
55

6-
def self.generate(main_css_relative_path)
7-
CssFetcher.new(main_css_relative_path).fetch.each do |route, css|
6+
def self.generate
7+
CssFetcher.new.fetch.each do |route, css|
88
Rails.cache.write(route, css, namespace: CACHE_NAMESPACE)
99
end
1010
end

lib/tasks/critical_path_css.rake

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ require 'critical_path_css_rails'
33
namespace :critical_path_css do
44
desc 'Generate critical CSS for the routes defined'
55
task generate: :environment do
6-
main_css_path = ActionController::Base.helpers.stylesheet_path('application.css').to_s
7-
8-
CriticalPathCss.generate(main_css_path)
6+
CriticalPathCss.generate
97
end
108
end

0 commit comments

Comments
 (0)