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

No source maps generated #23

Open
sudoremo opened this issue Jan 18, 2024 · 5 comments
Open

No source maps generated #23

sudoremo opened this issue Jan 18, 2024 · 5 comments

Comments

@sudoremo
Copy link

Problem

According to the documentation, you can enable the generation of inline source maps using the following configuration:

Rails.application.config.sass.inline_source_maps = true

In our case though, this does not generate any source maps in the generated application.css, even after removing tmp/cache entirely. This is in development mode.

Diagnosis

In SassC::Rails::Template#config_options, I have verified that inline_source_maps is indeed enabled, which in turn sets the following options:

opts.merge!(source_map: true,
            source_map_file: '.',
            source_map_embed: true,
            source_map_contents: true)

Looking at the API of sass-embedded, I can't find the aforementioned options though. There are the options source_map and source_map_include_sources instead. Though enabling these does not produce a source map either.

Environment

  • rails (7.1.3)
  • sprockets (3.7.2)
  • sprockets-rails (3.2.2)
  • dartsass-sprockets (3.1.0)
  • sassc-embedded (1.70.0)

Many thanks for your wonderful Gem and for having a look at our issue 👍

@JosephTico
Copy link

Same issue

@keithslater
Copy link

Same problem here

@Mr0grog
Copy link

Mr0grog commented Oct 11, 2024

I’m having the same issue.

Looking at the API of sass-embedded, I can't find the aforementioned options though. There are the options source_map and source_map_include_sources instead. Though enabling these does not produce a source map either.

I can at least explain this: the code here calls sassc-embedded (and then that calls sass-embedded). sassc-embedded takes the options used here (it’s emulating the API of sassc-ruby) and transforms them into the two options you are looking for when it passes things along to sass-embedded: https://github.com/sass-contrib/sassc-embedded-shim-ruby/blob/68900334f3298c2d059be690088824c550fc072e/lib/sassc/embedded.rb#L26-L27


Looking at what's happening under the hood in my install (sprockets 4.2.1, sprockets-rails 3.5.2, dartsass-sprockets 3.1.0, sassc-embedded 1.79.0), dartsass-sprockets is getting back a CSS string with a source map in it, so probably something higher up in sprockets or sprockets-rails is stripping out the comment when handling the result.

@Mr0grog
Copy link

Mr0grog commented Oct 11, 2024

I figured this out for my case, and it didn’t have anything to do with this gem. Other people might be having issues for a different reason.

For me, the problem was that the app I’m working on used Bootstrap, and the Bootstrap gem pulls in the autoprefixer-rails gem. Even though autoprefixer-rails supports inline source maps when using it manually, it strips them out when using the Sprockets integration (and it autoregisters itself with Sprockets).

If you’ve got autoprefixer-rails installed and still need inline source maps, you can remove it from Sprockets (unfortunately this means no autoprefixing 🤷). You can add this in an initializer or in development.rb:

Rails.application.config.assets.configure do |env|
  AutoprefixerRails.uninstall(env)
end
Alternatively, if you want to keep autoprefixer around…

You can replace the built-in integration with one that supports inline source maps, but it’s a bit more complicated. This worked for me, but I haven’t tested extensively:

# config/initializers/autoprefixer.rb

class AutoprefixerWithSourcemap < AutoprefixerRails::Sprockets
  def self.run(filename, css)
    output = "#{filename.chomp(File.extname(filename))}.css"

    # If you want this nice an generic, you could check `css` for the presence
    # of an inline source map, and set the `map` argument based on that
    # instead of always setting it to `true`.
    result = @processor.process(css, from: filename, to: output, map: true)

    result.warnings.each do |warning|
      warn "autoprefixer: #{warning}"
    end

    result.css
  end

  def self.use_bundle_processor?
    ::Sprockets::VERSION.to_f >= 4
  end

  def self.install(env)
    if use_bundle_processor?
      env.register_bundle_processor("text/css", self)
    else
      env.register_postprocessor("text/css", self)
    end
  end

  def self.uninstall(env)
    if use_bundle_processor?
      env.unregister_bundle_processor("text/css", self)
    else
      env.unregister_postprocessor("text/css", self)
    end
  end
end

Rails.application.config.assets.configure do |env|
  AutoprefixerRails.uninstall(env)
  AutoprefixerWithSourcemap.register_processor(AutoprefixerRails.processor({}))
  AutoprefixerWithSourcemap.install(env)
end

I’ve also filed an issue on autoprefixer-rails if you want to follow that: ai/autoprefixer-rails#228

@JosephTico
Copy link

Oh wow you were spot on with that one, @Mr0grog im pretty sure that was also my situation, thank you very much

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

4 participants