Search code examples
webpacksassbootstrap-4node-sass

Bootstrap 4 Webpack Sass functions not working


Having an issue with Webpack and Bootstrap 4 mixins and functions.

sass|css loader:

{
    test: /\.(sa|sc|c)ss$/,
    use: [
        // fallback to style-loader in development
        // which creates style nodes from JS strings (IE: imports)
        process.env.NODE_ENV === 'development' ? 'style-loader' : MiniCssExtractPlugin.loader,
        {
            loader: 'css-loader',
            options: {
                sourceMap: true
            }
        },
        {
            loader: 'sass-loader',
            options: {
                sourceMap: true,
                includePaths: ['node_modules']
            }
        }
    ]
}

imported with:

@import "bootstrap/scss/functions";
@import "bootstrap/scss/variables";
@import "bootstrap/scss/mixins";
@import "bootstrap/scss/print";
@import "bootstrap/scss/reboot";
@import "bootstrap/scss/type";
@import "bootstrap/scss/images";
@import "bootstrap/scss/code";
@import "bootstrap/scss/grid";
@import "bootstrap/scss/tables";
@import "bootstrap/scss/forms";
@import "bootstrap/scss/buttons";
@import "bootstrap/scss/transitions";
@import "bootstrap/scss/dropdown";
@import "bootstrap/scss/button-group";
@import "bootstrap/scss/input-group";
@import "bootstrap/scss/custom-forms";
@import "bootstrap/scss/nav";
@import "bootstrap/scss/navbar";
@import "bootstrap/scss/card";
@import "bootstrap/scss/breadcrumb";
@import "bootstrap/scss/pagination";
@import "bootstrap/scss/badge";
@import "bootstrap/scss/jumbotron";
@import "bootstrap/scss/alert";
@import "bootstrap/scss/progress";
@import "bootstrap/scss/media";
@import "bootstrap/scss/list-group";
@import "bootstrap/scss/close";
@import "bootstrap/scss/modal";
@import "bootstrap/scss/tooltip";
@import "bootstrap/scss/popover";
@import "bootstrap/scss/carousel";
@import "bootstrap/scss/utilities";

Everything seems to work great, except when it comes to the mixins and functions.

For example, color: color("purple"); works just fine, and places the proper color, however a simple <span className="badge badge-green ml-2">badge here</span> loads the base styles for the badge, but not the color variant for green.

There are no errors of any kind, so I'm not sure what's going on.


Solution

  • Did you define the color variant "green" in the $theme-colors bootstrap variable?

    By default there are the following options only (copied from bootstrap/_variables.scss):

    $theme-colors: () !default;
    // stylelint-disable-next-line scss/dollar-variable-default
    $theme-colors: map-merge(
      (
        "primary":    $primary,
        "secondary":  $secondary,
        "success":    $success,
        "info":       $info,
        "warning":    $warning,
        "danger":     $danger,
        "light":      $light,
        "dark":       $dark
      ),
      $theme-colors
    );
    

    The $theme-color variable is used by all the components for the color modifiers like this (copied from bootstrap/_badge.scss using the badge component as an example):

    @each $color, $value in $theme-colors {
      .badge-#{$color} {
        @include badge-variant($value);
      }
    }
    

    So if you want to use a color variant "green" you have to register it in the $theme-colors variable first like this (wherever you override your default bootstrap variables):

    $theme-colors: (
      "green": #00ff00
    );
    

    More info can be found at the documentation for the badge component (https://getbootstrap.com/docs/4.1/components/badge/) and the documentation for theme colors (https://getbootstrap.com/docs/4.1/getting-started/theming/#theme-colors) by bootstrap itself.