Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tailwindcss-rails raising SassC::SyntaxError #82

Closed
sajjadmurtaza opened this issue Oct 12, 2021 · 14 comments
Closed

tailwindcss-rails raising SassC::SyntaxError #82

sajjadmurtaza opened this issue Oct 12, 2021 · 14 comments

Comments

@sajjadmurtaza
Copy link

sajjadmurtaza commented Oct 12, 2021

I am using gem 'tailwindcss-rails'

Layout:

    <%= stylesheet_link_tag "inter-font", "data-turbo-track": "reload" %>
    <%= stylesheet_link_tag "tailwind", "data-turbo-track": "reload" %>

Production:
config.assets.css_compressor = :purger

In circle ci it's raising a following error

rake aborted!
SassC::SyntaxError: Error: unterminated attribute selector for type
on line 1021:16 of stdin

.prose ol[type="A" s] {

---------------^
stdin:1021

@gardnerapp
Copy link

Also getting this error in when running integration & controller test found this stack overflow post

https://stackoverflow.com/questions/68898511/tailwindcss-typography-sasscsyntaxerror-error-unterminated-attribute-select

which lead me to use this in my gemfile

gem "tailwindcss-rails",
github: "dorianmariefr/tailwindcss-rails",
branch: "minimal"

@rockwellll
Copy link

I can confirm, same thing is happening with my setup.

@dhh
Copy link
Member

dhh commented Oct 12, 2021

@dixpac If you want to dive into this one. We have to find a way such that Sassc is not being applied to the TW output.

@dixpac
Copy link
Contributor

dixpac commented Oct 12, 2021

Alright. I'll give my best 😊

@ericdfields
Copy link

Same issue. Trying the forked gem for now. Subscribed 👍

@ccouton
Copy link

ccouton commented Nov 12, 2021

Is there any news about this issue? thanks

@gardnerapp
Copy link

Before my Rails App was running on version 6.1.4 . I just started a fresh app on Rails7.0.0Alpha2 and I am no longer encountering the issue I described in an earlier post.

@dhh
Copy link
Member

dhh commented Nov 14, 2021

This is not a problem on Rails 7 out of the box because we no longer default to having Sass turned on there. There's still a compatibility issue if you turn Sass on, because SassC doesn't understand the modern CSS syntax used by TW.

@MSchmidt
Copy link

At the point of running the compression we don't really need sass specifically. Any compressor should be fine, right? The Rails Guides still talk about yui-compressor which hasn't been touched since 2013. I did look for something newer, but sass-rails is pretty much mentioned everywhere. I guess it was too good for too long that nobody felt the need for an alternative.

@dhh
Copy link
Member

dhh commented Dec 14, 2021

Added a note to the README about this compatibility issue.

@domchristie
Copy link
Contributor

I ran into this recently (outside of using tailwindcss-rails). My Sprockets-based solutions was to create a custom compressor, utilising a modern compressor like CSSO. It does depend on Node/npm, but I thought I'd share the steps:

  1. Install csso-cli
yarn add csso-cli
# or npm install csso-cli --save 
  1. Register a custom Sprockets compressor in config/initializers/csso.rb
require "open3"

class CssoCompressor
  def self.call(input)
    puts "[CssoCompressor] Compressing…"

    # Copy the contents of the CSS file to a temp file 
    temp_file = Tempfile.new([input[:name], ".css"])
    temp_file.open
    temp_file.write(input[:data])
    temp_file.flush

    # Run the compressor and capture the output
    css, err, status = Open3.capture3("npx", "csso", temp_file.path)

    {data: css}
  end
end

Rails.application.config.assets.configure do |env|
  env.register_compressor "text/css", :csso, CssoCompressor
end

A Sprockets compressor is just a class with a call method that gets called after running the processors.

  1. Set the CSS compressor to :csso in production.rb
config.assets.css_compressor = :csso

/cc @MSchmidt

@AnalyzePlatypus
Copy link

@domchristie, that worked perfectly for me!
I also had to set CSS compressor to :csso in test.rb to let CI pass.

@liqites
Copy link

liqites commented May 28, 2022

@domchristie You saved me! Your solution works perfect.

@benjaminwood
Copy link

While monkeypatching isn't advisable, you may find this useful if you're in a transitional phase where you need tailwind & sass at the same time.

module SassCompressorMonkeypatch
  def call_processor(processor, input)
    super
  rescue SassC::SyntaxError => e
    raise unless processor == Sprockets::SassCompressor
    puts "Warning: Could not compress #{input[:filename]} with Sprockets::SassCompressor. Returning uncompressed result."
    metadata = (input[:metadata] || {}).dup
    metadata[:data] = input[:data]
    metadata
  end
end

Sprockets::Base.prepend(SassCompressorMonkeypatch)

This simply rescues SassC::SyntaxError and returns the input, which in the case of tailwind is already compressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests