-
Notifications
You must be signed in to change notification settings - Fork 45
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
Add an ability to build assets from Rails root folder #9
base: main
Are you sure you want to change the base?
Conversation
@@ -29,13 +29,16 @@ By default, only `app/assets/stylesheets/application.scss` will be built. If you | |||
``` | |||
# config/initializers/dartsass.rb | |||
Rails.application.config.dartsass.builds = { | |||
"app/index.sass" => "app.css", | |||
"site.scss" => "site.css" | |||
"index.sass" => "app.css", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed app/
folder from this example app/index.sass
as it gives us wrong impression that we are starting from Rail's root folder (but we are technically enforcing prefix app/assets/stylesheets/
to each source file)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah! that's correct. When I was integrating even I thought the same and gave path accordingly. But later realised that it is from app/assets/stylesheets
input_file_path = "#{CSS_LOAD_FROM_RAILS_ROOT_PATH.join(input)}" unless File.exist?(input_file_path) | ||
"#{input_file_path}:#{CSS_BUILD_PATH.join(output)}" | ||
} | ||
builds_map.uniq.join(" ") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like easy workaround for now.
Let me give a try to use assets's path instead of Rails.root
. IMO that will be more inclined to Rails-way.
This will allow use to write less lengthy file path.
I will play around it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome PR #11 was merged recently. so we have Rails.application.config.assets.paths
in load-path
now, which is great!
but it doesn't solve our use-case when we need to pre-compile (and watch) files located in non-standard assets folders outside of /app/assets/stylesheets
Update README file
2de1273
to
143945c
Compare
Instead of checking each path for existence in the |
@aergonaut yeah, great idea! I was thinking about it as well :) Rails.application.config.dartsass.builds = {
"index.sass" => "app.css", # relative to stylesheets path will be prefixed with CSS_LOAD_PATH
# generated full-path "/path-to-your-rails-app/app/assets/stylesheets/index.sass"
"/lib/stylesheets/common.css" => "common.css", # relative to rails path will be prefixed with Rails.root.join
# generated full-path "/path-to-your-rails-app/lib/stylesheets/common.css"
} @dhh @vovimayhem @chipairon @aergonaut let me know if you like this convention, I'll be happy to make changes 🍻 |
Ah, sorry, that's not what I meant. Let's say you configure Rails.application.config.dartsass.builds = {
"app/index.sass" => "app.css",
Rails.root.join("lib/stylesheets/common.sass") => "common.css"
} The first entry is a relative path, so it would be prefixed with The second entry is an absolute path. (In this case, I constructed it using We could test for absoluteness by converting the entry keys into Treating the entries like this would let people specify any file they want to be compiled. It's a potential footgun that they could specify files outside of the |
@aergonaut I'm fine with both options. |
I can't speak for the maintainers, but here is my suggestion: // file at app/asssets/stylesheets/common.scss
@import 'common'; Dartsass will be able to pick up the right load path, since For the case of entry points coming from engines, a similar approach could be taken. # config/initializers/dartsass.rb
Rails.application.config.dartsass.builds = {
...,
"engines/travel/app/assets/stylesheets/travel/main.scss" => "travel/main.css"
} Add an entry point file @import 'travel/main'; And in the initializer: # config/initializers/dartsass.rb
Rails.application.config.dartsass.builds = {
...,
"travel/main.scss" => "travel/main.css"
} While that approach could get the job done without any change to this gem, I agree it would be great to have a composable way to add entry points from engines via an initializer with this shape: # engines/travel/config/initializers/dartsass.rb
Rails.application.config.dartsass.builds << { # NOTE: not valid syntax right now, as 'builds' is a Hash
"travel/main.scss" => "travel/main.css"
} That mechanism would remove the need to know (from a host app) how a particular engine is building their assets and keep up with changes, letting each engine add to the pipeline what it needs. |
Hey folks, any progress on this PR? |
@elliotcm No. We dropped usage of dartsass-rails as of now. We will revisit that approach in future upgrades of our project |
hey @elliotcm could you explain the issue you are facing with? |
@mekhovov Pretty much the same issue as mentioned above; working within an engine and not being able to add gem assets into the dartsass pipeline from within the gem, you currently have to get the top level app developer to add a top level (S)CSS file as a workaround:
Ideally we'd want to skip step 2 so that the end user of the engine doesn't have to alter their app's assets until and unless they want to explicitly override them. |
This would be very useful for me. I would like to build assets from |
Add an ability to build assets from Rails root folder if it's css file is not found in
app/assets/stylesheets/
directory.It will allow us to include assets from
engines
,lib
,vendor
and any other non-standard folder.I have tried similar PR #7 but it doesn't really work as it doesn't handle assets outside
app/assets/stylesheets/
folder, which is enforced by the gem.To respect backward compatibility - we will check if asset exist in
app/assets/stylesheets/
folder, if it's not - we will setCSS_BUILD_PATH
folder to Rail's root directory which will allow us to pass any custom folder for assets we need.This is how we are using it in our app
Ideally, I would love to extend dartsass-rails gem API to support more abstracted build-map.
I was thinking about accepting
source
/output
folders andfile patterns
similarly to example above ^^^However, I haven't yet found found a good-enough-common pattern for it without introducing too much app-specific structure to the common gem...
Will be happy to brainstorm on that and improve implementation 🍺
I think current improvement makes sense, it's low risk and supports backward compatibility with existing API, expanding it for more complex rails apps like ours.