Skip to content

Commit

Permalink
Various improvements (#30)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
johnnyshields authored Jan 2, 2025
1 parent dc8e3b2 commit 2a27f2b
Show file tree
Hide file tree
Showing 14 changed files with 219 additions and 164 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,38 @@ jobs:
- '3.1'
- '3.2'
- '3.3'
- '3.4'
- ruby
- jruby
- truffleruby
gemfile:
- gemfiles/rails_6_1.gemfile
- gemfiles/rails_7_0.gemfile
- gemfiles/rails_7_1.gemfile
- gemfiles/rails_8_0.gemfile
exclude:
- os: windows-latest
ruby: jruby
- os: windows-latest
ruby: truffleruby
- gemfile: gemfiles/rails_6_1.gemfile
ruby: '3.4'
- gemfile: gemfiles/rails_6_1.gemfile
ruby: ruby
- gemfile: gemfiles/rails_7_0.gemfile
ruby: '3.4'
- gemfile: gemfiles/rails_7_0.gemfile
ruby: ruby
- gemfile: gemfiles/rails_7_1.gemfile
ruby: jruby
- gemfile: gemfiles/rails_7_1.gemfile
ruby: truffleruby
- gemfile: gemfiles/rails_8_0.gemfile
ruby: '3.1'
- gemfile: gemfiles/rails_8_0.gemfile
ruby: jruby
- gemfile: gemfiles/rails_8_0.gemfile
ruby: truffleruby
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ Naming/FileName:
Exclude:
- 'lib/dartsass-sprockets.rb'

Style/EmptyMethod:
EnforcedStyle: expanded

Style/HashSyntax:
EnforcedShorthandSyntax: never
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
- **3.2.0**
- Bump sassc-embedded dependency to 1.80.1.
- Allow pass-thru of all sass-embedded options.
- @ntkme - [Fix custom importer and avoid mutex lock for custom functions](https://github.com/tablecheck/dartsass-sprockets/pull/22)
- @botandrose - [Restore import tracking from Sprockets::SassProcessor so that edits to secondary imports are tracked](https://github.com/tablecheck/dartsass-sprockets/pull/29)
- @jukra - [Alter the config to pass a --quiet-deps flag](https://github.com/tablecheck/dartsass-sprockets/pull/27)
- Remove config.sass.line_comments.
- Remove Sprockets 2.x related code.
- Add Ruby 3.4 to Github CI tests.
- Add Rails 8.0 to Github CI tests.
- Silence @import deprecation in tests.
- Fix broken tests.

- **3.1.0**
- Change dependency from dartsass-ruby to sassc-embedded.
- Drop support for Ruby prior to 3.1.
Expand Down
8 changes: 4 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

source 'https://rubygems.org'

gem 'mocha'
gem 'rake'
gem 'rubocop', '~> 1.69.2'

platforms :windows do
gem 'tzinfo-data'
end

gem 'mocha'
gem 'rake'
gem 'rubocop'

gemspec
76 changes: 59 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,80 @@ The current version of `dartsass-sprockets` supports:

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

### Upgrading to Dart Sass
## CSS Minification (Production)

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

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

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

## Source Maps (Development)

To turn on inline source maps, add the following configuration
to your `development.rb` file:
to your `config/environments/development.rb` file:

```ruby
# config/environments/development.rb
# in config/environments/development.rb
config.sass.inline_source_maps = true
```

After adding this config line, you may need to clear your assets cache
(`rm -r tmp/cache/assets`), stop Spring, and restart your Rails server.

Note these source maps are *inline* and will be appended to the compiled
`application.css` file. (They will *not* generate additional files.)
Note these source maps appended *inline* to the compiled `application.css` file.
(This option will *not* generate additional files.)

## Silencing Deprecation Warnings That Come From Dependencies
## Silencing Deprecation Warnings

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

```ruby
# config/environments/development.rb
# in config/application.rb
config.sass.quiet_deps = true
config.sass.silence_deprecations = ['import']
```

## Advanced Configuration

The following options are exposed via `Rails.application.config.sass.{option}`.
Options denoted with * are handed by the sass-embedded gem and passed into Dart Sass;
refer to [the sass-embedded documentation](https://rubydoc.info/gems/sass-embedded/Sass)
and the [Dart Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/options/).

| Option | Type | Description |
|-------------------------|-----------------|-------------------------------------------------------------------------------------------------------------------------------|
| `load_paths` | `Array<String>` | Additional paths to look for imported files. |
| `inline_source_maps` | `Boolean` | If `true`, will append source maps inline to the generated CSS file. Refer to section below. |
| `style`* | `Symbol` | `:expanded` (default) or `:compressed`. See note about CSS Minification below. |
| `charset`* | `Boolean` | Whether to include a @charset declaration or byte-order mark in the CSS output (default `true`). |
| `logger`* | `Object` | An object to use to handle warnings and/or debug messages from Sass. |
| `alert_ascii`* | `Boolean` | If `true`, Dart Sass will exclusively use ASCII characters in its error and warning messages (default `false`). |
| `alert_color`* | `Boolean` | If `true`, Dart Sass will use ANSI color escape codes in its error and warning messages (default `false`). |
| `verbose`* | `Boolean` | By default (`false`) Dart Sass logs up to five occurrences of each deprecation warning. Setting to `true` removes this limit. |
| `quiet_deps`* | `Boolean` | If `true`, Dart Sass won’t print warnings that are caused by dependencies (default `false`). |
| `silence_deprecations`* | `Array<String>` | An array of active deprecations to ignore. Refer to (deprecations)[dartsass-deprecations]. |
| `fatal_deprecations`* | `Array<String>` | An array of deprecations to treat as fatal. Refer to (deprecations)[dartsass-deprecations]. |
| `future_deprecations`* | `Array<String>` | An array of future deprecations to opt-into early. Refer to (deprecations)[dartsass-deprecations]. |
| `importers`* | `Array<Object>` | Custom importers to use when resolving `@import` directives. |

When changing config options in Development environment, you may need to clear
your assets cache (`rm -r tmp/cache/assets`) and restart your Rails server.

### Upgrading from Legacy Sass Rails

This gem is a drop-in replacement to [sass-rails](https://github.com/rails/sass-rails).
Note the following differences:

* This library does not apply SASS processing to `.css` files. Please ensure all your SASS files have file extension `.scss`.
* `config.sass.style` values `:nested` and `:compact` will behave as `:expanded`. Use `:compressed` for minification.
* `config.sass.line_comments` option is ignored and will always be disabled.

## Alternatives

* [dartsass-rails](https://github.com/rails/dartsass-rails): The Rails organization
Expand All @@ -89,3 +129,5 @@ config.sass.quiet_deps = true
3. Commit your changes (`git commit -am 'Add some feature'`) - try to include tests
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request

[dartsass-deprecations]: https://github.com/sass/sass/blob/40c50cb/js-api-doc/deprecations.d.ts#L260
2 changes: 1 addition & 1 deletion dartsass-sprockets.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 3.1'

spec.add_dependency 'railties', '>= 4.0.0'
spec.add_dependency 'sassc-embedded', '~> 1.69'
spec.add_dependency 'sassc-embedded', '~> 1.80.1'
spec.add_dependency 'sprockets', '> 3.0'
spec.add_dependency 'sprockets-rails'
spec.add_dependency 'tilt'
Expand Down
5 changes: 5 additions & 0 deletions gemfiles/rails_8_0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

eval_gemfile('../Gemfile')

gem 'rails', '~> 8.0.0'
11 changes: 1 addition & 10 deletions lib/sassc/rails/compressor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,7 @@ def initialize(options = {})
end

def call(*args)
input = if defined?(data)
data # sprockets 2.x
else
args[0][:data] # sprockets 3.x
end

SassC::Engine.new(input, { style: :compressed }).render
SassC::Engine.new(args[0][:data], { style: :compressed }).render
end

# sprockets 2.x
alias evaluate call
end
end
10 changes: 2 additions & 8 deletions lib/sassc/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ class Railtie < ::Rails::Railtie
# Initialize the load paths to an empty array
config.sass.load_paths = []

# Display line comments above each selector as a debugging aid
config.sass.line_comments = true

# Silence deprecation warnings during compilation that come from dependencies
config.sass.quiet_deps = false

Expand Down Expand Up @@ -75,11 +72,8 @@ class Railtie < ::Rails::Railtie
end

initializer :setup_compression, group: :all do |app|
if ::Rails.env.development?
# Use expanded output instead of the sass default of :nested unless specified
app.config.sass.style ||= :expanded
else
app.config.assets.css_compressor = :sass unless app.config.assets.key?(:css_compressor)
unless ::Rails.env.development?
app.config.assets.css_compressor = :sass unless app.config.assets.key?(:css_compressor) # rubocop:disable Style/SoleNestedConditional
end
end
end
Expand Down
68 changes: 28 additions & 40 deletions lib/sassc/rails/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@
module SassC
module Rails
class SassTemplate < Sprockets::SassProcessor
PASS_THRU_OPTIONS = %i[
style
charset
importers
logger
alert_ascii
alert_color
verbose
quiet_deps
silence_deprecations
fatal_deprecations
future_deprecations
].freeze

def initialize(options = {}, &block) # rubocop:disable Lint/MissingSuper
@cache_version = options[:cache_version]
@cache_key = "#{self.class.name}:#{VERSION}:#{::SassC::VERSION}:#{@cache_version}"
Expand All @@ -21,25 +35,33 @@ def initialize(options = {}, &block) # rubocop:disable Lint/MissingSuper
end
end

def call(input)
def call(input) # rubocop:disable Metrics/AbcSize
context = input[:environment].context_class.new(input)

options = {
load_paths: input[:environment].paths | (::Rails.application.config.sass.load_paths || []),
filename: input[:filename],
line_comments: line_comments?,
syntax: self.class.syntax,
load_paths: input[:environment].paths,
functions: @functions,
importer: @importer_class,
quiet_deps: ::Rails.application.config.sass.quiet_deps,
sprockets: {
context: context,
environment: input[:environment],
dependencies: context.metadata[:dependency_paths]
}
}.merge!(config_options) { |key, left, right| safe_merge(key, left, right) }
}

PASS_THRU_OPTIONS.each do |option|
options[option] = ::Rails.application.config.sass.send(option)
end

if ::Rails.application.config.sass.inline_source_maps
options.merge!(source_map_file: '.',
source_map_embed: true,
source_map_contents: true)
end

engine = ::SassC::Engine.new(input[:data], options)
engine = ::SassC::Engine.new(input[:data], options.compact)

css = engine.render

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

def config_options
opts = { style: sass_style, load_paths: load_paths }

if ::Rails.application.config.sass.inline_source_maps
opts.merge!(source_map_file: '.',
source_map_embed: true,
source_map_contents: true)
end

opts
end

def sass_style
(::Rails.application.config.sass.style || :expanded).to_sym
end

def load_paths
::Rails.application.config.sass.load_paths || []
end

def line_comments?
::Rails.application.config.sass.line_comments
end

def safe_merge(_key, left, right)
if [left, right].all? { |v| v.is_a? Hash }
left.merge(right) { |k, l, r| safe_merge(k, l, r) }
elsif [left, right].all? { |v| v.is_a? Array }
(left + right).uniq
else
right
end
end

# The methods in the Functions module were copied here from sprockets in order to
# override the Value class names (e.g. ::SassC::Script::Value::String)
module Functions
Expand Down
2 changes: 1 addition & 1 deletion lib/sassc/rails/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module SassC
module Rails
VERSION = '3.1.0'
VERSION = '3.2.0'
end
end
2 changes: 1 addition & 1 deletion test/dummy/app/assets/stylesheets/imports_test.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@import "partials/css_sass_import";
@import "partials/sass_import";
@import "partials/scss_import";
@import "partials/explicit_extension_import.foo";
// @import "partials/explicit_extension_import.foo";
@import "globbed/**/*";
@import "subfolder/plain";
@import "subfolder/second_level";
Expand Down
3 changes: 3 additions & 0 deletions test/dummy/app/assets/stylesheets/plain_css.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.goodbye {
color: #FFF;
}
Loading

0 comments on commit 2a27f2b

Please sign in to comment.