Skip to content

Commit 82829e4

Browse files
Merge pull request #2 from mudbugmedia/move-config-to-yaml
Move config to yaml
2 parents f1def0d + 3ff4328 commit 82829e4

File tree

9 files changed

+103
-27
lines changed

9 files changed

+103
-27
lines changed

.rubocop_todo.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2015-09-19 12:37:55 -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
@@ -9,4 +9,4 @@
99
# Offense count: 2
1010
# Configuration parameters: AllowURI, URISchemes.
1111
Metrics/LineLength:
12-
Max: 91
12+
Max: 86

README.md

+11-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ This gem uses [PhantomJS](https://github.com/colszowka/phantomjs-gem) and [Penth
1414
Add `critical-path-css-rails` to your Gemfile:
1515

1616
```
17-
gem 'critical-path-css-rails', '~> 0.1.0'
17+
gem 'critical-path-css-rails', '~> 0.2.0'
1818
```
1919

2020
Download and install by running:
@@ -23,24 +23,26 @@ Download and install by running:
2323
bundle install
2424
```
2525

26-
Create the rake task that will generate your critical CSS
26+
Run the generator to install the rake task and configuration file:
2727

2828
```
2929
rails generator critical_path_css:install
3030
```
3131

32-
This adds the following file:
32+
The generator adds the following files:
3333

34+
* `config/critical_path_css.yml`
3435
* `lib/tasks/critical_path_css.rake`
3536

3637

3738
## Usage
3839

39-
First, you'll need to configue a few variables in the rake task: `lib/tasks/critical_path_css.rake`
40+
First, you'll need to configue a few things in the YAML file: `config/critical_path_css.yml`
4041

41-
* `@base_url`: Change the url's here to match your Production and Development base URL, respectively.
42-
* `@routes`: List the routes that you would like to generate the critical CSS for. (i.e. /resources, /resources/show/1, etc.)
43-
* `@main_css_path`: Inside of the generate task, you'll need to define the path to the application's main CSS. The gem assumes your CSS lives in `RAILS_ROOT/public`. If your main CSS file is in `RAILS_ROOT/public/assets/main.css`, you would set the variable to `/assets/main.css`.
42+
* `manifest_name`: If you're using the asset pipeline, add the manifest name.
43+
* `css_path`: If you're not using the asset pipeline, you'll need to define the path to the application's main CSS. The gem assumes your CSS lives in `RAILS_ROOT/public`. If your main CSS file is in `RAILS_ROOT/public/assets/main.css`, you would set the variable to `/assets/main.css`.
44+
* `routes`: List the routes that you would like to generate the critical CSS for. (i.e. /resources, /resources/show/1, etc.)
45+
* `base_url`: Add your application's URL for the necessary environments.
4446

4547

4648
Before generating the CSS, ensure that your application is running (viewable from a browser) and the main CSS file exists. Then in a separate tab, run the rake task to generate the critical CSS.
@@ -57,15 +59,15 @@ rake critical_path_css:generate
5759

5860
To load the generated critical CSS into your layout, in the head tag, insert:
5961

60-
```html
62+
```HTML+ERB
6163
<style>
6264
<%= CriticalPathCss.fetch(request.path) %>
6365
</style>
6466
```
6567

6668
A simple example using loadcss-rails looks like:
6769

68-
```html
70+
```HTML+ERB
6971
<style>
7072
<%= CriticalPathCss.fetch(request.path) %>
7173
</style>

lib/config/critical_path_css.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
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
6+
routes:
7+
- /
8+
9+
development:
10+
<<: *defaults
11+
base_url: http://localhost:3000
12+
13+
staging:
14+
<<: *defaults
15+
base_url: http://staging.example.com
16+
17+
production:
18+
<<: *defaults
19+
base_url: http://example.com
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module CriticalPathCss
2+
class Configuration
3+
CONFIGURATION_FILENAME = 'critical_path_css.yml'
4+
5+
def initialize
6+
@configurations = YAML.load_file(configuration_file_path)[Rails.env]
7+
end
8+
9+
def base_url
10+
@configurations['base_url']
11+
end
12+
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']
26+
end
27+
28+
private
29+
30+
def configuration_file_path
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)
36+
end
37+
end
38+
end

lib/critical_path_css/css_fetcher.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module CriticalPathCss
2+
class CssFetcher
3+
require 'phantomjs'
4+
require 'critical_path_css/configuration'
5+
6+
PENTHOUSE_PATH = "#{File.dirname(__FILE__)}/../penthouse/penthouse.js"
7+
8+
def initialize
9+
@config = Configuration.new
10+
end
11+
12+
def fetch
13+
@config.routes.map { |route| [route, css_for_route(route)] }.to_h
14+
end
15+
16+
private
17+
18+
def css_for_route(route)
19+
Phantomjs.run(PENTHOUSE_PATH, @config.base_url + route, @config.css_path)
20+
end
21+
end
22+
end
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module CriticalPathCSS
22
module Rails
3-
VERSION = '0.1.0'
3+
VERSION = '0.2.0'
44
PENTHOUSE_VERSION = '0.3.4'
55
end
66
end

lib/critical_path_css_rails.rb

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
module CriticalPathCss
2-
require 'phantomjs'
2+
require 'critical_path_css/css_fetcher'
33

44
CACHE_NAMESPACE = 'critical-path-css'
5-
PENTHOUSE_PATH = "#{File.dirname(__FILE__)}/penthouse/penthouse.js"
65

7-
def self.generate(main_css_path, base_url, routes)
8-
full_main_css_path = "#{Rails.root}/public#{main_css_path}"
9-
10-
routes.each do |route|
11-
css = Phantomjs.run(PENTHOUSE_PATH, base_url + route, full_main_css_path)
6+
def self.generate
7+
CssFetcher.new.fetch.each do |route, css|
128
Rails.cache.write(route, css, namespace: CACHE_NAMESPACE)
139
end
1410
end

lib/generators/critical_path_css/install_generator.rb

+6
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,11 @@ def copy_rake_task
99
task_filename = 'critical_path_css.rake'
1010
copy_file "../../tasks/#{task_filename}", "lib/tasks/#{task_filename}"
1111
end
12+
13+
# Copy the needed configuration YAML file for generating critical CSS
14+
def copy_config_file
15+
task_filename = 'critical_path_css.yml'
16+
copy_file "../../config/#{task_filename}", "config/#{task_filename}"
17+
end
1218
end
1319
end

lib/tasks/critical_path_css.rake

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
require 'critical_path_css_rails'
22

33
namespace :critical_path_css do
4-
@base_url = Rails.env.production? ? 'http://example.com' : 'http://localhost:3000'
5-
@routes = %w(
6-
/
7-
)
8-
94
desc 'Generate critical CSS for the routes defined'
105
task generate: :environment do
11-
@main_css_path = ActionController::Base.helpers.stylesheet_path('application.css').to_s
12-
13-
CriticalPathCss.generate(@main_css_path, @base_url, @routes)
6+
CriticalPathCss.generate
147
end
158
end

0 commit comments

Comments
 (0)