Skip to content

Commit a52d063

Browse files
Merge pull request #498 from BetterErrors/feature/sass
Use SASS to build CSS
2 parents 698b4c6 + 5625bf8 commit a52d063

File tree

8 files changed

+782
-726
lines changed

8 files changed

+782
-726
lines changed

.github/workflows/release.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,14 @@ jobs:
4848
run: |
4949
bundle exec gem bump better_errors --version ${{ steps.get_version.outputs.version }} --no-commit
5050
51+
- name: Compile CSS
52+
run: |
53+
bundle exec rake style:build
54+
5155
- name: Build gem
5256
run: gem build better_errors.gemspec
5357

54-
- name: Upload gem to Release
58+
- name: Add gem to GitHub Release
5559
uses: actions/upload-release-asset@v1
5660
env:
5761
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@
99

1010
/gemfiles/*.gemfile.lock
1111
/gemfiles/.bundle
12+
13+
14+
# No CSS is committed. In development, the SCSS is used. The CSS is compiled when building a gem release.
15+
*.css

Rakefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require "bundler/gem_tasks"
22
require "rspec/core/rake_task"
3+
require "better_errors/error_page_style"
34

45
RSpec::Core::RakeTask.new(:test)
56
task :default => :test
@@ -36,3 +37,18 @@ namespace :test do
3637
with_each_gemfile { sh "bundle exec rspec" rescue nil }
3738
end
3839
end
40+
41+
namespace :style do
42+
desc "Build main.css from the SASS sources"
43+
task :build do
44+
css = BetterErrors::ErrorPageStyle.compiled_style(true)
45+
File.open(File.expand_path("lib/better_errors/templates/main.css", File.dirname(__FILE__)), "w") do |f|
46+
f.write(css)
47+
end
48+
end
49+
50+
desc "Remove main.css so that the SASS sources will be used directly"
51+
task :develop do
52+
File.unlink File.expand_path("lib/better_errors/templates/main.css", File.dirname(__FILE__))
53+
end
54+
end

better_errors.gemspec

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ Gem::Specification.new do |s|
1212
s.homepage = "https://github.com/BetterErrors/better_errors"
1313
s.license = "MIT"
1414

15-
s.files = `git ls-files -z`.split("\x0").reject do |f|
16-
f.match(%r{^((test|spec|features|feature-screenshots)/|Rakefile)})
17-
end
15+
s.files = `git ls-files -z`.split("\x0").reject { |f|
16+
f.match(%r{^((test|spec|features|feature-screenshots)/|Rakefile)|\.scss$})
17+
} + %w[lib/better_errors/templates/main.css]
1818

1919
s.require_paths = ["lib"]
2020

@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
2525
s.add_development_dependency "rspec-html-matchers"
2626
s.add_development_dependency "rspec-its"
2727
s.add_development_dependency "yard"
28+
s.add_development_dependency "sassc"
2829
# kramdown 2.1 requires Ruby 2.3+
2930
s.add_development_dependency "kramdown", (RUBY_VERSION < '2.3' ? '< 2.0.0' : '> 2.0.0')
3031
# simplecov and coveralls must not be included here. See the Gemfiles instead.

lib/better_errors/error_page.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require "cgi"
22
require "json"
33
require "securerandom"
4+
require "better_errors/error_page_style"
45

56
module BetterErrors
67
# @private
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require "sassc"
2+
3+
module BetterErrors
4+
# @private
5+
module ErrorPageStyle
6+
def self.compiled_style(for_deployment = false)
7+
style_dir = File.expand_path("style", File.dirname(__FILE__))
8+
style_file = "#{style_dir}/main.scss"
9+
10+
engine = SassC::Engine.new(
11+
File.read(style_file),
12+
filename: style_file,
13+
style: for_deployment ? :compressed : :expanded,
14+
line_comments: !for_deployment,
15+
load_paths: [style_dir],
16+
)
17+
engine.render
18+
end
19+
20+
def self.style_tag
21+
style_file = File.expand_path("templates/main.css", File.dirname(__FILE__))
22+
css = if File.exist?(style_file)
23+
File.open(style_file).read
24+
else
25+
compiled_style(false)
26+
end
27+
"<style type='text/css'>\n#{css}\n</style>"
28+
end
29+
end
30+
end

0 commit comments

Comments
 (0)