-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from mudbugmedia/move-config-to-yaml
Move config to yaml
- Loading branch information
Showing
9 changed files
with
103 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defaults: &defaults | ||
# If using the asset pipeline, provide the manifest name | ||
manifest_name: application | ||
# Else provide the relative path of your CSS file from the /public directory | ||
# css_path: /path/to/css/from/public/main.css | ||
routes: | ||
- / | ||
|
||
development: | ||
<<: *defaults | ||
base_url: http://localhost:3000 | ||
|
||
staging: | ||
<<: *defaults | ||
base_url: http://staging.example.com | ||
|
||
production: | ||
<<: *defaults | ||
base_url: http://example.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module CriticalPathCss | ||
class Configuration | ||
CONFIGURATION_FILENAME = 'critical_path_css.yml' | ||
|
||
def initialize | ||
@configurations = YAML.load_file(configuration_file_path)[Rails.env] | ||
end | ||
|
||
def base_url | ||
@configurations['base_url'] | ||
end | ||
|
||
def css_path | ||
@css_path ||= begin | ||
relative_path = @configurations['css_path'] || manifest_path | ||
"#{Rails.root}/public#{relative_path}" | ||
end | ||
end | ||
|
||
def manifest_name | ||
@configurations['manifest_name'] | ||
end | ||
|
||
def routes | ||
@configurations['routes'] | ||
end | ||
|
||
private | ||
|
||
def configuration_file_path | ||
@configuration_file_path ||= Rails.root.join('config', CONFIGURATION_FILENAME) | ||
end | ||
|
||
def manifest_path | ||
@manifest_path ||= ActionController::Base.helpers.stylesheet_path(manifest_name) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module CriticalPathCss | ||
class CssFetcher | ||
require 'phantomjs' | ||
require 'critical_path_css/configuration' | ||
|
||
PENTHOUSE_PATH = "#{File.dirname(__FILE__)}/../penthouse/penthouse.js" | ||
|
||
def initialize | ||
@config = Configuration.new | ||
end | ||
|
||
def fetch | ||
@config.routes.map { |route| [route, css_for_route(route)] }.to_h | ||
end | ||
|
||
private | ||
|
||
def css_for_route(route) | ||
Phantomjs.run(PENTHOUSE_PATH, @config.base_url + route, @config.css_path) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module CriticalPathCSS | ||
module Rails | ||
VERSION = '0.1.0' | ||
VERSION = '0.2.0' | ||
PENTHOUSE_VERSION = '0.3.4' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,8 @@ | ||
require 'critical_path_css_rails' | ||
|
||
namespace :critical_path_css do | ||
@base_url = Rails.env.production? ? 'http://example.com' : 'http://localhost:3000' | ||
@routes = %w( | ||
/ | ||
) | ||
|
||
desc 'Generate critical CSS for the routes defined' | ||
task generate: :environment do | ||
@main_css_path = ActionController::Base.helpers.stylesheet_path('application.css').to_s | ||
|
||
CriticalPathCss.generate(@main_css_path, @base_url, @routes) | ||
CriticalPathCss.generate | ||
end | ||
end |