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

Pass raw params to less parser #116

Closed
wants to merge 7 commits into from
Closed
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

### 2.7.2 - ?

* Make it possible to pass parameters to less.rb
Fixes https://github.com/metaskills/less-rails/issues/94

### 2.7.1 - 2016-09-01

* Fix Sprockets 3 integration. Thanks @suzan2go and @ccallebs!
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ Will end up acting as if you had done this below:
#leftnav { .border-radius(5px); }
```

## Passing parameters to less.rb

You can pass any parameters that the [`less.rb`](https://github.com/cowboyd/less.rb) gem (which `less-rails` is based upon) supports by modifying `Rails.application.config.less.raw`.
For example, `less.rb` uses [lessc --relative-urls](http://lesscss.org/usage/#command-line-usage-relative-urls) by default. This means that `url('../ralative/paths.png')` for `@import`ed files will be modified according to `.less` file location. To return back to default `lessc` behavior, add these lines to `config/initializers/assets.rb`:

```ruby
Rails.application.config.less.raw.relativeUrls = false
```

For more parameters supported by `less.rb`, please consult [less.rb gem](https://github.com/cowboyd/less.rb/blob/master/lib/less/defaults.rb).


## Helpers
Expand Down
1 change: 1 addition & 0 deletions lib/less/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Railtie < ::Rails::Railtie
config.less = ActiveSupport::OrderedOptions.new
config.less.paths = []
config.less.compress = false
config.less.raw = ActiveSupport::OrderedOptions.new
config.app_generators.stylesheet_engine :less

config.before_initialize do |app|
Expand Down
6 changes: 5 additions & 1 deletion lib/less/rails/template_handlers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def config_to_less_parser_options(scope)
paths = config_paths(scope) + scope.environment.paths
local_path = scope.pathname.dirname
paths += [local_path] unless paths.include? local_path
{:filename => eval_file, :line => line, :paths => paths, :dumpLineNumbers => config_from_rails(scope).line_numbers}
{:filename => eval_file, :line => line, :paths => paths, :dumpLineNumbers => config_from_rails(scope).line_numbers}.merge(config_raw(scope))
end

def config_to_css_options(scope)
Expand All @@ -36,6 +36,10 @@ def config_to_css_options(scope)
def config_paths(scope)
config_from_rails(scope)[:paths]
end

def config_raw(scope)
config_from_rails(scope)[:raw]
end

def config_from_rails(scope)
scope.environment.context_class.less_config
Expand Down
15 changes: 15 additions & 0 deletions test/cases/basics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ class BasicsSpec < Less::Rails::Spec

end

describe 'relative path setting must be effective' do
after do
Rails.application.config.less.raw.clear
end

it 'must use relavite paths by default' do
basics.must_match %r{body\{background-image:url\('i-have-no-imagination.png'\)\}}
end

it 'should respond to config modification' do
Rails.application.config.less.raw.relativeUrls = false
basics.must_match %r{body\{background-image:url\('../../i-have-no-imagination.png'\)\}}
end
end

protected

def basics
Expand Down
2 changes: 2 additions & 0 deletions test/dummy_app/app/assets/stylesheets/basics.css.less
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@
// Vendored
@import "vendored";
#test-vendored { .vendored; }

@import "frameworks/bootstrap/relative_path_url";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-image: url('../../i-have-no-imagination.png');
}