Skip to content

Add an ability to build assets from Rails root folder #9

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ 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",
Copy link
Author

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)

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

"site.scss" => "site.css"
"engines/travel/app/assets/stylesheets/travel/main.scss" => "travel/main.css",
"lib/stylesheets/common.css" => "common.css",
}
```

The hash key is the relative path to a Sass file in `app/assets/stylesheets/` and the hash value will be the name of the file output to `app/assets/builds/`.

If file does not exist in `app/assets/stylesheets/` directory - relative path will be changed to `Rails.root`.

## Importing assets from gems
`dartsass:build` includes application [assets paths](https://guides.rubyonrails.org/asset_pipeline.html#search-paths) as Sass [load paths](https://sass-lang.com/documentation/at-rules/use#load-paths). Assuming the gem has made assets visible to the Rails application, no additional configuration is required to use them.

Expand Down
10 changes: 7 additions & 3 deletions lib/tasks/build.rake
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
EXEC_PATH = "#{Pathname.new(__dir__).to_s}/../../exe/dartsass"
CSS_LOAD_PATH = Rails.root.join("app/assets/stylesheets")
CSS_BUILD_PATH = Rails.root.join("app/assets/builds")
CSS_LOAD_FROM_RAILS_ROOT_PATH = Rails.root

def dartsass_build_mapping
Rails.application.config.dartsass.builds.map { |input, output|
"#{CSS_LOAD_PATH.join(input)}:#{CSS_BUILD_PATH.join(output)}"
}.join(" ")
builds_map = Rails.application.config.dartsass.builds.map { |input, output|
input_file_path = "#{CSS_LOAD_PATH.join(input)}"
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(" ")

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.

Copy link
Author

@mekhovov mekhovov Feb 23, 2022

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

end

def dartsass_build_options
Expand Down