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

Using the Rails asset pipeline for images #730

Closed
tijmenb opened this issue May 29, 2018 · 3 comments · Fixed by #733
Closed

Using the Rails asset pipeline for images #730

tijmenb opened this issue May 29, 2018 · 3 comments · Fixed by #733

Comments

@tijmenb
Copy link

tijmenb commented May 29, 2018

In Rails we use helpers to refer to assets in CSS:

background-image: image-url("icon-pointer.png");
background-image: asset-url("icon-pointer.png");

This makes the asset pipeline in Rails work: instead of linking to icon-pointer.png, it will link to the fingerprinted version of the file.

Currently GOV.UK Frontend just uses url("file.png") in the CSS, so Rails won't point the correct assets:

@return url($govuk-global-images + $file);

For GOV.UK to use GOV.UK Frontend, we'll need a way to override the govuk-file-url or to instruct it to use asset-url.

Options

Defining the function first

I thought maybe defining the govuk-file-url function above the @import for GOV.UK Frontend would work, but the later definition overrides this.

Defining the function last

This doesn't work because govuk-file-url is already used by the CSS.

Conditionally defining the function

I though maybe if we wrap the function in a conditional like below, our override might work.

@if function-exists('govuk-file-url') == false {
  @function govuk-file-url($file) {
    @return url($govuk-global-images + $file);
  }
}

Unfortunately SCSS errors out with:

Functions may not be defined within control directives or other mixins.

Conditional inside function

This works:

$govuk-use-rails: true;

@function govuk-file-url($file) {
  @if $govuk-use-rails {
    @return asset-url($file);
  } else {
    @return url($govuk-global-images + $file);
  }
}

This follows a spike into using GOV.UK Frontend on GOV.UK (alphagov/govuk_publishing_components#328).

@36degrees
Copy link
Contributor

Hey @tijmenb,

Thanks for raising – can you take a look at #733 and see if it'll address your needs?

Ta

@tijmenb
Copy link
Author

tijmenb commented May 30, 2018

@36degrees tested by hacking it into the PR: alphagov/govuk_publishing_components@643c482. Works well! 👌

@NickColley
Copy link
Contributor

Thanks for the great issue @tijmenb 👍

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

Successfully merging a pull request may close this issue.

3 participants