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

Feature Proposal - Environment dependant directives #692

Closed
TomasBarry opened this issue Aug 18, 2020 · 1 comment
Closed

Feature Proposal - Environment dependant directives #692

TomasBarry opened this issue Aug 18, 2020 · 1 comment

Comments

@TomasBarry
Copy link

Expected behavior

This feature proposal would address (at least) these issues:

I am proposing amending the process_directives method in lib/sprockets/directive_processor.rb which currently looks like:

def process_directives(directives)
  directives.each do |line_number, name, *args|
    begin
      send("process_#{name}_directive", *args)
    rescue Exception => e
      e.set_backtrace(["#{@filename}:#{line_number}"] + e.backtrace)
      raise e
    end
  end
end

My assumption is that within this method we can have access to Rails.env so that we can test for the environment that would be within args:

def process_directives(directives)
  directives.each do |line_number, name, *args|
    begin
      # If the target environment does not match the current environment
      # then skip the directive
      if args["target_environment"] && args["target_environment"] != Rails.env[args["target_environment"]]
        next
      end
      send("process_#{name}_directive", *args)
    rescue Exception => e
      e.set_backtrace(["#{@filename}:#{line_number}"] + e.backtrace)
      raise e
    end
  end
end

The API for the directive would be something along the lines of

//= DIRECTIVE PATH EXTENSION target_environment=[production|development|test|*]

Example:

//= link_directory ../stylesheets/some_specific_sheet .css target_environment=development

By default, the exclusion of target_environment would mean that the directive should apply to all environments. Perhaps multiple environments should be possible in one line too:

//= link_directory ../stylesheets/some_specific_sheet .css target_environment=[development,test]

Or perhaps event an exclude_environment argument should be present:

//= link_directory ../stylesheets/some_specific_sheet .css exclude_environment=production

//= link_directory ../stylesheets/some_specific_sheet .css exclude_environment=[production,test]

What drew me to this issue was GraphiQL from graphql-ruby which is only mounted in development and with the latest version of sprockets, the JS and CSS for GraphiQL would need to be added to the manifest.js file (//= link graphiql/rails/application.css) in all environments instead of just in development.

Actual behavior

Directives cannot be conditional depending on the Rails environment. Specifically for link_tree etc, assets are either pre-compiled in all environments or in none.

System configuration

  • Sprockets version - 4.0.2
  • Ruby version - 2.6.0

Example App (Reproduction) - THIS IS IMPORTANT YOUR ISSUE LIKELY WILL NOT BE RESOLVED WITHOUT THIS

Example of the manifest.js:

//= link_tree ../images
//= link_directory ../javascripts .js
//= link_directory ../stylesheets .css

//= link_directory ../stylesheets/some_specific_sheet_only_for_development .css target_environment=development
@schneems
Copy link
Member

Sorry, i just can't. If you have a proposal please send a PR or maybe write it up in your own library that extends sprockets or sprockets-rails. It's extremely hard for me to pre-approve any discussion without also seeing the code involved to implement it.

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

2 participants