Skip to content

Commit 2a27f2b

Browse files
Various improvements (#30)
Various improvements: - Require latest sassc-embedded. - Allow pass-thru of all sass-embedded options. - Remove config.sass.line_comments. - Add Ruby 3.4 to Github build. - Version bump. - Remove Sprockets 2.x related code. - Fix broken tests.
1 parent dc8e3b2 commit 2a27f2b

14 files changed

+219
-164
lines changed

.github/workflows/build.yml

+20
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,38 @@ jobs:
3030
- '3.1'
3131
- '3.2'
3232
- '3.3'
33+
- '3.4'
3334
- ruby
3435
- jruby
3536
- truffleruby
3637
gemfile:
3738
- gemfiles/rails_6_1.gemfile
3839
- gemfiles/rails_7_0.gemfile
3940
- gemfiles/rails_7_1.gemfile
41+
- gemfiles/rails_8_0.gemfile
4042
exclude:
4143
- os: windows-latest
4244
ruby: jruby
4345
- os: windows-latest
4446
ruby: truffleruby
47+
- gemfile: gemfiles/rails_6_1.gemfile
48+
ruby: '3.4'
49+
- gemfile: gemfiles/rails_6_1.gemfile
50+
ruby: ruby
51+
- gemfile: gemfiles/rails_7_0.gemfile
52+
ruby: '3.4'
53+
- gemfile: gemfiles/rails_7_0.gemfile
54+
ruby: ruby
55+
- gemfile: gemfiles/rails_7_1.gemfile
56+
ruby: jruby
57+
- gemfile: gemfiles/rails_7_1.gemfile
58+
ruby: truffleruby
59+
- gemfile: gemfiles/rails_8_0.gemfile
60+
ruby: '3.1'
61+
- gemfile: gemfiles/rails_8_0.gemfile
62+
ruby: jruby
63+
- gemfile: gemfiles/rails_8_0.gemfile
64+
ruby: truffleruby
4565
steps:
4666
- name: Checkout
4767
uses: actions/checkout@v3

.rubocop.yml

+3
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,8 @@ Naming/FileName:
2323
Exclude:
2424
- 'lib/dartsass-sprockets.rb'
2525

26+
Style/EmptyMethod:
27+
EnforcedStyle: expanded
28+
2629
Style/HashSyntax:
2730
EnforcedShorthandSyntax: never

CHANGELOG.md

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
- **3.2.0**
2+
- Bump sassc-embedded dependency to 1.80.1.
3+
- Allow pass-thru of all sass-embedded options.
4+
- @ntkme - [Fix custom importer and avoid mutex lock for custom functions](https://github.com/tablecheck/dartsass-sprockets/pull/22)
5+
- @botandrose - [Restore import tracking from Sprockets::SassProcessor so that edits to secondary imports are tracked](https://github.com/tablecheck/dartsass-sprockets/pull/29)
6+
- @jukra - [Alter the config to pass a --quiet-deps flag](https://github.com/tablecheck/dartsass-sprockets/pull/27)
7+
- Remove config.sass.line_comments.
8+
- Remove Sprockets 2.x related code.
9+
- Add Ruby 3.4 to Github CI tests.
10+
- Add Rails 8.0 to Github CI tests.
11+
- Silence @import deprecation in tests.
12+
- Fix broken tests.
13+
114
- **3.1.0**
215
- Change dependency from dartsass-ruby to sassc-embedded.
316
- Drop support for Ruby prior to 3.1.

Gemfile

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
source 'https://rubygems.org'
44

5+
gem 'mocha'
6+
gem 'rake'
7+
gem 'rubocop', '~> 1.69.2'
8+
59
platforms :windows do
610
gem 'tzinfo-data'
711
end
812

9-
gem 'mocha'
10-
gem 'rake'
11-
gem 'rubocop'
12-
1313
gemspec

README.md

+59-17
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,80 @@ The current version of `dartsass-sprockets` supports:
3333

3434
For older versions of Ruby and Rails may be supported with earlier versions of this gem.
3535

36-
### Upgrading to Dart Sass
36+
## CSS Minification (Production)
3737

38-
This gem is a drop-in replacement to [sass-rails](https://github.com/rails/sass-rails).
39-
Note the following differences:
38+
This gem uses a Railtie to automatically set the following
39+
configuration in all environments *except* Development:
4040

41-
* This library does not apply SASS processing to `.css` files. Please ensure all your SASS files have file extension `.scss`.
42-
* `config.sass.style` values `:nested` and `:compact` will behave as `:expanded`. Use `:compressed` for minification.
43-
* `config.sass.line_comments` option is ignored and will always be disabled.
41+
```ruby
42+
# set automatically by this gem
43+
config.assets.css_compressor = :sass
44+
```
4445

45-
## Inline Source Maps
46+
This causes Sprockets to minify *all* CSS assets (both Sass and plain CSS) using Dart Sass.
47+
This minification is done as a *second-pass* after compiling the Sass to CSS,
48+
and is done irrespective of whether the `config.sass.style` option is set to `:compressed`.
49+
To disable this behavior, set `config.assets.css_compressor = false`.
50+
51+
## Source Maps (Development)
4652

4753
To turn on inline source maps, add the following configuration
48-
to your `development.rb` file:
54+
to your `config/environments/development.rb` file:
4955

5056
```ruby
51-
# config/environments/development.rb
57+
# in config/environments/development.rb
5258
config.sass.inline_source_maps = true
5359
```
5460

55-
After adding this config line, you may need to clear your assets cache
56-
(`rm -r tmp/cache/assets`), stop Spring, and restart your Rails server.
57-
58-
Note these source maps are *inline* and will be appended to the compiled
59-
`application.css` file. (They will *not* generate additional files.)
61+
Note these source maps appended *inline* to the compiled `application.css` file.
62+
(This option will *not* generate additional files.)
6063

61-
## Silencing Deprecation Warnings That Come From Dependencies
64+
## Silencing Deprecation Warnings
6265

63-
To silence deprecation warnings during compilation that come from dependencies, add the following configuration to your `application.rb` file:
66+
To silence common deprecation warnings, add the following
67+
configuration. Refer to details in the below section.
6468

6569
```ruby
66-
# config/environments/development.rb
70+
# in config/application.rb
6771
config.sass.quiet_deps = true
72+
config.sass.silence_deprecations = ['import']
6873
```
6974

75+
## Advanced Configuration
76+
77+
The following options are exposed via `Rails.application.config.sass.{option}`.
78+
Options denoted with * are handed by the sass-embedded gem and passed into Dart Sass;
79+
refer to [the sass-embedded documentation](https://rubydoc.info/gems/sass-embedded/Sass)
80+
and the [Dart Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/options/).
81+
82+
| Option | Type | Description |
83+
|-------------------------|-----------------|-------------------------------------------------------------------------------------------------------------------------------|
84+
| `load_paths` | `Array<String>` | Additional paths to look for imported files. |
85+
| `inline_source_maps` | `Boolean` | If `true`, will append source maps inline to the generated CSS file. Refer to section below. |
86+
| `style`* | `Symbol` | `:expanded` (default) or `:compressed`. See note about CSS Minification below. |
87+
| `charset`* | `Boolean` | Whether to include a @charset declaration or byte-order mark in the CSS output (default `true`). |
88+
| `logger`* | `Object` | An object to use to handle warnings and/or debug messages from Sass. |
89+
| `alert_ascii`* | `Boolean` | If `true`, Dart Sass will exclusively use ASCII characters in its error and warning messages (default `false`). |
90+
| `alert_color`* | `Boolean` | If `true`, Dart Sass will use ANSI color escape codes in its error and warning messages (default `false`). |
91+
| `verbose`* | `Boolean` | By default (`false`) Dart Sass logs up to five occurrences of each deprecation warning. Setting to `true` removes this limit. |
92+
| `quiet_deps`* | `Boolean` | If `true`, Dart Sass won’t print warnings that are caused by dependencies (default `false`). |
93+
| `silence_deprecations`* | `Array<String>` | An array of active deprecations to ignore. Refer to (deprecations)[dartsass-deprecations]. |
94+
| `fatal_deprecations`* | `Array<String>` | An array of deprecations to treat as fatal. Refer to (deprecations)[dartsass-deprecations]. |
95+
| `future_deprecations`* | `Array<String>` | An array of future deprecations to opt-into early. Refer to (deprecations)[dartsass-deprecations]. |
96+
| `importers`* | `Array<Object>` | Custom importers to use when resolving `@import` directives. |
97+
98+
When changing config options in Development environment, you may need to clear
99+
your assets cache (`rm -r tmp/cache/assets`) and restart your Rails server.
100+
101+
### Upgrading from Legacy Sass Rails
102+
103+
This gem is a drop-in replacement to [sass-rails](https://github.com/rails/sass-rails).
104+
Note the following differences:
105+
106+
* This library does not apply SASS processing to `.css` files. Please ensure all your SASS files have file extension `.scss`.
107+
* `config.sass.style` values `:nested` and `:compact` will behave as `:expanded`. Use `:compressed` for minification.
108+
* `config.sass.line_comments` option is ignored and will always be disabled.
109+
70110
## Alternatives
71111

72112
* [dartsass-rails](https://github.com/rails/dartsass-rails): The Rails organization
@@ -89,3 +129,5 @@ config.sass.quiet_deps = true
89129
3. Commit your changes (`git commit -am 'Add some feature'`) - try to include tests
90130
4. Push to the branch (`git push origin my-new-feature`)
91131
5. Create a new Pull Request
132+
133+
[dartsass-deprecations]: https://github.com/sass/sass/blob/40c50cb/js-api-doc/deprecations.d.ts#L260

dartsass-sprockets.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
1818
spec.required_ruby_version = '>= 3.1'
1919

2020
spec.add_dependency 'railties', '>= 4.0.0'
21-
spec.add_dependency 'sassc-embedded', '~> 1.69'
21+
spec.add_dependency 'sassc-embedded', '~> 1.80.1'
2222
spec.add_dependency 'sprockets', '> 3.0'
2323
spec.add_dependency 'sprockets-rails'
2424
spec.add_dependency 'tilt'

gemfiles/rails_8_0.gemfile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
eval_gemfile('../Gemfile')
4+
5+
gem 'rails', '~> 8.0.0'

lib/sassc/rails/compressor.rb

+1-10
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,7 @@ def initialize(options = {})
1616
end
1717

1818
def call(*args)
19-
input = if defined?(data)
20-
data # sprockets 2.x
21-
else
22-
args[0][:data] # sprockets 3.x
23-
end
24-
25-
SassC::Engine.new(input, { style: :compressed }).render
19+
SassC::Engine.new(args[0][:data], { style: :compressed }).render
2620
end
27-
28-
# sprockets 2.x
29-
alias evaluate call
3021
end
3122
end

lib/sassc/rails/railtie.rb

+2-8
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ class Railtie < ::Rails::Railtie
1515
# Initialize the load paths to an empty array
1616
config.sass.load_paths = []
1717

18-
# Display line comments above each selector as a debugging aid
19-
config.sass.line_comments = true
20-
2118
# Silence deprecation warnings during compilation that come from dependencies
2219
config.sass.quiet_deps = false
2320

@@ -75,11 +72,8 @@ class Railtie < ::Rails::Railtie
7572
end
7673

7774
initializer :setup_compression, group: :all do |app|
78-
if ::Rails.env.development?
79-
# Use expanded output instead of the sass default of :nested unless specified
80-
app.config.sass.style ||= :expanded
81-
else
82-
app.config.assets.css_compressor = :sass unless app.config.assets.key?(:css_compressor)
75+
unless ::Rails.env.development?
76+
app.config.assets.css_compressor = :sass unless app.config.assets.key?(:css_compressor) # rubocop:disable Style/SoleNestedConditional
8377
end
8478
end
8579
end

lib/sassc/rails/template.rb

+28-40
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@
88
module SassC
99
module Rails
1010
class SassTemplate < Sprockets::SassProcessor
11+
PASS_THRU_OPTIONS = %i[
12+
style
13+
charset
14+
importers
15+
logger
16+
alert_ascii
17+
alert_color
18+
verbose
19+
quiet_deps
20+
silence_deprecations
21+
fatal_deprecations
22+
future_deprecations
23+
].freeze
24+
1125
def initialize(options = {}, &block) # rubocop:disable Lint/MissingSuper
1226
@cache_version = options[:cache_version]
1327
@cache_key = "#{self.class.name}:#{VERSION}:#{::SassC::VERSION}:#{@cache_version}"
@@ -21,25 +35,33 @@ def initialize(options = {}, &block) # rubocop:disable Lint/MissingSuper
2135
end
2236
end
2337

24-
def call(input)
38+
def call(input) # rubocop:disable Metrics/AbcSize
2539
context = input[:environment].context_class.new(input)
2640

2741
options = {
42+
load_paths: input[:environment].paths | (::Rails.application.config.sass.load_paths || []),
2843
filename: input[:filename],
29-
line_comments: line_comments?,
3044
syntax: self.class.syntax,
31-
load_paths: input[:environment].paths,
3245
functions: @functions,
3346
importer: @importer_class,
34-
quiet_deps: ::Rails.application.config.sass.quiet_deps,
3547
sprockets: {
3648
context: context,
3749
environment: input[:environment],
3850
dependencies: context.metadata[:dependency_paths]
3951
}
40-
}.merge!(config_options) { |key, left, right| safe_merge(key, left, right) }
52+
}
53+
54+
PASS_THRU_OPTIONS.each do |option|
55+
options[option] = ::Rails.application.config.sass.send(option)
56+
end
57+
58+
if ::Rails.application.config.sass.inline_source_maps
59+
options.merge!(source_map_file: '.',
60+
source_map_embed: true,
61+
source_map_contents: true)
62+
end
4163

42-
engine = ::SassC::Engine.new(input[:data], options)
64+
engine = ::SassC::Engine.new(input[:data], options.compact)
4365

4466
css = engine.render
4567

@@ -53,40 +75,6 @@ def call(input)
5375
context.metadata.merge(data: css, sass_dependencies: sass_dependencies)
5476
end
5577

56-
def config_options
57-
opts = { style: sass_style, load_paths: load_paths }
58-
59-
if ::Rails.application.config.sass.inline_source_maps
60-
opts.merge!(source_map_file: '.',
61-
source_map_embed: true,
62-
source_map_contents: true)
63-
end
64-
65-
opts
66-
end
67-
68-
def sass_style
69-
(::Rails.application.config.sass.style || :expanded).to_sym
70-
end
71-
72-
def load_paths
73-
::Rails.application.config.sass.load_paths || []
74-
end
75-
76-
def line_comments?
77-
::Rails.application.config.sass.line_comments
78-
end
79-
80-
def safe_merge(_key, left, right)
81-
if [left, right].all? { |v| v.is_a? Hash }
82-
left.merge(right) { |k, l, r| safe_merge(k, l, r) }
83-
elsif [left, right].all? { |v| v.is_a? Array }
84-
(left + right).uniq
85-
else
86-
right
87-
end
88-
end
89-
9078
# The methods in the Functions module were copied here from sprockets in order to
9179
# override the Value class names (e.g. ::SassC::Script::Value::String)
9280
module Functions

lib/sassc/rails/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module SassC
44
module Rails
5-
VERSION = '3.1.0'
5+
VERSION = '3.2.0'
66
end
77
end

test/dummy/app/assets/stylesheets/imports_test.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@import "partials/css_sass_import";
22
@import "partials/sass_import";
33
@import "partials/scss_import";
4-
@import "partials/explicit_extension_import.foo";
4+
// @import "partials/explicit_extension_import.foo";
55
@import "globbed/**/*";
66
@import "subfolder/plain";
77
@import "subfolder/second_level";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.goodbye {
2+
color: #FFF;
3+
}

0 commit comments

Comments
 (0)