Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override Spree Commerce's Bootstrap Variables

I'm having an issue with deploying a customized _variables.scss to my production server as a compiled asset.

Everything is fine on my development environment, it's in production that my variables are being overwritten.

I'm using Rails 4.2.1 with Spree 3.0 Stable branch.

I have the following structure:

Files created in vendor/assets/stylesheets/frontend

  • _variables.scss (my custom app variables)
  • all.css (generated by Spree)
  • frontend_bootstrap.css.scss (override Spree)
  • navbar.scss (my customization)

The _variables.scss contains the following:

// Place all Sass variables here.

// Colors
$brand-primary: green;
$gray: #aaa;

// Navbar
$navbar-default-bg: #fff;
$navbar-height: 100px;
$navbar-border-radius: 0;
$navbar-default-border: none;
$navbar-default-toggle-hover-bg: $navbar-default-bg;
$navbar-default-toggle-icon-bar-bg: lighten($gray, 60%);
$navbar-default-toggle-border-color: $navbar-default-bg;
$navbar-default-link-active-bg: $brand-primary;

The frontend_boostrap.css.scss contains the following:

// Spree Bootstrap Override

// Core
@import "variables";
@import "bootstrap-sprockets";
@import "bootstrap";

// Custom Overrides
@import "navbar";

The navbar.scss contains the following:

// Navbar Customization

.navbar-myapp {
  margin-bottom: 40px;
  border-top: none;
  border-bottom: 1px solid $navbar-default-toggle-icon-bar-bg;

  .navbar-brand {
    padding: 15px;
  }
}

The Rails standard app/assets/stylesheets/application.css manifest isn't being used/I haven't declared anything specfic in there.

The produced HTML head code shows all.css and frontend.

<link rel="stylesheet" media="screen" href="/assets/spree/frontend/all.self-33fc4a513acb9a5f3fd4ba26b89c94184e5d028c4bd40eee6736d3ccfea5c140.css?body=1">

<link rel="stylesheet" media="screen" href="/assets/spree/frontend/frontend_bootstrap.self-88eb7ced3e4d78d298a33264c3cfc65af6cef8ac32ae56a7dd7a3e321ba97378.css?body=1">

All is well in development but when I deploy this to my test server, some of the variables are being overwritten by the default, this includes the navbar configuration and a color.

Compiled Assets on Server

I'm not sure if this is because of asset compilation order; or if it's how bootstrap-sass is imported.

Any suggestion on how I can go about using _variables.scss without it being overwritten? I didn't want any duplication, that's why I wanted to change the navbar and colors in the the variables sass file.

like image 410
Wasabi Developer Avatar asked Jan 09 '23 05:01

Wasabi Developer


1 Answers

It looks like I've solved the issue.

The Bootstrap Sass gem states:

Do not use //= require in Sass or your other stylesheets will not be able to access the Bootstrap mixins or variables.

To get this working in Production / compiled assets. I had to:

  1. Change all.css to all.scss
  2. Change the //= require statements to @import

The vendor/assets/stylesheets/spree/frontend/all.scss:

// Sass Application Manifest

@import "frontend_bootstrap";

The vendor/assets/stylesheets/spree/frontend/frontend_bootstrap.css.scss:

// Spree Bootstrap Override

// Core
@import "bootstrap-sprockets";
@import "variables";
@import "bootstrap";

I hope this helps anyone who stumbled like I did.

like image 173
Wasabi Developer Avatar answered Jan 14 '23 22:01

Wasabi Developer